Kendo UI Grid本地数据源列默认排序

jon*_*now 28 kendo-grid

尝试从本地数据源在我的kendo UI网格上设置默认排序列.我已经阅读了我应该提出的所有内容:

sort: { field: "price", dir: "desc" }
Run Code Online (Sandbox Code Playgroud)

到数据源.我试过这个但它仍然无法工作(参见下面示例的底部).

这是我的完整代码,我哪里错了?

$('#grid').kendoGrid({
                dataSource: [
                    {
                        date: "Feb 13 2014",
                        price: 5,
                    },
                    {
                        date: "Feb 15 2014",
                        price: 7,
                    },
                    {
                        date: "Feb 12 2014",
                        price: 6,
                    }
                ],
                height:500,
                sortable: true,
                pageable: false,
                columns: [
                    {
                        field: "date",
                        title: "Date"
                    },
                    {
                        field: "price",
                        title: "Price",
                    }
                ],
                sort: {field: "price", dir: "desc"}
            });
Run Code Online (Sandbox Code Playgroud)

git*_*tgo 48

您正在sort错误的位置定义该行.你把它作为网格属性之一,但它(如你所说)是数据源的属性之一.

把它作为datasource属性的子代:

$('#grid').kendoGrid({
    dataSource: {
        data: [{
            date: "Feb 13 2014",
            price: 5,
        }, {
            date: "Feb 15 2014",
            price: 7,
        }, {
            date: "Feb 12 2014",
            price: 6,
        }],
        sort: {
            field: "price",
            dir: "desc"
        }
    },
    height: 500,
    sortable: true,
    pageable: false,
    columns: [{
        field: "date",
        title: "Date"
    }, {
        field: "price",
        title: "Price",
    }],
});
Run Code Online (Sandbox Code Playgroud)

如果它仍然不起作用,我可以提供一个jsFiddle供您使用.


Ima*_*idi 5

如果您使用的是最终渲染到 Kendo UI 的 Telerik MVC Control

.DataSource(dataSource => dataSource
        .Ajax()
        .Sort(sort => sort.Add("City").Ascending()) // <-- initial sort expression
        .Read(read => read.Action("Index", "Home"))
    )
Run Code Online (Sandbox Code Playgroud)