مسعود
مسعود

Reputation: 689

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

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. 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.

Upvotes: 1

Views: 1153

Answers (1)

Hassan
Hassan

Reputation: 930

Try to convert your data array (gridData in your case) to json array by calling gridData.toJson() to it. 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"})

Hope it works.

Upvotes: 2

Related Questions