问题
I have 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>
The JSon for store looks like:
{"items":[{"title":"ESC, MICHAEL (MESC)","value":"1000","label":"ESC, MICHAEL"},...}]
,"totalCount":7,"endList":7,"label":"label","identifier":"value","startList":1}
How I can set "items.title
" attribute from JSon as HTML attribute "title" to each checkbox which will created for CheckedMultiSelect
?
回答1:
Try this:
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');
}
}});
});
What it does is, that it
- forces the store to load its data from server, returning the items array (
query: {id:'*'}
) - iterates them, placing titles onto the options.
If instead youre deploying your options via markup, the title attributes are not mapped with this widget, only markup which is allowed as configure parameters are valid. In other words, title-attributes are discarded. The widget is quite poor and has not updated with the rest of dojotoolkit, so it is not that flexible.
When parser runs over the markup, it ignores the title attribute (your markup gets destroyed and replaced by the CheckedMultiSelect template, see dojobase/dojox/form/resources/CheckedMultiSelect.html).
So, solution is - maintain a JS array, mapped with
// 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];
});
});
来源:https://stackoverflow.com/questions/11502836/dojo-set-attribute-from-json-as-html-attribute