我想将检索到的数据排序到描述字段,但排序不起作用。
UI代码:正确显示数据
var gridDataSource = new kendo.data.DataSource({
autoSync: true,
data: transformation.Activities,
schema: {
model: {
id: "TransformerActivityUID",
fields: {
//It has different field. for instance one is
TargetTable: { editable: false, sortable: true },
}
}
},
sort: { field: "Description", dir: "desc" },
group: { field: "TargetTable" }
});
CreateGrid("functionTable", new BasicGrid(gridDataSource, columns, ActivityChanged));
ChangesDetection(ToggleSave);
AutoResizeModal("95%");
var grid = GetGridData("functionTable");
$("#functionsList").kendoDropTarget({
group: "gridGroup",
drop: AddActivity
});
grid.table.kendoDropTarget({
group: "gridGroup",
drop: AddActivity
});
当我检索数据时,我希望按描述字段
对其进行排序functionTableGrid = GetGridData("functionTable");
gridSource = functionTableGrid.dataSource;
gridData = functionTableGrid.dataSource.data();
var dsSort = [];
dsSort.push({ field: "Description", dir: "desc" });
var testData = gridSource.sort(dsSort);
var sortedData= testdata.data();
//I have tried this
gridData.dataSource.sort(dsSort) //not working
gridSource.sort(dsSort); // not working
重要的是,我在UI中显示的数据与此处相同。 我尝试了不同的东西,但我不确定它是如何工作的。我对JavaScript很陌生,所以任何帮助都会很棒。
答案 0 :(得分:2)
尝试通过调用gridData.toJson()
将数据数组(在您的情况下为gridData)转换为json数组。并尝试类似的事情:
gridData = [{name: "tester 03", param2: "test3"},
{name: "tester 01", param2: "test1"},
{name: "tester 02", param2: "test2"}]; //Assuming some test values here
var sortedData = a.sort(function(e,f) {
return e.name < f.name; //by name or description or whatever
});
如果这个dir是关于排序的方向
,请将其转换为函数function sortData(arr, sorter) {
return arr.sort(function(e,f) {
return sorter.dir == "desc" ?
e[sorter.field] < f[sorter.field] : e[sorter.field] > f[sorter.field];
});
}
并传递以下值:sortData(gridData, {field: "Description", dir: "desc"})
希望它有效。