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

标签 javascript dojo dojo.gridx

我正在使用 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>

关于javascript - 如何在可编辑的 GridX 中将局部变量作为属性提供给单元格编辑器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17547855/

相关文章:

javascript - 单选按钮在 css Accordion 中不起作用

javascript - 如何将小部件添加到 Dojo gridx/Grid header ?

dojo - 从 dojo.gridx 获取行内容它总是显示最初加载的旧数据

css - 如何让我的 css 为我的 dojo dijit 表单按钮正常工作?

javascript - 如何监听 react.js 中的状态变化?

javascript - 向鼠标单击时鼠标悬停的所有元素添加类

dojo - 如何从 Dijit.Editor 获取值?

javascript - dojo 组合框弹出菜单宽度太大

javascript - TypeScript - 如何使用数组映射到 ES 6 映射?

javascript - 下拉项目上的点击事件 - jquery