我尝试使用dgrid(使用他们的示例)为网格布局实现一个类,但是在加载网格时出现此错误( subRows未定义)。我的代码看起来像这样:
define([
"dojo/_base/declare",
"dojo/_base/lang",
"dgrid/Grid",
"dgrid/Keyboard",
"dgrid/Selection"
], function(
declare,
lang,
Grid,
Keyboard,
Selection
){
return declare([Grid, Keyboard, Selection], {
columns: {
first: "First Name",
last: "Last Name",
age: "Age"
},
selectionMode: "single", // for Selection; only select a single row at a time
cellNavigation: false, // for Keyboard; allow only row-level keyboard navigation
constructor: function() {
var data = [
{ first: "Bob", last: "Barker", age: 89 },
{ first: "Vanna", last: "White", age: 55 },
{ first: "Pat", last: "Sajak", age: 65 }
];
this.renderArray(data);
}
});
});
并像这样调用/创建
app.gridLayout = new GridLayout().placeAt(document.body);
答案 0 :(得分:2)
将constructor
代码放入postCreate
方法:
return declare([Grid, Keyboard, Selection], {
columns: {
first: "First Name",
last: "Last Name",
age: "Age"
},
selectionMode: "single",
cellNavigation: false,
postCreate: function() {
var data = [
{ first: "Bob", last: "Barker", age: 89},
{ first: "Vanna", last: "White", age: 55},
{ first: "Pat", last: "Sajak", age: 65}
];
this.renderArray(data);
}
});
另请注意dgrid
不是 dijit 以最小化其依赖关系,因此它没有placeAt()
方法。你有两个选择:
将占位符的 DOM节点或id
string 提供给构造函数作为第二个参数:
var gridLayout = new GridLayout({}, "placeholder");
通过将dgrid
子类化为dijit/_WidgetBase
,使gridLayout.placeAt("placeholder")
成为 dijit :
declare([_WidgetBase, Grid, Keyboard, Selection], {});