Calling sort on slickgrid

百般思念 提交于 2019-12-20 10:48:31

问题


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 does no sorting. Is there a way to call the sort event handler. I've defined the sort handler like grid.onSort.subscribe(function(){});


回答1:


The behavior you are observing is correct.

    grid.setSortColumn(columnId, isAsc);

only updates the glyph on the sort column. In your case, you will initially need to sort the data, and then use setSortColumn to update the glyph on sortColumn. You can reuse sorter used in onSort event like this:

   var gridSorter = function(columnField, isAsc, grid, gridData) {
       var sign = isAsc ? 1 : -1;
       var field = columnField
       gridData.sort(function (dataRow1, dataRow2) {
              var value1 = dataRow1[field], value2 = dataRow2[field];
              var result = (value1 == value2) ?  0 :
                         ((value1 > value2 ? 1 : -1)) * sign;
              return result;
       });
       grid.invalidate();
       grid.render();
   }
   var grid = new Slick.Grid($gridContainer, gridData, gridColumns, gridOptions);

   //These 2 lines will sort you data & update glyph while loading grid     
   //columnField is field of column you want to sort initially, isAsc - true/false
   gridSorter(columnField, isAsc, grid, gridData);

   //I had the columnField, columnId same else used columnId below
   grid.setSortColumn(columnField, isAsc); 

   grid.onSort.subscribe(function(e, args) {
        gridSorter(args.sortCol.field, args.sortAsc, grid, gridData);
   });

How I arrived on this solution?

Read comments here. https://github.com/mleibman/SlickGrid/issues/325




回答2:


dataView.fastSort does the job. You can then use setSortColumn to set the sorting glyph.




回答3:


I have multiple column sort enabled, I had to change the function to pass the correct sort column.

grid.onSort.subscribe(function(e, args) {
    gridSorter(**args.sortCols[0].sortCol.field**, **args.sortCols[0].sortAsc**, grid, gridData);
});



回答4:


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



回答5:


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);



回答6:


I can't leave comments due to reputation, which is where this would be most appropriate, however, my answer is in regard to @Premshankar Tiwari and @Siddharth answers.

I preferred the dataView.fastSort option in Siddharth's answer, which works for me in all browsers except IE7 and 8. I didn't test it in IE9 or above. Unfortunately, most on my network run IE7 or 8 due to compatibility issues for legacy applications. BUT, Premshankar's answer works in IE7 and 8.

So, I ended up doing something like this:

if (msie > 0) {
    $(".slick-header-columns").children().eq(5).trigger("click");
    $(".slick-header-columns").children().eq(4).trigger("click");
} else {
    dataView.fastSort('process','unit');
}

where column index (5) = 'unit' and column index (4) = 'process'. Notice that is the reverse order in dataView.fastSort method. I am also using a function that detects IE browser version and assigns it to msie.

My only complaint about utilizing the .trigger method is that if you set up your grid to dynamically hide/show columns, the indexed feature would potentially sort on unintended columns unless you are only calling it on initialization when hide/show capabilities are present.




回答7:


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




回答8:


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();
}



回答9:


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()




回答10:


If you want multiple column sorting:

function grid_sorter(args, grid, dataView) {
    let cols = args.sortCols;
    console.log(cols)
    dataView.sort(function (dataRow1, dataRow2) {
        for (let i = 0, l = cols.length; i < l; i++) {
            let field = cols[i].sortCol.field;
            let sign = cols[i].sortAsc ? 1 : -1;
            let value1 = dataRow1[field], value2 = dataRow2[field];
            let result = (value1 === value2 ? 0 : (value1 > value2 ? 1 : -1)) * sign;
            if (result !== 0) {
                return result;
            }
        }
        return 0;
    });
    grid.invalidate();
    grid.render();
}

grid_sorter(default_sorting, grid_2, dataView_2);

cols is an object like this:

- sortCols {
  - length: 2
  - 0 : {
       "sortAsc: true,
       "sortCol": {
           "field: column_id
        }
      }
  - 1: {..}
}


来源:https://stackoverflow.com/questions/9678309/calling-sort-on-slickgrid

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!