最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Select all on dojo checkbox group - Stack Overflow

programmeradmin0浏览0评论

I currently have a dojo checkbox group (dijit.form.CheckBox with names in the form "some_name[]") It works exactly as desired however I need to add a select all button/link, preferably not another checkbox (this shouldn't be necessary given the functionality will be implemented in js).

Is the a "dojo" way to do this as using standard js seems to get the elements fine but setting element.checked=true has no effect. For the purposes of testing, I've disabled the dijit.form.CheckBox (so I have regular bog standard checkboxes) and I am able to mark all the boxes checked with the code that is failing when dojo'ified.

I'm running dojo 1.5 if that makes any difference

UPDATE:

OK, it seems to be setting the checkbox to be checked/unchecked but not re-rendering the dojo widget. I'm also being told that the checkboxes don't have any of the method I would expect them to have as dijit.form.CheckBox objects (like the set() method).

I currently have a dojo checkbox group (dijit.form.CheckBox with names in the form "some_name[]") It works exactly as desired however I need to add a select all button/link, preferably not another checkbox (this shouldn't be necessary given the functionality will be implemented in js).

Is the a "dojo" way to do this as using standard js seems to get the elements fine but setting element.checked=true has no effect. For the purposes of testing, I've disabled the dijit.form.CheckBox (so I have regular bog standard checkboxes) and I am able to mark all the boxes checked with the code that is failing when dojo'ified.

I'm running dojo 1.5 if that makes any difference

UPDATE:

OK, it seems to be setting the checkbox to be checked/unchecked but not re-rendering the dojo widget. I'm also being told that the checkboxes don't have any of the method I would expect them to have as dijit.form.CheckBox objects (like the set() method).

Share Improve this question edited Jun 26, 2013 at 16:20 Endophage asked Jan 26, 2011 at 21:08 EndophageEndophage 21.5k13 gold badges61 silver badges93 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 6

So I found out what the problem was. I was doing dojo.query() to get the checkbox group dom nodes. This however doesn't return the dijit widget. To get those I had to do:

nodes = dojo.query('[name=\"checkbox_group[]\"]'); 
dojo.forEach(nodes,function(node)   
  {dijit.getEnclosingWidget(node).set("checked",true);});

By manipulating the widget it visually updates correctly. Also, I wasn't aware that the checkbox input element isn't the widget itself so dijit.byNode(node) returns undefined because the checkbox node doesn't match any of the initialised dijit widgets.

We do this something like show below. This is trimmed down from a lot of other things that get hooked up as handlers in an "addHandlers" function that gets called when the page is loaded.

The selectall and clearall are the links that we're hooking up to.

var selectall = dojo.byId('selectall');
var clearall  = dojo.byId('clearall');

var checklist = getCheckboxList(); // Magic happens here (1)

dojo.connect(selectall, "onclick",
    function(evt) {
        allClick(evt, checklist, true);
    }
);

dojo.connect(clearall, "onclick",
    function(evt) {
        allClick(evt, checklist, false);
    }
);

(1) We get a list of the checkboxes that will be affected based on some ugly criteria, and we use the list for more than this check/clear-all -- you could probably do something much simpler to get yours.

The "allClick" function just checks or unchecks the list of things you pass it

function allClick(evt, list, checkstate)
{
    dojo.forEach(list,
        function(el, idx, ary) {
            el.checked = checkstate;
        }
    );
    evt.preventDefault(); // keep the link from firing
}

In case anyone is looking at this for the answer, and there is now (4 years later) another way to do this:

var checkboxes = registry.findWidgets(dom.byId('checkGroupWrapper'));

checkboxes.forEach(function (checkboxWidg) {
    checkboxWidg.set('checked', true);
});

See this documentation.

发布评论

评论列表(0)

  1. 暂无评论