道场。将JSon中的属性设置为HTML属性

时间:2012-07-16 10:54:55

标签: javascript html dojo

我有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}

如何将JSon中的“items.title”属性设置为HTML属性“title”到为CheckedMultiSelect创建的每个复选框?

1 个答案:

答案 0 :(得分: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. 强制商店从服务器加载数据,返回items数组(query: {id:'*'}
  2. 迭代它们,将标题放在选项上。
  3. 如果您通过标记部署选项,则标题属性不会与此窗口小部件一起映射,只有作为配置参数允许的标记才有效。换句话说,标题属性被丢弃。小部件很差,并且没有使用dojotoolkit的其余部分进行更新,所以它不是那么灵活。

    当解析器在标记上运行时,它会忽略title属性(标记被销毁并被CheckedMultiSelect模板替换,请参阅 dojobase / dojox / form / resources / 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];
      });
    
    });