道场。 从JSON属性设置为HTML属性(Dojo. Set attribute from JS

2019-10-17 00:17发布

我有dojox.form.CheckedMultiSelect

<span dojoType="dojo.data.ItemFileReadStore" url="..." jsId="listStore"></span>
<select id="unsubscribedList" class="soria" dojoType="dojox.form.CheckedMultiSelect" 
multiple="true" onchange="..." store="listStore" title="title"></select>

JSON的店铺的样子:

{"items":[{"title":"ESC, MICHAEL (MESC)","value":"1000","label":"ESC, MICHAEL"},...}]
,"totalCount":7,"endList":7,"label":"label","identifier":"value","startList":1}

如何设置“ items.title ”从JSON作为HTML属性“标题”,以这将为创建的每个复选框属性CheckedMultiSelect

Answer 1:

尝试这个:

dojo.ready(function() {
    // 1.7.2 template has 'dojoxCheckedMultiSelectHidden' className on its select from template.
    // if this is different (inspect your DOM after onload), adapt the query selector
    var opts = dojo.query('.dojoxCheckedMultiSelectHidden option'),
        store = dijit.byId('listStore');
    store.fetch({ onComplete: function(items) {
       for(var i = 0; i < items.length; i++) {
          if(!opts[i]) continue; 
          else opts[i].title = store.getValue(items[i], 'title');
       }

    }});
});

它是什么,它

  1. 迫使存储从服务器加载它的数据,返回项目阵列( query: {id:'*'}
  2. 遍历他们,把标题上的选项。

相反,如果您选择通过标记部署你的选择,标题属性不想要这个小工具,只能作为配置参数是有效的,其被允许的标记映射。 换言之, 标题的属性将被丢弃 。 窗口小部件是相当差,一直没有更新,dojotoolkit的休息,所以它不是那么灵活。

当分析器运行在的标记,它忽略了title属性(您的标记被摧毁,由CheckedMultiSelect模板更换,见dojobase / DojoX中/表格/资源/ CheckedMultiSelect.html)。

所以,解决方案是 - 维护一个JS阵列,其中映射

// array with indexes matching the options from markup
var titles = [ "title1", "title2", "title3" ];

dojo.addOnLoad(function() {

  // note, that the '_0' is a generic ID, config options are not accepting the id attribute either
  // calling private function, again not correctly layed out
  var childObjects = dijit.byId('dojox_form_CheckedMultiSelect_0')._getChildren();

  dojo.forEach(childObjects, function(optObject, index) {
    optObject.labelNode.title = titles[index];
  });

});


文章来源: Dojo. Set attribute from JSon as HTML attribute