Skip to content

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

@seare-kidane

Description

@seare-kidane

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.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions