shijiebao 2010-09-21 16:43
浏览 223
已采纳

extjs form设置值的问题

问题描述:在form中设置checkbox的值设置不进去,checkbox分为functionIds和modules,分别给他们指定了inputValue,详细问题请看下面的代码和带有"**"的注释,该代码可以运行,有一个测试的tabr。谢谢

[code="javascript"]
Ext.onReady(function() { // form的元素
var mItems = [{
colspan : 2,
xtype : 'panel',
layout : 'table',
items : [{
xtype : 'numberfield',
name : 'id',
hidden : true
}, {
xtype : 'numberfield',
name : 'unitCodePcId',
hidden : true
}, {
xtype : 'label',
text : '角色名称:'
}, {
xtype : 'textfield',
width : 200,
name : 'roleName'
}, {
xtype : 'label',
text : '系统最高用户:'
}, {
xtype : 'label',
text : '是'
}, {
// 这个值就可以设置进来
xtype : 'radio',
name : "isTOPUser",
inputValue : true

                            }, {
                                xtype : 'label',
                                text : '否'
                            }, {
                                xtype : 'radio',
                                name : "isTOPUser",
                                inputValue : false

                            }]
                }];
        // 模块的数据,真是业务是从数据库查询得来
        var modules = [{
                    id : 1,
                    moduleCode : 'userManager',
                    moduleName : '用户管理',// 一级模块
                    functions : [],
                    modules : [{
                                id : 4,
                                moduleCode : 'userRegister',
                                moduleName : '用户注册',
                                functions : [{
                                            id : 1,
                                            typ : 0
                                        }]
                            }, {
                                id : 7,
                                moduleCode : 'unitRegister',
                                moduleName : '单位注册',
                                functions : [{
                                            id : 2,
                                            typ : 0
                                        }, {
                                            id : 5,
                                            typ : 1
                                        }]
                            }]
                }];
        var functionText = ['录入', '查询', '打印'];
        // 循环添加checkbox
        Ext.each(modules, function(module) {
            var m1Label = {
                xtype : 'label',
                text : module.moduleName,
                style : "font-weight:bold;",
                colspan : 2
            };
            mItems.push(m1Label);
            Ext.each(module.modules, function(module2) {
                var mCheckBox = {
                    xtype : 'panel',
                    layout : 'form',
                    items : [{
                                xtype : 'checkbox',
                                name : 'moduleIds',
                                fieldLabel : module2.moduleName,
                                // inputValue是module的id
                                inputValue : module2.id
                            }]
                };
                mItems.push(mCheckBox);
                var fItems = [];
                Ext.each(module2.functions, function(f) {
                            var itemCheckBox = {
                                xtype : 'panel',
                                layout : 'form',
                                items : [{

                                            xtype : 'checkbox',
                                            name : 'functionIds',
                                            fieldLabel : functionText[f.type],
                                            // inputValue是function的id
                                            inputValue : f.id
                                        }]
                            };
                            fItems.push(itemCheckBox);
                        });
                var fPanel = {
                    xtype : 'panel',
                    layout : 'table',
                    items : fItems
                }
                mItems.push(fPanel);
            });
        });
        var form = new Ext.FormPanel({
                    width : '100%',
                    height : 500,
                    layout : "table",
                    autoScroll : true,
                    autoWidth : false,
                    autoHeight : false,
                    frame : true,
                    renderTo : Ext.getBody(),
                    defaults : {
                        height : 22,
                        width : '100%',
                        labelAlign : 'right'
                    },
                    layoutConfig : {
                        tableAttrs : {
                            cellspacing : 4,
                            cellpadding : 4,
                            border : 0,
                            width : '100%'
                        },
                        columns : 2
                    },
                    items : mItems,
                    tbar : [{
                                text : '测试设置form checkbox的值',
                                handler : function() {
                                    var value = {
                                        id : 1,
                                        roleName : '测试',
                                        unitCodePcId : 22,
                                        isTOPUser : true,
                                        // name为"functionIds" checkbox的值
                                        functionIds : [1, 2, 5],
                                        // name为"modules" checkbox的值
                                        modules : [4, 7]
                                    };
                                    // *****在这里如果勾选了checkbox,formgetValues就可以得到checkbox的值
                                    var t = form.getForm().getValues();
                                    // *****但是这里setValues的时候不能把值设置到form中,查看了setValues()方法的源码也没看出个头绪
                                    form.getForm().setValues(value);
                                    t = form.getForm().getValues();
                                    var tt;
                                }
                            }]
                });
    });

[/code]

  • 写回答

1条回答 默认 最新

  • lizhiyezi 2010-09-22 09:52
    关注

    建议使用checkboxgroup.
    不然设置值的时候 functionIds:1 这样的话只会选中第一个。

    如果不想使用checkboxgroup还有种办法就是为checkbox设置id
    在设置值的时候[{id:'',value:1},{id:'',value:1}]使用这种格式值设置,每个id,value对应一个组件,id: checkbox使用id 其他组件可以使用name名称就行了。

    checkbox 值只有为 1,true,on才会选中。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 多址通信方式的抗噪声性能和系统容量对比
  • ¥15 winform的chart曲线生成时有凸起
  • ¥15 msix packaging tool打包问题
  • ¥15 finalshell节点的搭建代码和那个端口代码教程
  • ¥15 Centos / PETSc / PETGEM
  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答