snaif_lx 2015-09-06 12:45 采纳率: 80%
浏览 8372
已采纳

easyui datagrid combobox 默认值的设置

datagrid的一个列为combobox,现在要在新增数据的设置他的默认值,如何设置?
或者说如何让他默认选中第一行

           {
               field: 'NMK06', title: '性别', width: 100, align: 'center',
               editor: {
                   type: 'combobox', options: {
                       valueField: "value", textField: "text",
                       data: [{ value: '请选择', text: '请选择'},{ value: '男', text: '男' },{ value: '女', text: '女' }],
                       onLoadSuccess: function () {
                          // $(this).combobox('setValue', '0');
                          // $(this).combobox('setText', '--请选择--');
                       },

                       panelHeight: '60'
                   }
               }, sortable: true
           },
  • 写回答

8条回答 默认 最新

  • Go 旅城通票 2015-09-07 01:05
    关注

    你在调用datagrid的appendRow或者insertRow的时候,对应的列值的默认值要加上,要不匹配不上就是空的,如
    $('#xx').datagrid('appendRow',{NMK07:0,RealName:0}),要给默认值,要编辑时和指定的data数组值匹配不上导致combobox没有显示默认值

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • devmiao 2015-09-06 13:47
    关注
    评论
  • snaif_lx 2015-09-06 14:04
    关注
                           onLoadSuccess:function()  
                           {                        
                               var myvalue = $(this).combobox('getValue');  
                               if(myvalue == '' || myvalue==null )  
                               {                         
                                   var data = $(this).combobox('getData');
    
                                   $(this).combobox('select', data[0].id);
                                   $(this).combobox('setValue', data[0]['id']);
                                   $(this).combobox('setText', data[0]['text']);
                                   alert('aa');
                               }  
                           },   
    

    这样还是不行,求大能啊,是不是哪里有问题

    评论
  • snaif_lx 2015-09-06 14:05
    关注

           $(function () {
               var datagrid; //定义全局变量datagrid
               var editRow = undefined; //定义全局变量:当前编辑的行
               datagrid = $("#dd").datagrid({
                   url: 'UserCenter.aspx', //请求的数据源
                   iconCls: 'icon-save', //图标
                   pagination: true, //显示分页
                   pageSize: 15, //页大小
                   pageList: [15, 30, 45, 60], //页大小下拉选项此项各value是pageSize的倍数
                   fit: true, //datagrid自适应宽度
                   fitColumn: false, //列自适应宽度
                   striped: true, //行背景交换
                   nowap: true, //列内容多时自动折至第二行
                   border: false,
                   idField: 'ID', //主键
                   columns: [[//显示的列
                   {field: 'ID', title: '编号', width: 100, sortable: true, checkbox: true },
                    { field: 'UserName', title: '用户名', width: 100, sortable: true,
                        editor: { type: 'validatebox', options: { required: true} }
                    },
                     { field: 'RealName', title: '真实名称', width: 100,
                     editor: {
                         type: 'combobox', options: {
                             data: [{ id: 0, text: '请选择' }, { id: 1, text: '男' }, { id: 2, text: '女' }],
                             valueField: 'id', textField: 'text',
                             onLoadSuccess: function () {
                                 var myvalue = $(this).combobox('getValue');
                                 if (myvalue == '' || myvalue == null) {
                                     var data = $(this).combobox('getData');
    
                                     $(this).combobox('select', data[0].id);
                                     $(this).combobox('setValue', data[0]['id']);
                                     $(this).combobox('setText', data[0]['text']);
                                     alert('aa');
                                 }
                             }
                         }
    
                     }
                     },
                      { field: 'Email', title: '邮箱', width: 100,
                          editor: { type: 'validatebox', options: { required: true} }
                      }
                   ]],
                   queryParams: { action: 'query' }, //查询参数
                   toolbar: [{ text: '添加', iconCls: 'icon-add', handler: function () {//添加列表的操作按钮添加,修改,删除等
                       //添加时先判断是否有开启编辑的行,如果有则把开户编辑的那行结束编辑
                       if (editRow != undefined) {
                           datagrid.datagrid("endEdit", editRow);
                       }
                       //添加时如果没有正在编辑的行,则在datagrid的第一行插入一行
                       if (editRow == undefined) {
                           datagrid.datagrid("insertRow", {
                               index: 0, // index start with 0
                               row: {
    
                               }
                           });
                           //将新插入的那一行开户编辑状态
                           datagrid.datagrid("beginEdit", 0);
                           //给当前编辑的行赋值
                           editRow = 0;
                       }
    
                   }
                   }, '-',
                    { text: '删除', iconCls: 'icon-remove', handler: function () {
                        //删除时先获取选择行
                        var rows = datagrid.datagrid("getSelections");
                        //选择要删除的行
                        if (rows.length > 0) {
                            $.messager.confirm("提示", "你确定要删除吗?", function (r) {
                                if (r) {
                                    var ids = [];
                                    for (var i = 0; i < rows.length; i++) {
                                        ids.push(rows[i].ID);
                                    }
                                    //将选择到的行存入数组并用,分隔转换成字符串,
                                    //本例只是前台操作没有与数据库进行交互所以此处只是弹出要传入后台的id
                                    alert(ids.join(','));
                                }
                            });
                        }
                        else {
                            $.messager.alert("提示", "请选择要删除的行", "error");
                        }
                    }
                    }, '-',
                    { text: '修改', iconCls: 'icon-edit', handler: function () {
                        //修改时要获取选择到的行
                        var rows = datagrid.datagrid("getSelections");
                        //如果只选择了一行则可以进行修改,否则不操作
                        if (rows.length == 1) {
                            //修改之前先关闭已经开启的编辑行,当调用endEdit该方法时会触发onAfterEdit事件
                            if (editRow != undefined) {
                                datagrid.datagrid("endEdit", editRow);
                            }
                            //当无编辑行时
                            if (editRow == undefined) {
                                //获取到当前选择行的下标
                                var index = datagrid.datagrid("getRowIndex", rows[0]);
                                //开启编辑
                                datagrid.datagrid("beginEdit", index);
                                //把当前开启编辑的行赋值给全局变量editRow
                                editRow = index;
                                //当开启了当前选择行的编辑状态之后,
                                //应该取消当前列表的所有选择行,要不然双击之后无法再选择其他行进行编辑
                                datagrid.datagrid("unselectAll");
                            }
                        }
                    }
                    }, '-',
                    { text: '保存', iconCls: 'icon-save', handler: function () {
                        //保存时结束当前编辑的行,自动触发onAfterEdit事件如果要与后台交互可将数据通过Ajax提交后台
                        datagrid.datagrid("endEdit", editRow);
                    }
                    }, '-',
                    { text: '取消编辑', iconCls: 'icon-redo', handler: function () {
                        //取消当前编辑行把当前编辑行罢undefined回滚改变的数据,取消选择的行
                        editRow = undefined;
                        datagrid.datagrid("rejectChanges");
                        datagrid.datagrid("unselectAll");
                    }
                    }, '-'],
                   onAfterEdit: function (rowIndex, rowData, changes) {
                       //endEdit该方法触发此事件
                       console.info(rowData);
                       editRow = undefined;
                   },
                   onDblClickRow: function (rowIndex, rowData) {
                       //双击开启编辑行
                       if (editRow != undefined) {
                           datagrid.datagrid("endEdit", editRow);
                       }
                       if (editRow == undefined) {
                           datagrid.datagrid("beginEdit", rowIndex);
                           editRow = rowIndex;
                       }
                   }
               });
           });
    </script>
    


          <table id="dd">
    

    评论
  • qq_27569229 2015-09-06 18:13
    关注

    你在设置性别男女的时候,给他多加一个id,对应的每次选择之后传回相应的id. 每次加载的时候,再把id付给combobox.

    评论
  • qq_27569229 2015-09-06 18:19
    关注

    value text id 三个都写

    评论
  • snaif_lx 2015-09-07 01:02
    关注

    通过url获取值的combobox默认值没有问题,但是通过data方式获取数据的combobox默认就没有值,奇怪了

    评论
  • jetlee1028 2015-09-06 12:50
    关注

    在你的data里面,找到要默认值的那个,设置selected=true即可

    评论
查看更多回答(7条)

报告相同问题?

悬赏问题

  • ¥15 单点式登录SSO怎么爬虫获取动态SSO_AUTH_ACCESS_Token
  • ¥15 MATLAB中的fft问题
  • ¥30 哈夫曼编码译码器打印树形项目
  • ¥20 求完整顺利登陆QQ邮箱的python代码
  • ¥15 怎么下载MySQL,怎么卸干净原来的MySQL
  • ¥15 网络打印机Ip地址自动获取出现问题
  • ¥15 求局部放电案例库,用于预测局部放电类型
  • ¥100 QT Open62541
  • ¥15 stata合并季度数据和日度数据
  • ¥15 谁能提供rabbitmq,erlang,socat压缩包,记住版本要对应