2008年11月24日星期一

Re: [fw-mvc] How do I Dojo.query to disable dojo form elements?

Jim Scherer wrote:
> I disable my form elements (in my Zend_Controller_Action) and require my
> users to press an edit button to be able to modify the data. I do this
> simply with the following code:
>
> foreach ($this->view->form->getElements() as $item) {
> $item->setAttrib('disable','disable');
> }
>
> I've been playing with dojo and was trying to figure out how I would do the
> same thing. I think I would use dojo.query and .attr("disabled",status));
> but I'm not sure how or where to do this.
>
> Thanks in advance, Jim
>
dojo.query only works with DOM Nodes. So
dojo.query(...).attr("disabled", disabled) will only disable the
_native_ input, not the Dijit itself. You need a dijit reference to use
the widget .attr():

dijit.byId("myInput").attr("disabled", "disabled");

With that in mind, there are several ways you can go about this, though
takes a little foresight in any case.

var myList = dojo.map(dojo.query("[dojoType]", "someId"), function(n){
return dijit.byNode(n); });

will give you an array of widget references contained within id="someId" you can iterate over:

dojo.forEach(myList, function(widget){ widget.attr('disabled',
'disabled'); });
// or:
dojo.forEach(myList, "item.attr('disabled', 'disabled')");

or if you know the id's in an array or something (of the targets):

dojo.forEach(theIdArray, function(id){ dijit.byId(id).attr(dis, dis); });

and last but not least, dijit.getEnclosingWidget will return a widget
ref from any node within the template or widget contents (including the inputs) ... perhaps
they all have a unique class:

dojo.query(".uniqueClass","myForm").forEach(function(n){
dijit.getEnclosingWidget(n).attr('disabled', 'disabled');
});


Hope this helps.

Regards,
Peter Higgins

没有评论: