运行dojo dgrid:子行是未定义的

时间:2012-06-18 20:04:47

标签: dojo dgrid

我尝试使用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);

1 个答案:

答案 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()方法。你有两个选择:

  1. 将占位符的 DOM节点id string 提供给构造函数作为第二个参数:

    var gridLayout = new GridLayout({}, "placeholder");
    
  2. 通过将dgrid子类化为dijit/_WidgetBase,使gridLayout.placeAt("placeholder")成为 dijit

    declare([_WidgetBase, Grid, Keyboard, Selection], {});