简体   繁体   English

在Kendo UI Grid中对Grid数据进行排序

[英]Sorting Grid data in kendo UI Grid

I want to sort the retrieved data wrt to description field but sorting is not working on it. 我想将检索到的数据排序到描述字段,但是无法对其进行排序。

UI Code: Displays the data correctly 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
});

When I retrieve the data I want it to be sorted by description field 当我检索数据时,我希望它按描述字段进行排序

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

Its important that I have the same data here as it is shown in UI. 重要的是,我在此处拥有与UI中显示的数据相同的数据。 I have tried different things but I am not sure how it will work. 我尝试了不同的方法,但不确定如何工作。 I am quite new to JavaScript so any help would be great. 我对JavaScript还是很陌生,所以任何帮助都会很棒。

Try to convert your data array (gridData in your case) to json array by calling gridData.toJson() to it. 尝试通过调用gridData.toJson()将数据数组(在您的情况下为gridData)转换为json数组。 and try something like: 并尝试类似:

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

Or convert it to a function if this dir thing is about the direction of sort 或将其转换为函数,如果此目录与排序方向有关

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

And pass the values like: sortData(gridData, {field: "Description", dir: "desc"}) 并传递以下值: sortData(gridData, {field: "Description", dir: "desc"})

Hope it works. 希望它能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM