简体   繁体   English

ExtJs-表单中的复选框选择

[英]ExtJs - checkbox selection in form

在此处输入图片说明

I want to check all checkboxes in a form when user click on top checkbox. 当用户单击顶部复选框时,我想检查表单中的所有复选框。 If any checkbox uncheck i want to uncheck top checkbox. 如果有任何复选框,请取消选中我要取消选中顶部的复选框。 I need same functionality like grid checkbox column. 我需要像网格复选框列一样的功能。

ExtJS version: 6.2.1 ExtJS版本:6.2.1

You can use xtype: checkboxgroup . 您可以使用xtype: checkboxgroup

In this FIDDLE , I have create a demo using checkboxgroup . 在这个FIDDLE中 ,我使用checkboxgroup创建了一个演示。 I hope this will help you to achieve your requirement. 希望这可以帮助您达到要求。

CODE SNIPPET 代码片段

Ext.application({
    name: 'Example',

    launch: function () {
        Ext.create('Ext.form.Panel', {

            bodyPadding: 10,

            renderTo: Ext.getBody(),

            items: [{
                xtype: 'checkboxgroup',
                layout: 'vbox',
                fieldLabel: 'Privilege',
                checkedArr: [],
                defaults: {
                    flex: 1,
                    name: 'mycheck',

                    listeners: {
                        change: function (field, newValue, oldValue) {
                            var group = field.up('checkboxgroup');

                            if (field.name == 'all') {
                                group.doCheckUnCheckAll(newValue);
                            } else {
                                var len = group.query('[name=mycheck]').length,
                                    allCB = group.down('[name=all]');

                                if (newValue) {
                                    group.checkedArr.push(field.inputValue)
                                } else {
                                    Ext.Array.remove(group.checkedArr, field.inputValue);
                                }
                                group.doSetCBValue(allCB, len == group.checkedArr.length);
                            }
                        }
                    }
                },
                /**
                 * this function will set value of checkbox and do event suspend & resume
                 * @param {Checbox}
                 * @param {Boolean}
                 */
                doSetCBValue: function (f, v) {
                    //Check or uncheck
                    f.suspendEvent('change');
                    f.setValue(v);
                    f.resumeEvent('change');
                },

                /**
                 * This event will check or uncheck the all checkbox when {ALL} checkbox has beed checked/unchecked
                 * @param {Boolean}
                 */
                doCheckUnCheckAll: function (isCheck) {
                    this.query('[name=mycheck]').forEach(f => {
                        this.doSetCBValue(f, isCheck);
                        //For checking to other checkbox is checked or not
                        if (isCheck) {
                            if (this.checkedArr.indexOf(f.inputValue) == -1)
                                this.checkedArr.push(f.inputValue);
                        } else {
                            Ext.Array.remove(this.checkedArr, f.inputValue);
                        }
                    });
                },

                items: [{
                    boxLabel: 'ALL',
                    inputValue: 'all',
                    name: 'all'
                }, {
                    boxLabel: 'Item 1',
                    inputValue: '1'
                }, {
                    boxLabel: 'Item 2',
                    inputValue: '2'
                }, {
                    boxLabel: 'Item 3',
                    inputValue: '3'
                }, {
                    boxLabel: 'Item 4',
                    inputValue: '4'
                }, {
                    boxLabel: 'Item 5',
                    inputValue: '5'
                }, {
                    boxLabel: 'Item 6',
                    inputValue: '6'
                }]
            }]
        });
    }
});

I believe you'll have to implement the custom logic yourself. 我相信您必须自己实现自定义逻辑。

I would suggest using the Ext.form.CheckboxGroup component and listen to the change event ( see example in docs ). 我建议使用Ext.form.CheckboxGroup组件并侦听change事件( 请参阅docs中的示例 )。 The value of the group would look something like this: 该组的值如下所示:

{    
 "Privileges": [
   "All",
   "Visible",
   "Manage"    
 ] 
}

Depending on the value of the group dynamically check or uncheck each checkbox. 根据组的值,动态选中或取消选中每个复选框。

As ground_call said, you need to implement custom logic. 正如ground_call所说,您需要实现自定义逻辑。 One of approaches is to set checkbox group, and assign itemId to top checkbox. 一种方法是设置复选框组,并将itemId分配给顶部复选框。 In ViewController create listener on change event for that top checkbox, that will turn on/off all other checkboxes. ViewController ,为该顶部复选框的change事件创建侦听器,这将打开/关闭所有其他复选框。 Other checkboxes you can handle via checkboxgroup 's change event. 您可以通过checkboxgroupchange事件处理的其他复选框。 Here is fiddle with template for this implementation: https://fiddle.sencha.com/#view/editor&fiddle/2eqm 这是此实现的模板摆弄: https : //fiddle.sencha.com/#view/editor&fiddle/2eqm

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM