无法在ken​​doGrid中添加记录

时间:2013-08-20 18:40:44

标签: 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_category": 1
  },
  {
   "category": {
      "id_category": 2,
      "desc_category": "Condiments"
  },
    "id_product": 2,
      "desc_product": "Aniseed Syrup",
      "price": "12",
      "id_category": 2
 }
]

然后我创建了一个kendogrid

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
  })
}   

问题是,当我尝试添加新记录时,它会运行一个错误(运行时错误:类别未定义)。

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

希望有人可以帮助我!!!

2 个答案:

答案 0 :(得分:2)

问题在于,当您创建新记录时category未定义且未定义,因为您的schema.model中没有DataSource。在调用categoryDropDownEditor函数之前,它实际上会实例化template并产生错误。

修复template中的错误非常简单,您只需要执行以下操作:

template: '#= data.category ? data.category.desc_category : 1 #'

检查当前行确定的data是否已定义category

但是这会带您进入下一个问题,即您无法关闭popup,因为没有schema.model定义。

您可以尝试将DataSource定义为:

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" }
            }
        }
    }
});

答案 1 :(得分:1)

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