0
votes

I have created a kendo grid and load its datasource using ajax call. The grid is loading correctly and its working fine. Now i want to sort the data in grid based upon only one column. The grid definition is as follows :

$("#grid").kendoGrid({
    scrollable: true,
    resizable:true,
    filterable: {
        extra:false,
        operators: {
            string:{ contains: "Contains"}
        }
    },
    dataSource: {
        transport: {
            read: {
                url: urlStr + dataPrm,
                dataType: "json",
                async:true
            },
            parameterMap: function(data, type) {
                if (type == "read") {
                    return {
                        $start_rows: data.skip,
                        $page_size: data.pageSize
                    }
                }
            }
        },
        pageSize: 20,
        serverPaging : true,
        schema : {
            model: {
                fields: {
                    a_id  : { type: "string" },  
                    a_percentage: { type: "number" },
                    emp_last_name: { type: "string" },
                    emp_first_name : { type: "string" }
                }
            },
            data: function(response) {
                return response.GridResult;
            },
            total: function(response) {
                $("#totalRows").text(response.total);
                return response.total;
            }
        },
    }
    pageable: {
        pageSizes: true,
    },
    columns: [
        { field: "a_id",width:"17%",title: "A ID",headerTemplate: '<span title="A ID" style="font-weight: 300; color:#333">A ID</span>',template:"<a class='link' style='cursor:pointer'>#=a_id#</a>" },
        { field: "emp_last_name",width:"15%",title: "Last Name",template:"<span>#=emp_last_name#</span>",headerTemplate: '<span title="Employee" style="font-weight: 300; color:#333">Last Name</span>'},
        { field: "emp_first_name",width:"15%",title: "First Name",template:"<span>#=emp_first_name#</span>",headerTemplate: '<span title="Employee" style="font-weight: 300; color:#333">First Name</span>'},
        { field: "a_percentage", title: "percentage value",width:"15%",template: "#=getPercentage(a,a_percentage)#" }
    ]
});

As you can see that I have created the grid using ajax call in it. when the grid loads, it must be in sorted form based upon the column named as "a_percentage". The value of "a_percentage" column is evaluated using a method named as "getPercentage". I have implemented the sorting logic in this grid but it restrict the user to click on the sortable column i.e. a_percentage. I don't want to sort the data by clicking on this column. whenever the grid loads, it must be sorted in descending order based upon the "a_percentage" column. please help me that how can i achieve this functionality.

1

1 Answers

3
votes

It is possible to configure the Grid's DataSource to sort the data initially:

http://docs.telerik.com/kendo-ui/api/javascript/data/datasource#configuration-sort

dataSource: {
    // ...
    sort: { field: "a_percentage", dir: "desc" }
    // ...
    serverPaging: true,
    serverSorting: true
}

Note that all data operations must be performed either on the client, or on the server. Since you are using server paging, also set serverSorting and serverFiltering to true

http://docs.telerik.com/kendo-ui/framework/datasource/overview#mixed-data-operations-mode