我正在使用dxDataGrid devextreme(jquery)。 我有一个主 - 细节网格,它显示每个国家的国家和城市名单。 当我添加一个国家的工作。但是当我点击添加按钮城市时,我在控制台中收到了一个我无法添加的错误。
我的问题在哪里?
$(function () {
$("#gridCountry").dxDataGrid({
dataSource: {
store: {
type: "array",
key: "ID",
data: countries
}
},
columns: [{
dataField: "CountryLatinName",
caption: "Country Name(Latin)"
}],
editing: {
mode: "form",
allowUpdating: true,
allowDeleting: true,
allowAdding: true
},
masterDetail: {
enabled: true,
template: function (container, options) {
var country = options.data;
container.addClass("internal-grid-container");
$("<div>").text(country.CountryLatinName).appendTo(container);
$("<div>")
.addClass("internal-grid")
.dxDataGrid({
editing: {
mode: "form",
allowUpdating: true,
allowDeleting: true,
allowAdding: true
},
columnAutoWidth: true,
columns: [{
dataField: "CityLatinName",
caption:"City Name(Latin)"
}, {
dataField: "GMT",
dataType:"number"
}, {
dataField:"Capital",
dataType: "boolean"
}],
dataSource: country.Cities
}).appendTo(container);
}
}
});
});
这是我的错误:Uncaught TypeError: Cannot read property '0' of undefined
,这是一系列国家/地区:
var countries = [{
"ID": 3,
"CountryLatinName": "Turkey",
"PhonePrefixes":"+90",
"Cities": [{
"ID": 30,
"CityLatinName": "Ankara",
"GMT": "+3:00",
"Capital": true
}, {
"ID": 31,
"CityLatinName": "Istanbul",
"GMT": "+3:00",
"Capital": false
}, {
"ID": 32,
"CityLatinName": "Bodrum",
"GMT": "+3:00",
"Capital": false
}, {
"ID": 33,
"CityLatinName": "Izmir",
"GMT": "+3:00",
"Capital": false
}]
}];
答案 0 :(得分:1)
创建新国家/地区时,Cities
数组丢失。好吧,你可以使用rowInserting事件修复它:
onRowInserting: function(e) {
e.data.Cities = [];
}
示例是here。