I was wondering if it is possible to implement sorting for only one column in a dash DataTable. I’ve looked around the forums a bit and haven’t found any good answers confirming whether or not this functionality exists.
Currently, I set the sort_action attribute of my DataTable to ‘native’ which allows for sorting. However, I’d really only like to allow the user to be able to sort by the first three columns of the table. Is there a way to do this? From the docs it looks like I could potentially clobber something together whereby I use sort_action=‘custom’ and then implement a custom callback that simply ignores the sorting action if it was performed on a column I don’t want the user to be able to sort by. This seems messy and also wouldn’t remove the sorting UI elements on those columns, which could be confusing to a user.
Below is my current code for the app layout. current_matrix is just a Pandas dataframe I’m using to populate the table.
app.layout = html.Div([
html.Div([
dash_table.DataTable(
id='displayed-matrix',
columns=[{'name': col, 'id': col} for col in current_matrix.columns],
data = current_matrix.to_dict('records'),
sort_action='native'
)
])
])
I don’t think this is possible, although it would be an interesting feature to have. The solution you suggested seems to be the right way to go. You could style differently the sortable and non-sortable columns as explained here.
+1 on this feature. I have a need for this right now. I have a column that has a string representation of risk, something like 3:(Range: 0-11). For values in this column that are less than one, the string representation is <1. Sorting the string representation is problematic. If I can’t turn sorting off, is there a way to sort that column by data values rather than column values?
It doesn’t have all of the features of the Dash DataTable, but it does have some things that the Dash table does not have yet, such as specifying which columns are sortable. More info here: http://tabulator.info/docs/4.8/sort#func