easyui实现datagrid行内编

easyui的datagrid行内编辑对于修改信息而言比较方便直观,下面举例说明:  

需求说明: 
在行内编辑员工状态 
 

一、编辑jsp 
Html代码   收藏代码
  1. ...  
  2.                <a href="#" id="save" onclick="obj.save()" class="easyui-linkbutton" iconCls="icon-save" plain="true">保存</a>  
  3.                <a href="#" id="redo" onclick="obj.redo()" class="easyui-linkbutton" iconCls="icon-redo" plain="true">取消编辑</a>  
  4.            </form>  
  5. ...  


二、编辑js 
说明: 
1.根据需求,除了status列,其它列均不要求被编辑,故设置readonly:'true',disabled:true,禁止其被编辑。 
2.对obj对象设置editRow的意义在于限制用户只能对一行进行修改操作,即在this.editRow == undefined前提下进行修改。 
3.$('#dg').datagrid('beginEdit',index);是修改操作的开始,$('#dg').datagrid('endEdit',this.editRow);结束修改。需注意的是,只有提交的信息通过验证(如果存在的话)才能结束修改。结束修改后会进入datagrid的onAfterEdit方法,以提交至控制器。 

影音先锋电影 http://www.iskdy.com/ 
Js代码   收藏代码
  1. obj = {  
  2.             //编辑行属性  
  3.             editRow:undefined,  
  4.             //保存  
  5.             save:function(){  
  6.                 //结束当前编辑行.注:1.只有填写的信息通过验证后才能结束编辑.2.结束编辑后进入datagrid的onAfterEdit方法。  
  7.                 $('#dg').datagrid('endEdit',this.editRow);  
  8.             },  
  9.             //取消  
  10.             redo:function(){  
  11.                 this.editRow = undefined  
  12.                 $('#dg').datagrid('rejectChanges')//回滚已编辑数据  
  13.             },  
  14.             //datagrid行内修改用户状态。index:当前行索引值。  
  15.             editStatus:function(index){  
  16.                 //获取选择行  
  17.                 var rows = $('#dg').datagrid('getSelections');  
  18.                 if(currUserStatus!=0){  
  19.                     $.messager.alert('警告''您不是管理员,无权执行此操作!''warning');  
  20.                 }else{  
  21.                     //修改操作需选中一行  
  22.                     if(rows.length == 1 ){  
  23.                         //如果已有编辑行,则关闭当前编辑行  
  24.                         if(this.editRow != undefined) $('#dg').datagrid('endEdit',this.editRow);  
  25.                         //如果未有编辑行,则进行编辑  
  26.                         if(this.editRow == undefined){  
  27.                             //设置当前行为编辑状态  
  28.                             $('#dg').datagrid('beginEdit',index);  
  29.                             //将当前行索引值赋给obj的editRow  
  30.                             this.editRow = index;  
  31.                             //取消选择当前行  
  32.                             $('#dg').datagrid('unselectRow',index);  
  33.                         }  
  34.                     }else{  
  35.                         $.messager.alert('警告''修改必须选中仅且一行!''warning');  
  36.                     }  
  37.                 }  
  38.             },  
  39.             ...  
  40.               
  41.     }  
  42. ...  
  43. $('#dg').datagrid({  
  44.         //条纹  
  45.         striped:true,   
  46.         //标题  
  47.         title:'用户管理',  
  48.         //提交的url地址  
  49.         url:'emplAction!listEmpl.action',  
  50.         //允许收缩数据  
  51.         collapsible:'true',  
  52.         //分页  
  53.         pagination:'true',  
  54.         //自适应  
  55.         fit:'true',  
  56.         //默认页容量  
  57.         pageSize:'10',  
  58.         //分页工具位置  
  59.         pagePosition:'top',  
  60.         //列自适应  
  61.         fitColumns:'true',  
  62.         //显示编号  
  63.         rownumbers:'true',  
  64.         //排序  
  65.         sortable:'true',  
  66.         sortName:'id',  
  67.         //主键  
  68.         idField:'id',  
  69.         ...  
  70.         //在datagrid中进行添加或修改操作。注:当前业务没有在datagrid中“添加操作”的需求,只有更新操作,故省去了“添加操作”的判定。  
  71.         onAfterEdit:function(rowIndex,rowData,changes){  
  72.             var updated = $('#dg').datagrid('getChanges','updated');  
  73.             var url = '';  
  74.             //如果选择了修改,但实际未做任何改变,则直接返回false  
  75.             if(updated.length==0){  
  76.                 obj.editRow = undefined;  
  77.                 return false  
  78.             }  
  79.             //修改用户  
  80.             if(updated.length>0){  
  81.                 url='emplAction!editStatus.action';  
  82.             }  
  83.             //ajax提交,rowData是行数据  
  84.             $.ajax({  
  85.                 timeout:1000,//超时时间设置,单位:毫秒  
  86.                 type:'post',  
  87.                 url:url,  
  88.                 data:{  
  89.                     row:rowData,  
  90.                 },  
  91.                /* beforeSend:function(){ 
  92.                     console.log("提交前..") 
  93.                     console.log(rowData); 
  94.                     console.log(url); 
  95.                     $('#dg').datagrid('loading'); 
  96.                 },*/  
  97.                 success:function(data){  
  98.                     if(data){  
  99.                         $('#dg').datagrid('loaded');  
  100.                         $('#dg').datagrid('load');  
  101.                         $('#dg').datagrid('unselectAll');  
  102.                         //当前行结束编辑  
  103.                         $('#dg').datagrid('endEdit',obj.editRow);  
  104.                         $.messager.show({  
  105.                             title:'提示',  
  106.                             msg:data,  
  107.                         })  
  108.                         obj.editRow = undefined;  
  109.                     }  
  110.                 },  
  111.                 complete:function(XMLHttpRequest,status){  
  112.                     if(status=='timeout'){  
  113.                         alert('修改失败,超时..')  
  114.                     }  
  115.                 },  
  116.                 error:function(XMLHttpRequest,errorThrown){  
  117.                     if(errorThrown=='408'){  
  118.                         alert('修改失败,超时..')  
  119.                     }  
  120.                 }  
  121.                   
  122.             })  
  123.         },  
  124.         //列字段  
  125.         columns:[[  
  126.                   {  
  127.                       field:'ck',  
  128.                       checkbox:true  
  129.                   },  
  130.                   {  
  131.                       field:'eid',  
  132.                       title:'用户编号',  
  133.                       height:30,  
  134.                       width:100,  
  135.                       align:'center',  
  136.                       sortable:'true',//排序  
  137.                       readonly:'true',  
  138.                       disabled:true//禁止编辑  
  139.                       /*editor:{ 
  140.                           type:'numberbox', 
  141.                           options:{ 
  142.                               required:true, 
  143.                               disabled:true//禁止编辑 
  144.                           } 
  145.                       }*/  
  146.                   },  
  147.                   {  
  148.                       field:'ename',  
  149.                       title:'用户名',  
  150.                       width:100,  
  151.                       height:30,  
  152.                       align:'center',  
  153.                       readonly:'true',  
  154.                       disabled:true  
  155.                   },  
  156.                   {  
  157.                       field:'hiredate',  
  158.                       title:'入职时间',  
  159.                       width:100,  
  160.                       height:30,  
  161.                       align:'center',  
  162.                       readonly:'true',  
  163.                       disabled:true  
  164.                   },  
  165.                   {  
  166.                       field:'jname',  
  167.                       title:'职位描述',  
  168.                       width:100,  
  169.                       height:30,  
  170.                       align:'center',  
  171.                       readonly:'true',  
  172.                       disabled:true  
  173.                   },  
  174.                   {  
  175.                       field:'dname',  
  176.                       width:100,  
  177.                       height:30,  
  178.                       title:'所属部门',  
  179.                       align:'center',  
  180.                       readonly:'true',  
  181.                       disabled:true  
  182.                   },  
  183.                   {  
  184.                       field:'status',  
  185.                       width:100,  
  186.                       height:30,  
  187.                       title:'状态',  
  188.                       align:'center',  
  189.                       editor:{//编辑选项  
  190.                           type:'combobox',  
  191.                           options:{  
  192.                               url:"emplAction!getJobStatusList.action",//请求地址  
  193.                               required:true,  
  194.                               panelHeight: 'auto',  
  195.                               valueField:'jsid',//下拉框的值  
  196.                               textField:'status'//下拉框显示的文本  
  197.                           }  
  198.                       }  
  199.                             
  200.                   },  
  201.                   {  
  202.                       field:'process',  
  203.                       title:'操作',  
  204.                       width:150,  
  205.                       height:30,  
  206.                       align:'center',  
  207.                       //格式化列值(设置列值在页面的显示样式)  
  208.                       formatter:function(value,row,index){  
  209.                           return "<a href='javascript:obj.editStatus("+index+")'>修改状态</a>"  
  210.                           +"  <a href='javascript:obj._delete("+row.eid+")'>删除</a>"  
  211.                       }  
  212.                   }  
  213.                   ...  
  214.         ]],  
  215.           
  216.     })  

伦理片 http://www.dotdy.com/
三、编辑控制器 
Java代码   收藏代码
  1. //获取职位信息  
  2.     public void getJobStatusList() throws IOException{  
  3.            resp.setCharacterEncoding("UTF-8");  
  4.            jobStatus=jobStatusService.listJobStatus();  
  5.            List<Map<String,String>> list = new ArrayList<>();  
  6.            Map<String,String> map = null;  
  7.            if(jobStatus.size()!=0){  
  8.                for (int i = 0; i < jobStatus.size(); i++) {  
  9.                    map = new HashMap<String, String>();  
  10.                    map.put("jsid", String.valueOf(jobStatus.get(i).getJsid()));  
  11.                    map.put("status", jobStatus.get(i).getStatus());  
  12.                    list.add(map);  
  13.                }  
  14.            }  
  15.            String s = JSON.toJSONString(list);  
  16.            PrintWriter out = resp.getWriter();  
  17.            out.println(s);  
  18.     }  
  19.     //修改用户状态  
  20.     public void editStatus()  
  21.             throws IOException {  
  22.        resp.setCharacterEncoding("UTF-8");    
  23.            //获取提交的datagrid中的行数据  
  24.        int eid = Integer.parseInt(req.getParameter("row[eid]"));  
  25.        int status = Integer.parseInt(req.getParameter("row[status]"));  
  26.        boolean b = false;  
  27.        b = empService.editStatus(status, eid);  
  28.        PrintWriter out = resp.getWriter();  
  29.        if(b){  
  30.             out.print("修改成功!");  
  31.        }else{  
  32.             out.print("修改失败!");  
  33.        }  
  34.     }  


大功告成,效果如下: 
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值