Column sorting feature breaks if the initial columns array doesn't have sort field
See original GitHub issueRelates to #1233
When using BootstrapTable, if you first initialize the columns array without sort property and later update it to a different columns array with sort property, sorting doesn’t work.
To Reproduce Steps to reproduce the behaviour:
- Create a React component with
BootstrapTable - Initialize the the properties as follows
keyField='id', columns=[{ dataField: 'id', text: 'ID Column' }] - And then update the properties: e.g.
keyField='first', columns=[{ dataField: 'first', text: 'First Column', sort: true }, { dataField: 'second', text: 'Second Column', sort: true }] - Try sorting either of the columns and you will see the a console error
header-cell.js:123 Uncaught TypeError: onSort is not a function at cellAttrs.onClick (header-cell.js:123) at HTMLUnknownElement.callCallback (react-dom.development.js:336) at Object.invokeGuardedCallbackDev (react-dom.development.js:385) at invokeGuardedCallback (react-dom.development.js:440) at invokeGuardedCallbackAndCatchFirstError (react-dom.development.js:454) at executeDispatch (react-dom.development.js:584) at executeDispatchesInOrder (react-dom.development.js:609) at executeDispatchesAndRelease (react-dom.development.js:713) at executeDispatchesAndReleaseTopLevel (react-dom.development.js:722) at forEachAccumulated (react-dom.development.js:694)
I suspect this happens due to the BootstrapTableContainer context initializing the SortContext state on the constructor rather than doing it on the render.
I’m referring specifically to BootstrapTableContainer class constructor
constructor(props) {
...
if (props.columns.filter(col => col.sort).length > 0) {
this.SortContext = createSortContext(
dataOperator, this.isRemoteSort, this.handleRemoteSortChange);
}
...
}
Suggestion
Since the SortContext is dependent on the columns array and the columns array property can be updated at any given time, I suggest we move the above sort context initialization to the render method.
Issue Analytics
- State:
- Created 4 years ago
- Comments:12 (6 by maintainers)
Top Results From Across the Web
PrimeNG table with grouped columns won't sort - Stack Overflow
Explanation: By providing the p-table with a value for the columns property, the field names found in each column become accessible.
Read more >Lightning dataTable sortedDirection not working
I'm using lightning dataTable component. But the sortedDirection only ever sorts one direction. The sortDirection param never updates ...
Read more >How to alphabetize in Excel: sort columns and rows A-Z or Z-A
How do you alphabetize in Excel? This tutorial shows a few quick ways to sort rows and columns alphabetically. It also explains how...
Read more >How does ORDER BY FIELD() in MySQL work internally
When MySQL cannot use index to retrieve data in sorted order, it creates a temporary table/resultset with all selected columns and some additional...
Read more >Sorting Columns Gantt Docs - DHTMLX Documentation
To apply a custom sorting function to the grid, call the sort method with the name of your custom function as the first...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

@AllenFang Hi Allen! I am experiencing a similar issue. After digging into the code, I believe there is a problem with https://github.com/react-bootstrap-table/react-bootstrap-table2/blob/master/packages/react-bootstrap-table2/src/contexts/index.js#L87-L93 It is the first
ifcondition inUNSAFE_componentWillReceiveProps.Let’s say if the
nextProps.columns.filter(col => col.sort).length > 0is true andthis.SortContextis notnull,(nextProps.columns.filter(col => col.sort).length > 0 && !this.SortContext)will befalse, which will causethis.SortContextset tonull. In fact,this.SortContextshould not be set tonullbecause in thenextProps, we want our columns to be sorted. Settingthis.SortContextwill remove the sorting providers.@AllenFang Sorry for the late response, I now got a chance to verify the fix and I’m able to confirm sorting works after columns array update!
And @daweifeng thank you for quickly fixing submitting a fix!