question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Column sorting feature breaks if the initial columns array doesn't have sort field

See original GitHub issue

Relates 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:

  1. Create a React component with BootstrapTable
  2. Initialize the the properties as follows
    keyField='id',
    columns=[{
        dataField: 'id',
        text: 'ID Column'
    }]
    
  3. And then update the properties: e.g.
    keyField='first',
    columns=[{
        dataField: 'first',
        text: 'First Column',
        sort: true
    },
    {
        dataField: 'second',
        text: 'Second Column',
        sort: true
    }]
    
  4. 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:closed
  • Created 4 years ago
  • Comments:12 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
daweifengcommented, Feb 13, 2020

@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 if condition in UNSAFE_componentWillReceiveProps.

Let’s say if the nextProps.columns.filter(col => col.sort).length > 0 is true and this.SortContext is not null, (nextProps.columns.filter(col => col.sort).length > 0 && !this.SortContext) will be false, which will cause this.SortContext set to null. In fact, this.SortContext should not be set to null because in the nextProps, we want our columns to be sorted. Setting this.SortContext will remove the sorting providers.

0reactions
seare-kidanecommented, Feb 21, 2020

https://react-bootstrap-table.github.io/react-bootstrap-table2/blog/2020/02/16/version-bump.html

try the newest version and let me know if the issue still remain, thanks!

@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!

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found