Kendo UI,网格“工具栏”与Typescript

时间:2014-01-21 19:44:27

标签: kendo-ui typescript kendo-grid

我正在将Kendo UI的Grid系统用于数据网格,并将我的代码迁移到Typescript。到目前为止我很喜欢它,但是遇到了一些障碍;其中之一就是,即使使用Telerik的官方打字稿定义,网格声明似乎也有点不完整。

例如,此代码适用于javascript。但是,如果我在Typescript中运行这个完全相同的代码,我被告知toolbar属性无效,因为它期望类型为GridToolbarItem。我已经跟踪了GridToolbarItem,虽然我找到了它的界面,但我似乎无法实际声明或创建它,因此我不被允许为我的网格创建工具栏!

        elements.grid = $('#grid').kendoGrid({
            dataSource: {
                transport: {
                    read: {
                        url: "/administrator/data/items",
                        dataType: "json",
                        type: 'GET',
                        cache: false
                    }
                },
                schema: {
                    total: "total",
                    data: "data"
                },
                page: 0,
                pageSize: 15,
                take: 15,
                serverPaging: true,
                serverFiltering: true,
                type: "aspnetmvc-ajax"
            },
            toolbar: kendo.template($("#search-byName").html()),
            pageable: {
                refresh: true,
                pageSizes: true
            },
            selectable: "row",
            columns: [
                {
                    field: "Id",
                    width: 25,
                    title: "Identity"
                },
                {
                    field: "Name",
                    width: 40,
                    title: "Name",
                    template: "<div class='#: Quality.CSS #'>#: Name #</div><div>#: (Origin.Label != null) ? Origin.Label : '' #</div>"
                }
            ],
            change: function (e) {
                // do some stuff when an item is selected
            },
        }).data("kendoGrid");

1 个答案:

答案 0 :(得分:1)

TypeScript的类型系统与Java或C#的类型不同,因为它是重要类型的形状,而不是类层次结构中的名称。我喜欢将它视为合同。合同说你必须拥有某些东西,为什么你拥有它们或者你有更多东西并不重要。

这是我做的一个蹩脚的例子:

interface BagOfStuff {
    anum: number;
    astring: string;
    anany: any;
}

function processBag(bag: BagOfStuff): void { }

var aBag = {
    anum: 5,
    astring: "foo",
    anany: {},
    bonusThing: "bar"
};
processBag(aBag);   // works

aBag从未说它属于BagOfStuff类型,但它满足了所有要求,因此我可以将其传递给processBag,这样就可以了。