我知道了。您必须单独保留对数据设置的引用,并对它们进行排序,而不是 args 参数中的那些。
所以在这里,借助这里的许多好的答案(通过dependentObservable 使用 Knockoutjs 更新 SlickGrid)和这个(https://github.com/mleibman/SlickGrid/blob/gh-pages/examples/example-multi-column-排序.html )。它基于多列排序:
var grid;
var gridData;
ko.bindingHandlers.slickGrid = {
init: function (element, valueAccessor) {
var settings = valueAccessor();
gridData = ko.toJS(settings.data); // instead of ko.utils.unwrapObservable(settings.data) since within my ObservableArray there are Observables.
var columns = ko.utils.unwrapObservable(settings.columns);
var options = ko.utils.unwrapObservable(settings.options) || {};
grid = new Slick.Grid(element, gridData, columns, options);
grid.onSort.subscribe(function (e, args) {
var cols = args.sortCols;
gridData.sort(function (dataRow1, dataRow2) {
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;
});
grid.invalidate();
});
},
update: function (element, valueAccessor, allBindingAccessor, viewModel) {
var settings = valueAccessor();
gridData = ko.toJS(settings.data); //just for subscription
grid.setData(gridData, false);
grid.resizeCanvas(); // NB Very important for when a scrollbar appears
grid.render();
}
};
它不是 100% 漂亮,但它确实有效。