【问题标题】:Creating a column in KendoGrid that is not bound to datasource在未绑定到数据源的 KendoGrid 中创建列
【发布时间】:2013-01-10 20:25:06
【问题描述】:

我正在使用 Kendo UI 将数据源绑定到 Kendo Grid。我正在尝试找到在网格中创建 绑定到数据源的列的最佳方法。

目前,我正在数据源中创建一个字段,并在从远程端点读取数据后使用一些 javascript 和 jQuery 设置该列的值。

数据来源:

schema: {
    model: {
        fields: {
            blah: {},
            empty_column: {}
        }
    }
}

网格:

columns: {
    field: "empty_column",
    width: 100,
    title: "Empty"
}

Javascript:

datasource.data[item].set("empty_column", computed_value);

为简洁起见编辑了所有代码,一切正常。

我的问题:有没有办法为这个未连接到远程端点的新空列设置 defaultValue? 当我设置:

empty_column: {defaultValue: "None"}

网格仍然显示“未定义”,我认为是因为它在读取操作中没有接收到该字段的任何数据。有没有办法在网格中设置默认值?我还没有看到任何这样的例子......

【问题讨论】:

    标签: kendo-ui


    【解决方案1】:

    defaultValue 在您创建记录时应用,而不是在将要显示的时候应用。

    你可以这样做:

    选项 1:schema.fields.fieldName 中使用 parse,就是这样。

    model: {
        fields: {
            empty_column: {
                type        : "string",
                defaultValue: "none",
                parse       : function (data) {
                    if (!data.empty_columns) {
                        data.empty_column = "none";
                    }
                    return data;
                }
            },
            blah        : { type: "number" }
        }
    },
    

    选项 2: 使用 schema.parse 添加额外的字段值。

                schema: {
                    model: {
                        fields: {
                            empty_column: {type: "string", defaultValue: "none"},
                            blah        : { type: "number" }
                        }
                    },
                    parse: function (response) {
                        $.each(response, function (idx, item) {
                            if (!item.empty_column) {
                                item.empty_column = "none";
                            }
                        });
                        return response;
                    }
                }
    

    当然,(对于这两个选项)如果您确实知道 empty_column 中没有先前的值,您可以保存测试条件并只留下分配。

    【讨论】:

    • 谢谢,这正是我要找但找不到的!
    猜你喜欢
    • 2015-12-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2019-06-26
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多