如何在可编辑的GridX中将局部变量作为属性提供给单元格编辑器?

时间:2013-07-09 12:10:17

标签: javascript dojo dojo.gridx

我在 GridX 1.2 中使用 Dojo 1.9 。我只是将ComboBox配置为网格中单元格的编辑器。

我在示例中找到了以下配置语法:

    editor: "dijit/form/ComboBox",
    editorArgs: {
        props: 'store: myStore, searchAttr: "label"'
    }},

问题是,props必须是要解析的文本。它不接受该对象。这意味着,我必须将myStore作为全局变量,这是我想要避免的。

是否有另一种在GridX中配置编辑器的方法?

1 个答案:

答案 0 :(得分:1)

QUICK FIX: 而不是将其创建为全局变量,将其添加到命名空间,该命名空间广泛用于这些情况。因此,在创建商店时,将其添加到特定名称空间并在props中使用它。

var ns = {}; 
//use this namespace for all objects subject to grid/a particular section/anything
ns.myStore = new Memory({
    data: [
        {name:"Option 1", id:"OP1"},
        {name:"Option 2", id:"OP2"},
        {name:"Option 3Samoa", id:"OP3"}
    ]
});

props: 'store: ns.myStore, searchAttr: "label"'

因此,我们可以避免将全局对象直接添加到窗口对象。

推荐修正: 在为该列传递的模板字符串中,使用自定义小部件而不是使用默认组合框。

在此窗口小部件中,覆盖postCreate方法并设置所需的商店。

define([ 
    "dojo/_base/lang",
    "dijit/form/ComboBox",
    "dojo/store/Memory"
], function(lang, comboBox, Memory) {
    return dojo.declare("myapp.widget.MyComboBox", [ dijit.form.ComboBox], {
    // summary:
    //Custom sub-class of ComboBox with following functionality:
    //This will set the desired store

    postCreate: function(){
        // summary:
        // This will call default postCreate and additionally create/set store:

        this.inherited(arguments);
        var wid = this;
        //if store is static get storeObj from a json file
        //if store comes from backend, make the call here and get storeObj
        dojo.xhrGet({
            url: "filenameOrUrl",
            handleAs: "json"
        }).then(function(result){
            var storeObj = new Memory(result);
            wid.set("store",storeObj);
        });
    }
  });
});

现在你在该列的模板中。 请注意,我们无需在此处将商店视为字符串或对象,因为小部件本身会在创建后加载商店。

<div data-dojo-type="myapp/widget/MyComboBox" 
    style="width: 100%"
    data-dojo-attach-point="gridCellEditField"
></div>