javascript 检查 CheckboxGroup ExtJS 组件中的各种复选框
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9079600/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Check various checkboxes in CheckboxGroup ExtJS component
提问by balkon_smoke
I'm looking for solution how to check items in the Ext.form.CheckboxGroup
component which is already rendered and contains set of items.
我正在寻找如何检查Ext.form.CheckboxGroup
组件中已呈现并包含项目集的项目的解决方案。
Code of the component is:
组件代码为:
var oCheckboxGroup = new Ext.form.CheckboxGroup({
columns: 2,
vertical: true,
items: [
{boxLabel: "Value 1", inputValue: 1},
{boxLabel: "Value 2", inputValue: 2},
...
{boxLabel: "Value N", inputValue: N}
]
});
This component will be displayed in a modal window by clicking on a button, so I have to recheck items in checkboxgroup depending to record that will be modified.
该组件将通过单击按钮显示在模式窗口中,因此我必须根据将被修改的记录重新检查复选框组中的项目。
For example, when I'll show window first time I'll have to pre-check items 1, 2and 3, at the second time – 2, 4and 5.
例如,当我第一次显示 window 时,我必须在第二次预检查项目1、2和3– 2、4和5。
So the real question is: how can I loop through checkbox group items and check/uncheck checkboxes?
所以真正的问题是:如何遍历复选框组项目并选中/取消选中复选框?
Btw, I tried next solution, but nothing:
顺便说一句,我尝试了下一个解决方案,但没有:
oCheckboxGroup.items.each(function(oEl) {
oEl.checked = true;
});
Thanks.
谢谢。
UPD
UPD
Answer found. Question will be closed in 2 days when I'll be allowed to accept my own answer, or earlier if someone else answer correctly =))
找到了答案。问题将在 2 天后关闭,届时我将被允许接受我自己的答案,或者如果其他人回答正确则更早=))
回答by balkon_smoke
Solution found. Sencha says:
找到解决方案。Sencha 说:
setValue(Object value): Ext.form.CheckboxGroup
Sets the value(s) of all checkboxes in the group. The expected format is an Object of name-value pairs corresponding to the names of the checkboxes in the group. Each pair can have either a single or multiple values:
- A single Boolean or String value will be passed to the setValue method of the checkbox with that name. See the rules in Ext.form.field.Checkbox.setValue for accepted values.
- An Array of String values will be matched against the inputValue of checkboxes in the group with that name; those checkboxes whose inputValue exists in the array will be checked and others will be unchecked.
setValue(对象值):Ext.form.CheckboxGroup
设置组中所有复选框的值。预期格式是与组中复选框的名称相对应的名称-值对的对象。每对可以有一个或多个值:
- 单个布尔值或字符串值将传递给具有该名称的复选框的 setValue 方法。有关可接受的值,请参阅 Ext.form.field.Checkbox.setValue 中的规则。
- 字符串值数组将与具有该名称的组中复选框的 inputValue 匹配;inputValue 存在于数组中的那些复选框将被选中,其他的将不被选中。
So I just added name: "cbgroup"
property to checkbox
configs and then I use construction like
所以我只是name: "cbgroup"
在checkbox
配置中添加了属性,然后我使用了类似的构造
// first time
oCheckboxGroup.setValue({
cbgroup: [1, 2, 3]
})
// second time
oCheckboxGroup.setValue({
cbgroup: [2, 4, 5]
})
Thanks all who tried to help me, hope this answer will save somebody's time ;)
感谢所有试图帮助我的人,希望这个答案能节省一些人的时间;)
回答by Ajit Kumar
For ExtJS 3.x
对于 ExtJS 3.x
Use the setValue method on the CheckboxGroup object and specify true/false for the individual items in it:
在 CheckboxGroup 对象上使用 setValue 方法并为其中的各个项目指定 true/false:
setValue( Object value )
E.g. in your case, to pre-check 1, 2, 3 you will have to call:
例如,在您的情况下,要预先检查 1、2、3,您必须致电:
cbxGrpObj.setValue([true,true,true]);
To pre-check 4,5 and un-check 1,2,3 you will have to call:
要预先检查 4,5 并取消检查 1,2,3,您必须致电:
cbxGrpObj.setValue([false,false,false, true,true]);
Hope this helps!
希望这可以帮助!
回答by Paranoima
I just stumbled upon this while looking for how to set a checkboxgroup value from a form.setValues call. As it turns out, in ExtJS if you give all of your checkboxes in the group the same name, you can just give setValues an array of values for that name and any matching values will be checked for you.
我只是在寻找如何从 form.setValues 调用中设置复选框组值时偶然发现了这一点。事实证明,在 ExtJS 中,如果您为组中的所有复选框指定相同的名称,您只需为 setValues 指定一个该名称的值数组,并且将为您检查任何匹配的值。
var oCheckboxGroup = new Ext.form.CheckboxGroup({
columns: 2,
vertical: true,
items: [
{name: 'cbg1', boxLabel: "Value 1", inputValue: 1},
{name: 'cbg1', boxLabel: "Value 2", inputValue: 2},
{name: 'cbg1', boxLabel: "Value N", inputValue: N}
]
});
...
form.setValues( { cbg1: [ '1','N' ] } );
I hope this helps anyone else who finds this with the same keywords I did.
我希望这可以帮助其他使用我所做的相同关键字找到它的人。
回答by Kavan Fatehi
Ext.each(`oCheckboxGroup`.items.items, function(item) {
item.checked = true;
}, this);