排序实现不起作用。对于我已设置sortable的列:true,单击标题时我看到正在调用subscribe函数但没有任何反应。 dataRow1和dataRow2都有正确的信息,符号返回它应该返回的值,但网格渲染不起作用。任何帮助将不胜感激。
var data = getDataForGrid();
var dataView = new Slick.Data.DataView();
dataView.beginUpdate();
dataView.setItems(data, 'employeeID');
dataView.endUpdate();
var grid = new Slick.Grid("#myGrid", dataView, getColumnHeadersForGrid(), getOptions());
grid.onSort.subscribe(function(e,args){
var cols = args.sortCol;
dataView.sort(function (dataRow1, dataRow2) {
var field = cols[i].sortCol.field;
var value1 = null;
var value2 = null;
if(dataRow1 !=null){
value1 = dataRow1[sortedColumns.field];
}
if(dataRow2 !=null){
value2 = dataRow2[sortedColumns.field];
}
var sign = cols[i].sortAsc ? 1: -1;
var result = (value1 ==value2 ? 0 : (value1 > value2 ? 1: -1)) * sign;
if(result != 0) {
return result;
}
return 0;
});
grid.invalidate();
grid.render();
}
function getDataForGrid(){
var data = [];
//Have logic to get the data
return data;
}
function getColumnHeadersForGrid(){
var columns = [];
columns.push({id:"employeeID", name:"Employee ID", field:"employeeID", sortable: true, width: 200});
columns.push({id:"title", name:"Title", field:"title", sortable: true, width: 200});
columns.push({id:"duration", name:"Duration", field:"duration", sortable: true, width:150});
columns.push({id:"%", name:"% Complete", field:"percentComplete", sortable: true, width: 200});
return columns;
}
function
答案 0 :(得分:0)
var cols = args.sortCol;
这不是数组 - 这是getColumnHeadersForGrid()
sortedColumns
根本未在您的代码段中定义
试试这个:
grid.onSort.subscribe(function(e,args){
var field = args.sortCol.field;
var sign = args.sortAsc ? 1: -1;
dataView.sort(function (dataRow1, dataRow2) {
value1 = dataRow1[field];
value2 = dataRow2[field];
var result = (value1 ==value2 ? 0 : (value1 > value2 ? 1: -1)) * sign;
return result;
});
});