2013-08-20 54 views
1

我有一個問題,工作了三天,但無法找到任何解決方案! 在的document.ready,我創建與子對象無法在ken​​doGrid中添加記錄

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

問題是,當我嘗試添加新記錄時,它運行一個錯誤(運行時錯誤:類別未定義)。

有人可以告訴我,如果數據源是正確的? 代碼的結構是否正確? 問題在哪裏?

希望有人能幫助我!

回答

1

當您添加一個新的記錄,你應該設置在columns.field的默認值。 由於未聲明classe_iva變量,所以會引發錯誤。嘗試將其聲明爲全局。 希望這有助於。

+0

我有同樣的問題,但實際上我不明白什麼是classe_iva以及如何聲明它?有任何樣本可以幫助嗎? – Gabriel

2

問題是,當您創建新記錄時category未定義,並且未定義,因爲DataSource中沒有schema.model。在調用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" } 
      } 
     } 
    } 
}); 
+0

需要幫助!!! :) – pasluc74669

+0

但你有沒有嘗試我的答案?你有什麼問題? – OnaBai

+0

我發佈了上面的完整代碼,使用您的建議,但問題是一樣的,添加新記錄時數據源未定義。 – pasluc74669