JqueryEasyUI修改DataGrid使其支持多选框

最近研究JqueryEasyUI插件,用它的DataGrid来实现管理员权限管理的功能,但是在权限设置的时候用它提供的ComboBox发现有个BUG,那就是在点编辑的载入数据的时候莫名其妙的多了两个",," 干脆就自己用checkbox写个权限管理功能出来吧.

首先我有一个权限表power:

然后建一个服务端文件,来返回power表的数据(JSON格式),如下图:

最后再htm页面添加JqueryEasyUI的DataGrid控件,在页面载入时就加载所有的checkbox(从服务器获取数据),然后在editItem()事件里载入当前权限,

  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4.     <title></title>  
  5.     <link href="css/jiangsCMS.css" rel="stylesheet" type="text/css" />  
  6.     <link href="jquery-easyui-1.2.4/themes/default/easyui.css" rel="stylesheet" type="text/css" />  
  7.     <link href="jquery-easyui-1.2.4/themes/icon.css" rel="stylesheet" type="text/css" />  
  8.     <script src="jquery-easyui-1.2.4/jquery-1.6.min.js" type="text/javascript"></script>  
  9.     <script src="jquery-easyui-1.2.4/jquery.easyui.min.js" type="text/javascript"></script>  
  10.     <script src="js/jiangsCMS.js" type="text/javascript"></script>  
  11.     <script type="text/javascript">  
  12.         function formatDisplay(val, row) { //格式化显示/隐藏  
  13.             if (val == "True") {  
  14.                 return '启用';  
  15.             } else {  
  16.                 return '<span style="color:gray;">屏蔽</span>';  
  17.             }  
  18.         }  
  19.         function newItem(title, link) {//添加,title=标题,link=提交的页面  
  20.             $('#dlg').dialog('open').dialog('setTitle', title);  
  21.             $('#fm').form('clear');  
  22.             url = link;  
  23.         }  
  24.         function editItem(title, link) {  
  25.             var row = $('#dg').datagrid('getSelected');  
  26.             if (row) {  
  27.                 $('#dlg').dialog('open').dialog('setTitle', title);  
  28.                 $('#fm').form('load', row);  
  29.                 url = link + '&id=' + row.id;  
  30.                 initpower(row);//编辑的时候加载power的checkbox选中项  
  31.             }  
  32.         }  
  33.         function saveItem() {  
  34.             $('#fm').form('submit', {  
  35.                 url: url,  
  36.                 onSubmit: function () {  
  37.                     return $(this).form('validate');  
  38.                 },  
  39.                 success: function (result) {  
  40.                     var result = result.split('|'); //获取返回结果  
  41.                     if (result[0] == "1") {  
  42.                         $('#dlg').dialog('close');      // close the dialog     
  43.                         $('#dg').datagrid('reload');    // reload the user data     
  44.                     } else {  
  45.                         $.messager.show({  
  46.                             title: '提示',  
  47.                             msg: result[1]  
  48.                         });  
  49.                     }  
  50.                 }  
  51.             });  
  52.         }  
  53.         function removeItem(link) {  
  54.             var row = $('#dg').datagrid('getSelected');  
  55.             if (row) {  
  56.                 $.messager.confirm('提示', '确定删除这条数据?', function (r) {  
  57.                     if (r) {  
  58.                         $.post(link, { "id": row.id, "action": "delete" }, function (result) {  
  59.                             var result = result.split('|');  
  60.                             if (result[0] == "1") {  
  61.                                 $('#dg').datagrid('reload');    // reload the user data     
  62.                             } else {  
  63.                                 $.messager.show({   // show error message     
  64.                                     title: '提示',  
  65.                                     msg: result[1]  
  66.                                 });  
  67.                             }  
  68.                         });  
  69.                     }  
  70.                 });  
  71.             }  
  72.         }  
  73.         //初始化order只允许输入0-999数字  
  74.         function initOrder() {  
  75.             $('input[name=order]').numberbox({  
  76.                 min: 0,  
  77.                 max: 999,  
  78.                 precision: 0  
  79.             });  
  80.         }  
  81.         function initpower(row) {//编辑的时候加载power的checkbox选中项  
  82.             var temp = row.power.split(',');  
  83.             for (var i = 0; i < temp.length; i++) {  
  84.                 $("#_" + temp[i]).attr("checked", "checked");  
  85.             }  
  86.   
  87.         }  
  88.         function loadpower() {//页面初始化加载power的checkbox  
  89.             $.post("bll/adminpower.ashx", function (data, status) {  
  90.                 if (status == "success") {  
  91.                     var powers = $.parseJSON(data);  
  92.                     var sp1 = $("#span1");  
  93.                     for (var i = 0; i < powers.length; i++) {  
  94.                         power = powers[i];  
  95.                         var checkbox = $("<input id=_" + power.id + " type='checkbox' name='power' value=" + power.id + " /><label for=_" + power.id + ">" + power.item + "</label>");  
  96.                         if ((i + 1) % 4 == 0) {  
  97.                             checkbox = $("<input id=_" + power.id + " type='checkbox' name='power' value=" + power.id + " /><label for=_" + power.id + ">" + power.item + "</label><br />");  
  98.                         }  
  99.                         sp1.append(checkbox);  
  100.                     }  
  101.                 }  
  102.             });  
  103.   
  104.         }  
  105.         $(function () {  
  106.             initOrder();  
  107.             loadpower();  
  108.         });  
  109.           
  110.     </script>  
  111. </head>  
  112. <body class="mainframebody">  
  113.     <div class="mainframebox">  
  114.         <div class="mainframetitle">  
  115.             网站管理员设置</div>  
  116.         <div class="mainframecontent">  
  117.             <table id="dg" class="easyui-datagrid" style="width: 800px; height: 350px;" url="bll/adminuser.ashx?action=select"  
  118.                 singleselect="true" title="网站管理员" iconcls="icon-save" toolbar="#toolbar" rownumbers="false"  
  119.                 pagination="true">  
  120.                 <thead>  
  121.                     <tr>  
  122.                         <th field="id" width="80" sortable="true">  
  123.                             编号ID  
  124.                         </th>  
  125.                         <th field="userName" width="120">  
  126.                             用户名  
  127.                         </th>  
  128.                         <th field="passWord" width="120">  
  129.                             密码  
  130.                         </th>  
  131.                         <th field="effect" width="80" formatter="formatDisplay">  
  132.                             是否生效  
  133.                         </th>  
  134.                         <th field="power" width="160">  
  135.                             权限  
  136.                         </th>  
  137.                         <th field="comment" width="200">  
  138.                             备注  
  139.                         </th>  
  140.                     </tr>  
  141.                 </thead>  
  142.             </table>  
  143.             <div id="toolbar">  
  144.                 <a href="#" class="easyui-linkbutton" iconcls="icon-add" plain="true" onclick="newItem('添加管理员','bll/adminuser.ashx?action=insert')">  
  145.                     添加</a> <a href="#" class="easyui-linkbutton" iconcls="icon-edit" plain="true" onclick="editItem('编辑管理员信息','bll/adminuser.ashx?action=update')">  
  146.                         编辑</a> <a href="#" class="easyui-linkbutton" iconcls="icon-remove" plain="true" onclick="removeItem('bll/adminuser.ashx')">  
  147.                             删除</a>  
  148.             </div>  
  149.             <div id="dlg" class="easyui-dialog" style="width: 620px; height: 400px; padding: 10px 20px"  
  150.                 closed="true" buttons="#dlg-buttons">  
  151.                 <div class="ftitle">  
  152.                     网站管理员设置</div>  
  153.                 <form id="fm" method="post">  
  154.                 <div class="fitem">  
  155.                     <label class="description">  
  156.                         用户名:</label>  
  157.                     <input name="userName" class="easyui-validatebox" required="true" />  
  158.                 </div>  
  159.                 <div class="fitem">  
  160.                     <label class="description">  
  161.                         密码:</label>  
  162.                     <input name="passWord" class="easyui-validatebox" required="true" />  
  163.                 </div>  
  164.                 <div class="fitem">  
  165.                     <label class="description">  
  166.                         是否生效:</label>  
  167.                     <input name="effect" type="radio" value="True" />启用  
  168.                     <input name="effect" type="radio" value="False" />屏蔽  
  169.                 </div>  
  170.                 <div class="fitem">  
  171.                     <label class="description">  
  172.                         权限:</label>  
  173.                     <span id="span1" style="display: inline-block;"></span>  
  174.                 </div>  
  175.                 <div class="fitem">  
  176.                     <label class="description">  
  177.                         备注:</label>  
  178.                     <textarea rows="3" name="comment" style="width: 350px;" class="easyui-validatebox"></textarea>  
  179.                 </div>  
  180.                 </form>  
  181.             </div>  
  182.             <div id="dlg-buttons">  
  183.                 <a href="#" class="easyui-linkbutton" iconcls="icon-ok" onclick="saveItem()">保存</a>  
  184.                 <a href="#" class="easyui-linkbutton" iconcls="icon-cancel" onclick="javascript:$('#dlg').dialog('close')">  
  185.                     取消</a>  
  186.             </div>  
  187.         </div>  
  188.     </div>  
  189. </body>  
  190. </html>  



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值