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

How to give local variables as properties to cell editor in editable GridX?

本文关键字:属性 编辑器 单元格 局部变量 编辑 GridX      更新时间:2023-09-26

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

我在示例中发现了以下配置语法:

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

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

有没有其他方法可以在GridX中配置编辑器?

快速修复:与其将其创建为全局变量,不如将其添加到命名空间中,命名空间在这些情况下被广泛使用。因此,在创建存储时,将其添加到特定的命名空间中,并在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>