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

javascript - How to reset dojo CheckedMultiSelect - Stack Overflow

programmeradmin2浏览0评论

The widget is inside the form, however

form.reset()

does not clear the previously selected values of CheckedMultiSelect.

var list = new CheckedMultiSelect({
    dropDown: true,
    labelText: 'States',
    multiple: true,
    name: 'state',
    onChange: getValues,
    required: false
}, "stateSelect");

I have tried code below but it doesnt work.

list.reset()

Thanks in advance

The widget is inside the form, however

form.reset()

does not clear the previously selected values of CheckedMultiSelect.

var list = new CheckedMultiSelect({
    dropDown: true,
    labelText: 'States',
    multiple: true,
    name: 'state',
    onChange: getValues,
    required: false
}, "stateSelect");

I have tried code below but it doesnt work.

list.reset()

Thanks in advance

Share Improve this question asked May 6, 2014 at 1:59 CoderCoder 3,1308 gold badges52 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 6

You can change the value of CheckedMultiSelect via list.set('value',[...]). The selection is updated immediately, when the list is not empty...

To clear the selection, call:

list.set('value',[]);
list._updateSelection();

Tested on Dojo 1.9.2.

It's because the dojox/form/CheckedMultiSelect did not implement the reset() behavior properly. Like Lukasz mentioned, it's only updating the selection when the list is not empty.

So, you could for example create your own implementation:

declare("dojox/form/FixedCheckedMultiSelect", [ CheckedMultiSelect ], {
    reset: function() {
        this.inherited(arguments);
        if (!this._resetValue || !this._resetValue.length) {
            this._updateSelection();
        }
    }
});

This will properly reset your field. Be aware though, when you reset your multiselect, it will reset to the default value. If, by default, you already selected certain options by using the selected attribute on your <option>, then it will reset to those values.

If you want to make sure that when it resets, it's always unchecking all items, then you should add a single line to your implementation so it bees:

declare("dojox/form/FixedCheckedMultiSelect", [ CheckedMultiSelect ], {
    reset: function() {
        this._resetValue = [];
        this.inherited(arguments);
        if (!this._resetValue || !this._resetValue.length) {
            this._updateSelection();
        }
    }
});

I also made an example JSFiddle.

发布评论

评论列表(0)

  1. 暂无评论