Calling sort on slickgrid

后端 未结 10 897
我寻月下人不归
我寻月下人不归 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条回答
  •  梦毁少年i
    2021-02-04 18:55

    I was inspired by Mr.Hunts answer but I took a slightly different approach to extend the current grid.setSortColumn(columnId, isAsc) to grid.setInitialSortColumn(columnId, isAsc). This will apply the sort and do everything grid.setSortColumn does.

        var thisGrid = { //Your grid obj
            columns: , // Your columns object
            grid: , // new Slick.Grid....
        }
    
    
    
        thisGrid.grid.onSort.subscribe(function (e, args) { // ar  var cols = args.sortCols;]
                thisGrid.grid.customSort(args);
            });
    
        thisGrid.grid.customSort = function (args) {
            var cols = args.sortCols;
            thisGrid.dataView.sort(function (dataRow1, dataRow2) {
                    if (cols) {
                        for (var i = 0, l = cols.length; i < l; i++) {
                            var field = cols[i].sortCol.field;
                            var sign = cols[i].sortAsc ? 1 : -1;
                            var value1 = dataRow1[field],
                                value2 = dataRow2[field];
                            var result = (value1 == value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
                            if (result != 0) {
                                return result;
                            }
                        }
                    }
                    return 0;
                });
        }
        thisGrid.grid.setInitialSortColumn = function (columnId, ascending) {
            thisGrid.grid.setInitialSortColumns([{
                        columnId: columnId,
                        sortAsc: ascending
                    }
                ]);
        };
        thisGrid.grid.setInitialSortColumns = function (cols) {
            sortColumns = cols;
            $.each(sortColumns, function (i, col) {
                    var columnIndex = thisGrid.grid.getColumnIndex(col.columnId);
                    var column = thisGrid.columns[columnIndex];
                    if (col.sortAsc == null) {
                        col.sortAsc = true;
                    }
                    var args = {
                        grid: thisGrid.grid,
                        multiColumnSort: true,
                        sortCols: [{
                                sortCol: column,
                                sortAsc: col.sortAsc
                            }
                        ]
                    }
                    thisGrid.grid.setSortColumn(col.columnId, col.sortAsc);
                    thisGrid.grid.customSort(args);
    
                });
        };
    
        // Trigger 
        thisGrid.grid.setInitialSortColumn("dateDue", true);
    

提交回复
热议问题