Calling sort on slickgrid

后端 未结 10 892
我寻月下人不归
我寻月下人不归 2021-02-04 18:40

In the slickgrid I\'m able to set the sort column and it\'s sort direction using the grid.SetSortColumn(colName,true/false). This only sets the sorting glyph but do

相关标签:
10条回答
  • 2021-02-04 19:01

    A more clean solution is not to rely on the arguments to onSort but call getSortColumns instead:

      function gridSorter() {
         var scol=grid.getSortColumns();
         if (scol.length===0) return;
         var scolId=scol[0].columnId, asc=scol[0].sortAsc;
         data.sort(function(a, b) {
            var result = a[scolId] > b[scolId] ? 1 : a[scolId] < b[scolId] ? -1 : 0;
            return asc ? result : -result;
         });
         grid.invalidate();
      }
    

    Then do:

      grid.onSort.subscribe(gridSorter);
    

    This will allow to reestablish sorting anytime you want (from example after reloading the data with ajax) simply by calling gridSorter()

    0 讨论(0)
  • 2021-02-04 19:03

    I'm using multicolumn sorting, and loading saved sort data when initialising the grid.

    As expected, setSortColumns set the sorting, but didnt actually apply it, and dataView.reSort() or .fastSort() didnt seem to help, regardless of what point in loading I called them (I must have missed something, but just couldnt get it to work).

    In the end, this worked for me. I call it immediately after populating my dataView from an ajax call. Its probably not the slickest, so happy to take feedback on board!

    function forceResort() {
    
        var sortColumns = grid.getSortColumns();
        var cols = [];
        $.each(sortColumns, function(index, value) {
            var columnId = value.columnId;
            var sortAsc = value.sortAsc;
            var sortCol = { field: columnId };
            var col = { sortCol: sortCol, sortAsc : sortAsc};
            cols.push(col);
        });
    
        dataView.sort(function (dataRow1, dataRow2) {
    
            var sortResult = 0;
            for (var i = 0, l = cols.length; i < l; i++) {
                if (sortResult !== 0) {
                    break;
                }
    
                var field = cols[i].sortCol.field;
                var sign = cols[i].sortAsc ? 1 : -1;
                var value1 = dataRow1[field] || ''; //handle nulls - otherwise erratic sorting
                var value2 = dataRow2[field] || ''; //handle nulls - otherwise erratic sorting
    
                if ($.inArray(field, dateTypeColumns) > -1) {
                    sortResult = compareDates(value1, value2) * sign;
                } else {
                    if ($.inArray(field, numericColumns) > -1) {
                        sortResult = compareSimple(value1, value2) * sign;
                    } else {
                        sortResult = compareAlphaNumeric(value1, value2) * sign;
                    }
                }
            }
            return sortResult;
        });
    
        grid.invalidate();
        grid.render();
    }
    
    0 讨论(0)
  • 2021-02-04 19:06

    Maybe it will help you. Looks like SlickGrid is triggering sort to self - so You can trigger it manually if You want.

    0 讨论(0)
  • 2021-02-04 19:08

    You can trigger click event on the column header...which does sorting

    I fixed the issue like this...

    $('.slick-header-columns').children().eq(0).trigger('click'); // for first column
    
    0 讨论(0)
提交回复
热议问题