Kendo ui 无法在kendoGrid中添加记录

Kendo ui 无法在kendoGrid中添加记录,kendo-ui,kendo-grid,kendo-combobox,Kendo Ui,Kendo Grid,Kendo Combobox,我有一个问题,工作了三天,但找不到任何解决办法!!! 在document.ready中,我创建了一个包含子对象的数据源 var datasource = [ { "category": { "id_category": 1, "desc_category": "Beverages" }, "id_product": 1, "desc_product": "Chai", "price": "11", "id_categ

我有一个问题,工作了三天,但找不到任何解决办法!!! 在document.ready中,我创建了一个包含子对象的数据源

var datasource = [
  {
   "category": {
      "id_category": 1,
      "desc_category": "Beverages"
  },
   "id_product": 1,
      "desc_product": "Chai",
      "price": "11",
      "id_category": 1
  },
  {
   "category": {
      "id_category": 2,
      "desc_category": "Condiments"
  },
    "id_product": 2,
      "desc_product": "Aniseed Syrup",
      "price": "12",
      "id_category": 2
 }
]
然后我创造了一个肯多格里德

var kendoGrid = $("#grid").kendoGrid({

    selectable: true,
    dataSource: datasource,
    resizable: true,
    toolbar: [{
        name: "create",
        text: "Add Something"
    }],
    columns: [
        { field: "desc_product", title: "Description", width: 100 },
        { field: "price", title: "Price", width: 100 },
        { field: "id_product", title: "Category", width: 200, editor: categoryDropDownEditor, template: '#=category.desc_category#' },
        {
            command: [{
                name: "destroy",
                text: "Delete",
                confirmation: "Are you sure?"
            },
            {
                name: "edit",
                text: {
                edit: "Edit",
                update: "Update",
                cancel: "Cancel"
            }
            }
            ]
        }
    ],
    width: 200,
    height: 300,
    editable: editable = {
        mode: "inline",
        confirmation: "Are you sure?"
    }
});
最后是在编辑模式下填充网格中组合框的函数

function categoryDropDownEditor(container, options) {
var ds = [
{         
      "id_category": 1,
      "desc_category": "Beverages"
},
{
   "id_category": 2,
   "desc_category": "Condiments"
},
{
   "id_category": 3,
   "desc_category": "Confections"
},
{
   "id_category": 4,
   "desc_category": "Produce"
},
{
   "id_category": 5,
   "desc_category": "Sea Food"
}
];

$('<input data-text-field="desc_category" data-value-field="id_category" data-bind="value:' + options.field + '"/>"')
  .appendTo(container)
  .kendoComboBox({
      index: 0,
      placeholder: "Select Category",
      dataTextField: "desc_category",
      dataValueField: "id_category",
      dataSource: ds
  })
}   
function categorhydropdowneditor(容器、选项){
变量ds=[
{         
“id_类别”:1,
“描述类别”:“饮料”
},
{
“id_类别”:2,
“描述类别”:“调味品”
},
{
“id_类别”:3,
“描述类别”:“糖果”
},
{
“id_类别”:4,
“描述类别”:“生产”
},
{
“id_类别”:5,
“描述类别”:“海洋食品”
}
];
$('"')
.appendTo(容器)
kendoComboBox先生({
索引:0,
占位符:“选择类别”,
dataTextField:“描述类别”,
dataValueField:“id_类别”,
数据源:ds
})
}   
问题是,当我尝试添加新记录时,它会运行一个错误(运行时错误:类别未定义)

有人能告诉我数据源是否正确吗? 代码的结构正确吗? 问题在哪里


希望有人能帮助我!!!

问题是,当您创建一个新记录时,
类别
没有定义,也没有定义,因为您的
数据源
中没有
模式。模型
。在调用
类别Hydropdowneditor
函数之前,它实际上实例化了
模板
,并且产生错误

修复
模板中的错误非常容易,您只需执行以下操作:

template: '#= data.category ? data.category.desc_category : 1 #'
检查当前行的
数据
是否定义了
类别

但这将导致下一个问题,即无法关闭
弹出窗口
,因为没有
schema.model
定义

您可以尝试将
数据源定义为:

var datasource = new kendo.data.DataSource({
    data  : [
        {
            "category"    : {
                "id_category"  : 1,
                "desc_category": "Beverages"
            },
            "id_product"  : 1,
            "desc_product": "Chai",
            "price"       : "11",
            "id_category" : 1
        },
        {
            "category"    : {
                "id_category"  : 2,
                "desc_category": "Condiments"
            },
            "id_product"  : 2,
            "desc_product": "Aniseed Syrup",
            "price"       : "12",
            "id_category" : 2
        }
    ],
    schema: {
        model: {
            id    : "id_category",
            fields: {
                desc_product: { type: "string" },
                price       : { type: "number" }
            }
        }
    }
});

问题是,当您创建一个新记录时,
category
没有定义,也没有定义,因为您的
数据源中没有
schema.model
。在调用
categorhydropodowneditor
函数之前,它实际上实例化了
模板
,并产生了错误

修复
模板中的错误非常容易,您只需执行以下操作:

template: '#= data.category ? data.category.desc_category : 1 #'
检查当前行的
数据
是否定义了
类别

但这将导致下一个问题,即无法关闭
弹出窗口
,因为没有
schema.model
定义

您可以尝试将
数据源定义为:

var datasource = new kendo.data.DataSource({
    data  : [
        {
            "category"    : {
                "id_category"  : 1,
                "desc_category": "Beverages"
            },
            "id_product"  : 1,
            "desc_product": "Chai",
            "price"       : "11",
            "id_category" : 1
        },
        {
            "category"    : {
                "id_category"  : 2,
                "desc_category": "Condiments"
            },
            "id_product"  : 2,
            "desc_product": "Aniseed Syrup",
            "price"       : "12",
            "id_category" : 2
        }
    ],
    schema: {
        model: {
            id    : "id_category",
            fields: {
                desc_product: { type: "string" },
                price       : { type: "number" }
            }
        }
    }
});

添加新记录时,应在columns.field中设置默认值。 由于未声明classe_iva变量,因此引发错误。请尝试将其声明为全局变量。
希望这有帮助。

添加新记录时,应在columns.field中设置默认值。 由于未声明classe_iva变量,因此引发错误。请尝试将其声明为全局变量。
希望这有帮助。

但是你试过我的答案吗?你有什么问题?我用你的建议发布了上面的完整代码,但问题是相同的,添加新记录时数据源未定义。但是你试过我的答案吗?你有什么问题?我用你的建议发布了上面的完整代码,但问题是相同的,数据源添加新记录时未定义。我有同样的问题,但实际上我不明白什么是classe_iva以及如何声明它?是否有任何示例可帮助?我有同样的问题,但实际上我不明白classe_iva是什么以及如何声明它?是否有任何示例可帮助?