【问题标题】:How to allow kendo grid to bind to an undefined field如何允许剑道网格绑定到未定义的字段
【发布时间】:2015-02-02 15:17:47
【问题描述】:

我有这个 json 对象

{
  id: string,
  name: string,
  category: {
    id: string
    name: string,         
  }
}

我想要绑定到 productCategory.name 的列。但是,该字段可以为空。当 productCategory 为 null/undefined 时,kendo 将抛出错误。我如何告诉 kendo 如果字段未定义,只显示空字符串?

编辑

以下是我的示例数据

[{
   "id":1,
   "name":"Spaghetti",
   "category":{
      "id":"1",
      "name":"Food"
}},
{
   "id":2,
   "name":"Coca-cola",
   "category":null
}}]

下面是我的剑道数据源

var kendoDataSource = new kendo.data.DataSource({
        schema: {
            data: "data",
            total: "total",
            model: {
                id: "id",
                fields: {
                    id: { type: "string" },
                    name: { type: "string" },
                    "category.name": { type: "string" }                
                }

            }
        }
    });

上面的数据会抛出“未定义”错误,因为第二个产品没有类别。

【问题讨论】:

  • 能否提供您的代码?
  • 你有一个列来显示一个对象?

标签: kendo-ui kendo-grid


【解决方案1】:

尝试使用空对象而不是 null

创建了一个小提琴,检查一下:

http://jsfiddle.net/Sowjanya51/h930jcru/

【讨论】:

    【解决方案2】:

    只需在您的模型中提供一个默认值,如下所示:

    var kendoDataSource = new kendo.data.DataSource({
            schema: {
                data: "data",
                total: "total",
                model: {
                    id: "id",
                    fields: {
                        id: { type: "string" },
                        name: { type: "string" },
                        "category.name": { type: "string" },
                        category: { defaultValue: {
                                                    id: "",
                                                    name: "",         
                                                  }
                                  }
                    }
    
                }
            }
        });
    

    如果 productCategory 为 null 或未定义,这将初始化它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多