olifolkerd / tabulator

Interactive Tables and Data Grids for JavaScript
http://tabulator.info
MIT License
6.78k stars 821 forks source link

Tabulator loses sort state with auto columns after page change #4468

Closed HenriqueSabino closed 7 months ago

HenriqueSabino commented 7 months ago

Describe the bug When the tabulator is configured with auto columns, after changing the page, the sort caret no longer represents the current sorting state of the table (running tabulator.getSorters() returns correct column).

Tabulator Info

Working Example

The JsFiddle reproducing the issue https://jsfiddle.net/v02qyorf/

To Reproduce A step by step guide to recreate the issue in your JS Fiddle or Codepen:

  1. Go to the JsFiddle provided
  2. Click on id column twice so it sorts by descending order
  3. Change to page two
  4. Id column sort caret is reset to default state

Expected behavior The sort caret of the column should stay the same because the new column loaded has same field name or alternatively there should be a way to set the column sort direction programatically

Screenshots Initial state image Sorted column image After page change image

Desktop (please complete the following information):

olifolkerd commented 7 months ago

You can't use auto columns with remote paginated data, it will reset the column layout on every page load breaking the sort

HenriqueSabino commented 7 months ago

Ok. I got a work around for anyone in need

table.on('renderComplete', () => 
{
    let columns = table.getColumns();
    for (let column of columns) {

    var sorters = table.getSorters();
    const columnField = column.getField();
    var sortingColumn = sorters.find(x => x.field === columnField);

    console.log(sortingColumn)

    if (sortingColumn) {
        const dir = sortingColumn.dir === 'asc' ? 'ascending' : 'descending' ;
      column.getElement().setAttribute('aria-sort', dir)
    }
  }
})