我正在使用 Dojo 1.9 和 GridX 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/