使用Jquery+EasyUI 進行框架項目開發案例講解之三 角色管理源碼分享

    在上兩篇文章

   《使用Jquery+EasyUI進行框架項目開發案例講解之一---員工管理源碼分享》 

  《使用Jquery+EasyUI 進行框架項目開發案例講解之二---用戶管理源碼分享》

  我們分享了使用Jquery EasyUI來進行UI佈局等開發的相關方法,也許你在使用EasyUI的過程過更熟練,有更方便快捷的技巧,我強烈建議你可以分享出來,大家共同進步、共同學習,謝謝!

  接下來我分享「角色管理」模塊主要的核心代碼,角色管理主界面如下圖所示:

  首先是角色管理的UI界面aspx代碼如下: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<%@ Page Language="C#" MasterPageFile="~/Site.Master" AutoEventWireup="true" CodeBehind="RoleAdmin.aspx.cs" Inherits="RDIFramework.WebApp.Modules.RoleAdmin" %>
< asp:Content  ID="Content1" ContentPlaceHolderID="head" runat="server">
< script  src="../Scripts/jquery-checkbox.js" type="text/javascript"></ script >
< script  src="../Scripts/jQuery.Select.js" type="text/javascript"></ script >
< script  src="../Scripts/jquery.easyListSplitter.js" type="text/javascript"></ script >
< script  src="../Scripts/Business/RoleAdmin.js" type="text/javascript"></ script >
</ asp:Content >
 
< asp:Content  ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
< div  class="toolbar">
     <%=base.BuildToolBarButtons() %>   角色分類:  < input  id="txtCategory" name=Category type="text" style="width:200px" class="txt03" />  < a  id="a_search" class="easyui-linkbutton" plain="true" icon="icon-search">查詢</ a >
</ div >
< div  id="scolldiv" style="width:100%;overflow:auto;">
     < input  id="hidrolecategory" type="hidden" value="" runat="server" />
  < asp:GridView  ID="gvRoleList" runat="server" AutoGenerateColumns="False" CssClass="grid2" Width="100%">
         < Columns >       
         < asp:TemplateField  ItemStyle-Width="1px"  ItemStyle-HorizontalAlign="Center">
             < ItemTemplate >
                 < input  type="hidden" value="<%#Eval("Id") %>" />              
             </ ItemTemplate >
         </ asp:TemplateField >
         <%--< asp:TemplateField  ItemStyle-Width="20px"  ItemStyle-HorizontalAlign="Center" >           
             < ItemTemplate >               
                 <%#Container.DataItemIndex+1 %>               
             </ ItemTemplate >       
         </ asp:TemplateField >  --%>
             < asp:BoundField  DataField="Code" HeaderText="角色編號" ItemStyle-Width = "120px"/>          
             < asp:BoundField  DataField="RealName" HeaderText="角色名稱"  ItemStyle-Width = "150px"/>           
             < asp:BoundField  DataField="Enabled" HeaderText="有效" ItemStyle-Width = "80px"/>         
             < asp:BoundField  DataField="Description" HeaderText="描述"/>
             < asp:TemplateField  HeaderText="管理" ItemStyle-Width="320px" ItemStyle-HorizontalAlign="Center">
                 < ItemTemplate >
                     < a  href="#" rel="edit" rid="<%#Eval("Id")%>"  <%if(!permissionEdit) {Response.Write("disabled");}%>   roleCategory ="<%#Eval("Category") %>" title="編輯當前角色">< span  class="icon icon-group_edit"> </ span >[修改]</ a
                     < a  style="cursor: pointer;" rel="delete"  rid="<%#Eval("Id") %>"  <%if(permissionDelete != true) {Response.Write("disabled");}%>   title="刪除當前角色"  >< span  class="icon icon-group_delete"> </ span >[刪除]</ a
                     < a  style="cursor: pointer;" rel="setuser"  rid="<%#Eval("Id") %>"  <%if(!permissionRoleUser) {Response.Write("disabled");}%>   title="設置當前角色所擁有的用戶">< span  class="icon icon-group_link"> </ span >[用戶]</ a >
                 </ ItemTemplate >
             </ asp:TemplateField >
         </ Columns >
     </ asp:GridView >
</ div >
 
< div  id="w"></ div >< div  id="d"></ div >
< script  type="text/javascript">
     $(document).ready(function () {
         // 瀏覽器的高度和div的高度 
         var height = $(window).height();
         var divHeight = $("#scolldiv").height();
 
         //獲取div對象 
         var divh = $("#scolldiv").get(0);
 
         //div高度大於屏幕高度把屏幕高度賦給div,並出現滾動條 
         if (divHeight > height - 40) {
             divh.style.height = height - 40;
             divh.style.overflow = "auto";
         }
 
         $('#txtCategory').combobox({
             url: 'handler/DataItemAdminHandler.ashx?action=GetCategory&categorycode=RoleCategory',
             method: 'get',
             valueField: 'ItemValue',
             textField: 'ItemName',
             editable: false,
             panelHeight: 'auto'
         });
     }); 
</ script >
</ asp:Content

  綁定當前登錄用戶所擁有的功能按鈕列表代碼如下:  

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
/// <summary>
/// 獲得頁面的權限
/// </summary>
private  void  GetPermission()
{          
     this .permissionAdd =  this .IsAuthorized( "RoleManagement.Add" );
     this .permissionEdit =  this .IsAuthorized( "RoleManagement.Edit" );
     this .permissionExport =  this .IsAuthorized( "RoleManagement.Export" );
     this .permissionDelete =  this .IsAuthorized( "RoleManagement.Delete" );
     this .permissionRoleUser =  this .IsAuthorized( "RoleManagement.RoleUser" );
}
 
/// <summary>
/// 加載工具欄
/// </summary>
/// <returns>工具欄HTML</returns>
public  override  string  BuildToolBarButtons()
{
     StringBuilder sb =  new  StringBuilder();
     string  linkbtn_template =  "<a id=\"a_{0}\" class=\"easyui-linkbutton\" style=\"float:left\"  plain=\"true\" href=\"javascript:;\" icon=\"{1}\"  {2} title=\"{3}\">{4}</a>" ;
     sb.Append( "<a id=\"a_refresh\" class=\"easyui-linkbutton\" style=\"float:left\"  plain=\"true\" href=\"javascript:;\" icon=\"icon-reload\"  title=\"重新加載\">刷新</a> " );
     sb.Append( "<div class='datagrid-btn-separator'></div> " );
     sb.Append( string .Format(linkbtn_template,  "add" "icon-group-add" , permissionAdd ?  ""  "disabled=\"True\"" "新增角色" "新增" ));
     sb.Append( string .Format(linkbtn_template,  "edit" "icon-group_edit" , permissionEdit ?  ""  "disabled=\"True\"" "修改選中角色" "修改" ));
     sb.Append( string .Format(linkbtn_template,  "del" "icon-group_delete" , permissionDelete ?  ""  "disabled=\"True\"" "刪除選中角色" "刪除" ));
     sb.Append( "<div class='datagrid-btn-separator'></div> " );
     sb.Append( string .Format(linkbtn_template,  "roleuser" "icon-group_link" , permissionRoleUser ?  ""  "disabled=\"True\"" "設置當前角色擁有的用戶" "用戶" ));
     return  sb.ToString();
}
 
private  void  InitGrid()
{   
     this .DTRole =  base .GetRoleScope( this .PermissionItemCode);
     this .gvRoleList.DataSource =  this .DTRole;
     this .gvRoleList.DataBind();

  核心業務邏輯完整JS代碼如下: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
$( function  () {
     addRole();
     editRole();
     delRole();
 
     $( '#a_edit' ).click( function  () {
         if  ($( this ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  i = $( 'table' ).data( 'tr_index' );
         if  (i > -1)
             $( '.grid2 tr' ).eq(i).find( "a[rel='edit']" ).click();
         else
             top.$( '#notity' ).jnotifyAddMessage({ text:  '請選擇要編輯的數據.' , permanent:  false , type:  'warning'  });
     });
 
     $( '#a_del' ).click( function  () {
         if  ($( this ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  i = $( 'table' ).data( 'tr_index' );
         if  (i > -1)
             $( '.grid2 tr' ).eq(i).find( "a[rel='delete']" ).click();
         else
             top.$( '#notity' ).jnotifyAddMessage({ text:  '請選擇要刪除的數據.' , permanent:  false , type:  'warning'  });
     });
 
     $( '#a_roleuser' ).click( function  () {
         if  ($( this ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  i = $( 'table' ).data( 'tr_index' );
         if  (i > -1)
             $( '.grid2 tr' ).eq(i).find( "a[rel='setuser']" ).click();
         else
             top.$( '#notity' ).jnotifyAddMessage({ text:  '請選擇要添加用戶的角色.' , permanent:  false , type:  'warning'  });
     });
 
     $( '#a_refresh' ).click( function  () {
         window.location =  'RoleAdmin.aspx' ;
     });
 
     using( 'linkbutton' function  () {
         $( '#a_roleuser' ).linkbutton({ text:  "成員"  });
     });
 
     accreditUsers();  //授權角色包含的用戶
     searchUser();
 
     $( '#txtCategory' ).combobox({
         onChange:  function  (newValue, oldValue) {
             $( "#hidrolecategory" ).val(newValue)
         }
     })
});
 
function  scripthtmlStr() {
     var  html =  '<form id="uiform"><table cellpadding=5 cellspacing=0 width=100% align="center" class="grid" border=0><tr><td align="right">'
     html +=  '角色編號:</td><td align="left"><input id="txtCode"  name=Code type="text"  required="true" style="width:280px" class="txt03"/></td></tr><tr><td align="right">' ;
     html +=  '角色名稱:</td><td align="left"><input id="txtRealName"  name=RealName type="text"  required="true" style="width:280px" class="txt03"/></td></tr><tr><td align="right">' ;
     html +=  '角色分類:</td><td align="left"><input id="txtCategory" name=Category type="text"  required="true" style="width:280px" class="txt03" /></td></tr><tr><td align="right">' ;
     html +=  '有效性:</td><td align="left"><input id="chkEnabled" type="checkbox" name="Enabled" /><label>有效</label>  <span style="color:#666;padding-left:20px;">注:選中則啓用該角色。</span></td></tr><tr><td align="right"> ' ;
     html +=  '角色描述:</td><td align="left"><textarea rows="3" id="txtDescription"  name=Description style="width:280px;height:50px;" class="txt03"/></td></tr>' ;
     html +=  '</table></form>' ;
     return  html;
}
 
function  searchUser() {
     $( '#a_search' ).click( function  () {
         var  vValue = $( '#txtCategory' ).combobox( 'getValue' ) +  '|'  + $( '#txtCategory' ).combobox( 'getText' );
         top.$( '#notity' ).jnotifyAddMessage({ text: vValue, permanent:  false  });
     });
}
 
function  addRole() {
     $( '#a_add' ).click( function  () {
         if  ($( this ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         top.$( '#w' ).hWindow({ title:  '新增角色' , iconCls:  'icon-group-add' , width: 450, height: 320, html: scripthtmlStr(), submit:  function  () {
             top.$( '#txtCode' ).validatebox();
             top.$( '#txtRealName' ).validatebox();
             top.$( '#txtCategory' ).validatebox();
 
             var  $txtrolecode = top.$( '#txtCode' );
             var  $txtrolename = top.$( '#txtRealName' );
             var  enabledSelected = top.$( "#chkEnabled" ).attr( "checked" );
             var  $txtdescription = top.$( '#txtDescription' );
             var  vcategory = top.$( '#txtCategory' ).combobox( 'getValue' );
 
             if  ($txtrolename.val() !=  '' ) {
                 $.ajaxtext( 'handler/RoleAdminHandler.ashx?t='  + Math.random(),  'action=add&rolecode='  + $txtrolecode.val() +  '&rolename='  + $txtrolename.val() +  '&category='  + vcategory +  '&enabled='  + enabledSelected +  '&description='  + $txtdescription.val(),  function  (msg) {
                     if  (msg ==  '1' ) {
                         top.$( '#notity' ).jnotifyAddMessage({ text:  '新增角色成功。' , permanent:  false  });
                         top.$( '#w' ).window( 'close' );
                         window.location =  'RoleAdmin.aspx' ;
                     else  {
                         alert(msg);
                         return  false ;
                     }
                 })
 
             else  {
                 top.$( '#notity' ).jnotifyAddMessage({ text:  '請輸入角色名稱.' , permanent:  false  });
                 top.$( '#txtRealName' ).focus();
             }
             return  false ;
         }
         });
         bindCategory();
         top.$( '#txtCode' ).focus();
         top.$( '#chkEnabled' ).attr( "checked" true );
     });
}
 
function  editRole() {
     $( "a[rel='edit']" ).click( function  () {
         if  ($( '#a_edit' ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  id = $( this ).attr( 'rid' );
         var  roleCategory = $( this ).attr( 'roleCategory' );
         var  tds = $( this ).parent().siblings();
         top.$( '#w' ).hWindow({ title:  '編輯角色信息' , iconCls:  'icon-group_edit' , width: 450, height: 320, html: scripthtmlStr(), submit:  function  () {
             var  $txtrolecode = top.$( '#txtCode' );
             var  $txtrealname = top.$( '#txtRealName' );
             var  enabledSelected = top.$( "#chkEnabled" ).attr( "checked" );
             var  $txtdescription = top.$( '#txtDescription' );
             var  vcategory = top.$( '#txtCategory' ).combobox( 'getValue' );
 
             if  ($txtrealname.validatebox( 'isValid' )) {
                 $.ajaxtext( 'handler/RoleAdminHandler.ashx?n='  + Math.random(),  'action=edit&rolecode='  + $txtrolecode.val() +  '&rolename='  + $txtrealname.val() +  '&category='  + vcategory +  '&enabled='  + enabledSelected +  '&description='  + $.trim($txtdescription.val()) +  '&roleid='  + id,
                 function  (msg) {
                     if  (msg ==  '1' ) {
                         top.$( '#notity' ).jnotifyAddMessage({ text:  '角色信息修改成功.' , permanent:  false  });
                         top.$( '#w' ).window( 'close' );
                         window.location =  'RoleAdmin.aspx' ;
                     else  {
                         alert(msg);
                         return  false ;
                     }
                 })
             }
         }
         });
 
         top.$( '#txtRealName' ).validatebox();
         top.$( '#txtCode' ).val(tds.eq(1).html().replace( ' ' '' ));
         top.$( '#txtRealName' ).val(tds.eq(2).html().replace( ' ' '' ));
         //top.$('#txtCategory').val(roleCategory);
         top.$( '#chkEnabled' ).attr( "checked" , tds.eq(3).html().replace( ' ' '' ) ==  "1" );
         top.$( '#txtDescription' ).val(tds.eq(4).html().replace( ' ' '' ));
         bindCategory();
         top.$( '#txtCategory' ).combobox( "setValue" , roleCategory);
     })
}
 
function  delRole() {
     $( "a[rel='delete']" ).click( function  () {
         if  ($( '#a_del' ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  rid = $( this ).attr( 'rid' );
         if  (rid !=  '' ) {
             $.messager.confirm( '詢問提示' '你確認刪除當前所選角色嗎?' function  (data) {
              if  (data) {   
                     $.ajax({
                         type:  'post' ,
                         url:  'handler/RoleAdminHandler.ashx?t=<%=Guid.NewGuid().ToString() %>' ,
                         data:  'action=delete&roleid='  + rid,
                         beforeSend:  function  () { $.showLoading(); },
                         complete:  function  () { $.hideLoading(); },
                         success:  function  (msg) {
                             if  (msg ==  '1' ) {
                                 top.$( '#notity' ).jnotifyAddMessage({ text:  '角色刪除成功.' , permanent:  false  });
                                 window.location =  'RoleAdmin.aspx' ;
                             }
                             else
                                 top.$( '#notity' ).jnotifyAddMessage({ text:  '角色刪除失敗.' , permanent:  false , type:  'warning'  });
                         }
                     });
                 }
             });
            
         }
         else  {
             top.$( '#notity' ).jnotifyAddMessage({ text:  '請選擇要刪除的角色。' , permanent:  false , type:  'warning'  });
         }
     });
}
 
function  bindCategory() {
     top.$( '#txtCategory' ).combobox({
         url:  'Modules/handler/DataItemAdminHandler.ashx?action=GetCategory&categorycode=RoleCategory' ,
         method:  'get' ,
         valueField:  'ItemValue' ,
         textField:  'ItemName' ,
         editable:  false ,
         panelHeight:  'auto'
     });
}
 
var  formHtml =  '<div style="border-bottom:1px solid #CCC; margin-bottom:5px;"><span id="role_name" class="icon32 icon-group32" style="padding-left:48px;font-weight:bold; font-size:14px;color:#666;">角色名稱</span> </div>' ;
     formHtml +=  '<div style=" margin-bottom:10px;">描述:<input id="txtroleremark" type="text" readonly=true class="txt02" style="width:400px;color:#666;"></div>' ;
     formHtml +=  '<div> 成員:</div>' ;
     formHtml +=  '<select id="user_groups" size="10" style="width:475px; line-height:30px;height:195px;padding:5px"></select>' ;
     formHtml +=  '<div style="margin-top:2px;"><a href="#" id="group_add" class="easyui-linkbutton" plain="true" iconCls="icon-group-add">添加</a>' ;
     formHtml +=  '<a href="#" class="easyui-linkbutton" id="group_delete" plain="true" iconCls="icon-group-delete">移除</a>' ;
     formHtml +=  '<a href="#" class="easyui-linkbutton" id="group_clear" plain="true" iconCls="icon-clear">清空</a></div>'
 
var  userList =  '<table id="sp" cellpadding=5 cellspacing=0 width=100% align="center" ><tr><td><ul class="ul_users checkbox"></ul></td></tr></table><p><input type="checkbox" id="chkall" ><label id="labchkall" for="chkall"><b>全選</b></label></p>' ;
//授權用戶
function  accreditUsers() {  
     $( "a[rel='setuser']" ).click( function  () {
         if  ($( '#a_roleuser' ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  i = $( 'table' ).data( 'tr_index' );
         if  (i == undefined) {
             top.$( '#notity' ).jnotifyAddMessage({ text:  '請選擇角色.' , permanent:  false , type:  'warning'  });
             return  false ;
         }
         var  tds = $( '.grid2 tr:eq('  + i +  ') td' );
         var  rid = tds.eq(0).find( ':hidden' ).val();  //rid = $(this).attr('rid');
 
         top.$( '#w' ).hWindow({ html: formHtml, width: 500, height: 400, title:  '角色用戶' , iconCls:  'icon-group_link' , submit:  function  () {
             var  users =  new  Array();
             var  count = top.$( '#user_groups' ).get(0).options.length;
             for  ( var  i = 0; i < count; i++) {
                 users.push(top.$( '#user_groups' ).get(0).options[i].value);
             }
 
             $.ajaxtext( 'handler/RoleAdminHandler.ashx' 'action=setusers&users='  + users +  '&roleid='  + rid,  function  (msg) {
                 if  (msg ==  "1" ) {
                     top.$( '#notity' ).jnotifyAddMessage({ text:  '設置成功。' , permanent:  false , type:  'message'  });
                 }
                 else
                     alert(msg);
 
                 top.$( '#w' ).window( 'close' );
             });
             return  false ;
         }
         });
 
         top.$( 'a.easyui-linkbutton' ).linkbutton({ disabled:  false  });
         top.$( '#role_name' ).text(tds.eq(2).text());
         top.$( '#txtroleremark' ).val(tds.eq(4).text());
 
         $.getJSON( 'handler/RoleAdminHandler.ashx?n='  + Math.random(),  '&action=usersinrole&roleid='  + rid,  function  (json) {
             $.each(json,  function  (i, n) {
                 top.$( '#user_groups' ).append( '<option value="'  + n.Id +  '">'  + n.UserName +  ' | '  + n.RealName +  '</option>' );
             });
         });
 
 
         top.$( '#group_add' ).click( function  () {
             top.$( '#d' ).hDialog({ html: userList, title:  '選取用戶' , iconCls:  'icon-users' , width: 800, height: 600,
                 submit:  function  () {
                     var  selectedUids =  '' ;
                     top.$( '#sp :checked' ).each( function  () {  //匹配所有sp中被選中的元素(checkbox中被選中的)
                         if  (top.$( this ).is( ':checked' ))
                             selectedUids += top.$( this ).val() +  ',' ;
                     });
                     if  (selectedUids !=  '' )
                         selectedUids = selectedUids.substr(0, selectedUids.length - 1);
 
                     if  (selectedUids.length == 0) {
                         $.messager.alert( '請至少選擇一個用戶!' , msg,  'warning' );
                         return  false ;
                     else  {
                         var  users = top.$( '#sp' ).data( 'allusers' );
                         var  selarr = getSelectedUsers(users, selectedUids);
                         top.$( '#user_groups' ).empty();
                         top.$( '#sp' ).removeData( 'allusers' );
                         $.each(selarr,  function  (i, n) {
                             top.$( '#user_groups' ).append( '<option value="'  + n.Id +  '">'  + n.UserName +  ' | '  + n.RealName +  '</option>' );
                         });
 
                         top.$( '#d' ).dialog( "close" );
                     }
                 }
             });
 
 
             var  lis =  '' ;
             $.getJSON( 'handler/UserAdminHandler.ashx?n='  + Math.random(),  '' function  (json) {
                 $.each(json,  function  (i, n) {
                     lis +=  '<li><input type="checkbox" value="'  + n.Id +  '" /><label>'  + n.UserName +  ' | '  + n.RealName +  '</label></li>' ;
                 });
                 top.$( '.ul_users' ).empty().append(lis);
                 top.$( '#sp' ).data( 'allusers' , json);
             });
 
 
 
             top.$( '#labchkall' ).click( function  () {
                 var  flag = $( this ).prev().is( ':checked' );
                 var  pers = $( this ).parent().parent().prev();
 
                 if  (!flag) {
                     top.$( ":checkbox" '#sp' ).attr( "checked" true );
                 }
                 else  {
                     top.$( ":checkbox" '#sp' ).attr( "checked" false );
                 }
             });
         });
 
         top.$( '#group_delete' ).click( function  () {
             var  i = top.$( '#user_groups' ).get(0).selectedIndex;
 
             if  (i > -1) {
                 var  uid = top.$( '#user_groups option:selected' ).val();  //選中的值或 top.$("#user_groups").find("option:selected").val();
                 var  uname = top.$( '#user_groups option:selected' ).text();  //選中的文本 或top.$("#user_groups").find("option:selected").text();
 
                 top.$( '#user_groups' ).get(0).remove(i);
                 $.ajaxtext( 'handler/RoleAdminHandler.ashx' 'action=removeuserfromrole&uid='  + uid +  '&roleid='  + rid,  function  (msg) {
                     if  (msg ==  "1" ) {
                         top.$( '#notity' ).jnotifyAddMessage({ text:  '移除成功。' , permanent:  false , type:  'message'  });
                     }
                     else  {
                         top.$.messager.alert( '提示信息' , msg,  'warning' );
                     }
                 });
             }
             else  {
                 top.$.messager.alert( "操作提示" "請選擇要移除的用戶!" "info" );
             }
         });
         top.$( '#group_clear' ).click( function  () {
//            var count = $("#user_groups option").length
//            if (count <= 0) {
//                top.$.messager.alert("操作提示", "當前角色沒有用戶!", "info");
//                return;
//            }
 
             top.$.messager.confirm( '詢問提示' '確認要清除所有用戶嗎?' function  (data) {
                 if  (data) {
                     top.$( '#user_groups' ).empty();
                 }
             });
             return  false ;
         });
     });
}
 
function  getSelectedUsers(users, selecedVals) {
     var  arrUserid = eval( '['  + selecedVals +  ']' );
     var  arr =  new  Array();
     $.each(users,  function  (i, n) {
         if  ($.inArray(n.Id, arrUserid) > -1)
             arr.push(n);
     });
     return  arr;
}

  角色添加界面如下:

 

   角色修改界面:

  

  角色用戶設置界面:

 

          在上圖中,我們可以添加、移除、清空當前角色包含的用戶。

     選擇添加,打開「選取用戶」窗口,如下圖所示:        

  角色用戶設置及用戶選擇界面JS代碼如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
var  formHtml =  '<div style="border-bottom:1px solid #CCC; margin-bottom:5px;"><span id="role_name" class="icon32 icon-group32" style="padding-left:48px;font-weight:bold; font-size:14px;color:#666;">角色名稱</span> </div>' ;
     formHtml +=  '<div style=" margin-bottom:10px;">描述:<input id="txtroleremark" type="text" readonly=true class="txt02" style="width:400px;color:#666;"></div>' ;
     formHtml +=  '<div> 成員:</div>' ;
     formHtml +=  '<select id="user_groups" size="10" style="width:475px; line-height:30px;height:195px;padding:5px"></select>' ;
     formHtml +=  '<div style="margin-top:2px;"><a href="#" id="group_add" class="easyui-linkbutton" plain="true" iconCls="icon-group-add">添加</a>' ;
     formHtml +=  '<a href="#" class="easyui-linkbutton" id="group_delete" plain="true" iconCls="icon-group-delete">移除</a>' ;
     formHtml +=  '<a href="#" class="easyui-linkbutton" id="group_clear" plain="true" iconCls="icon-clear">清空</a></div>'
 
var  userList =  '<table id="sp" cellpadding=5 cellspacing=0 width=100% align="center" ><tr><td><ul class="ul_users checkbox"></ul></td></tr></table><p><input type="checkbox" id="chkall" ><label id="labchkall" for="chkall"><b>全選</b></label></p>' ;
 
//授權用戶
function  accreditUsers() {  
     $( "a[rel='setuser']" ).click( function  () {
         if  ($( '#a_roleuser' ).linkbutton( 'options' ).disabled ==  true ) {
             return ;
         }
         var  i = $( 'table' ).data( 'tr_index' );
         if  (i == undefined) {
             top.$( '#notity' ).jnotifyAddMessage({ text:  '請選擇角色.' , permanent:  false , type:  'warning'  });
             return  false ;
         }
         var  tds = $( '.grid2 tr:eq('  + i +  ') td' );
         var  rid = tds.eq(0).find( ':hidden' ).val();  //rid = $(this).attr('rid');
 
         top.$( '#w' ).hWindow({ html: formHtml, width: 500, height: 400, title:  '角色用戶' , iconCls:  'icon-group_link' , submit:  function  () {
             var  users =  new  Array();
             var  count = top.$( '#user_groups' ).get(0).options.length;
             for  ( var  i = 0; i < count; i++) {
                 users.push(top.$( '#user_groups' ).get(0).options[i].value);
             }
 
             $.ajaxtext( 'handler/RoleAdminHandler.ashx' 'action=setusers&users='  + users +  '&roleid='  + rid,  function  (msg) {
                 if  (msg ==  "1" ) {
                     top.$( '#notity' ).jnotifyAddMessage({ text:  '設置成功。' , permanent:  false , type:  'message'  });
                 }
                 else
                     alert(msg);
 
                 top.$( '#w' ).window( 'close' );
             });
             return  false ;
         }
         });
 
         top.$( 'a.easyui-linkbutton' ).linkbutton({ disabled:  false  });
         top.$( '#role_name' ).text(tds.eq(2).text());
         top.$( '#txtroleremark' ).val(tds.eq(4).text());
 
         $.getJSON( 'handler/RoleAdminHandler.ashx?n='  + Math.random(),  '&action=usersinrole&roleid='  + rid,  function  (json) {
             $.each(json,  function  (i, n) {
                 top.$( '#user_groups' ).append( '<option value="'  + n.Id +  '">'  + n.UserName +  ' | '  + n.RealName +  '</option>' );
             });
         });
 
 
         top.$( '#group_add' ).click( function  () {
             top.$( '#d' ).hDialog({ html: userList, title:  '選取用戶' , iconCls:  'icon-users' , width: 800, height: 600,
                 submit:  function  () {
                     var  selectedUids =  '' ;
                     top.$( '#sp :checked' ).each( function  () {  //匹配所有sp中被選中的元素(checkbox中被選中的)
                         if  (top.$( this ).is( ':checked' ))
                             selectedUids += top.$( this ).val() +  ',' ;
                     });
                     if  (selectedUids !=  '' )
                         selectedUids = selectedUids.substr(0, selectedUids.length - 1);
 
                     if  (selectedUids.length == 0) {
                         $.messager.alert( '請至少選擇一個用戶!' , msg,  'warning' );
                         return  false ;
                     else  {
                         var  users = top.$( '#sp' ).data( 'allusers' );
                         var  selarr = getSelectedUsers(users, selectedUids);
                         top.$( '#user_groups' ).empty();
                         top.$( '#sp' ).removeData( 'allusers' );
                         $.each(selarr,  function  (i, n) {
                             top.$( '#user_groups' ).append( '<option value="'  + n.Id +  '">'  + n.UserName +  ' | '  + n.RealName +  '</option>' );
                         });
 
                         top.$( '#d' ).dialog( "close" );
                     }
                 }
             });
 
 
             var  lis =  '' ;
             $.getJSON( 'handler/UserAdminHandler.ashx?n='  + Math.random(),  '' function  (json) {
                 $.each(json,  function  (i, n) {
                     lis +=  '<li><input type="checkbox" value="'  + n.Id +  '" /><label>'  + n.UserName +  ' | '  + n.RealName +  '</label></li>' ;
                 });
                 top.$( '.ul_users' ).empty().append(lis);
                 top.$( '#sp' ).data( 'allusers' , json);
             });
 
 
 
             top.$( '#labchkall' ).click( function  () {
                 var  flag = $( this ).prev().is( ':checked' );
                 var  pers = $( this ).parent().parent().prev();
 
                 if  (!flag) {
                     top.$( ":checkbox" '#sp' ).attr( "checked" true );
                 }
                 else  {
                     top.$( ":checkbox" '#sp' ).attr( "checked" false );
                 }
             });
         });
 
         top.$( '#group_delete' ).click( function  () {
             var  i = top.$( '#user_groups' ).get(0).selectedIndex;
 
             if  (i > -1) {
                 var  uid = top.$( '#user_groups option:selected' ).val();  //選中的值或 top.$("#user_groups").find("option:selected").val();
                 var  uname = top.$( '#user_groups option:selected' ).text();  //選中的文本 或top.$("#user_groups").find("option:selected").text();
 
                 top.$( '#user_groups' ).get(0).remove(i);
                 $.ajaxtext( 'handler/RoleAdminHandler.ashx' 'action=removeuserfromrole&uid='  + uid +  '&roleid='  + rid,  function  (msg) {
                     if  (msg ==  "1" ) {
                         top.$( '#notity' ).jnotifyAddMessage({ text:  '移除成功。' , permanent:  false , type:  'message'  });
                     }
                     else  {
                         top.$.messager.alert( '提示信息' , msg,  'warning' );
                     }
                 });
             }
             else  {
                 top.$.messager.alert( "操作提示" "請選擇要移除的用戶!" "info" );
             }
         });
         top.$( '#group_clear' ).click( function  () {
//            var count = $("#user_groups option").length
//            if (count <= 0) {
//                top.$.messager.alert("操作提示", "當前角色沒有用戶!", "info");
//                return;
//            }
 
             top.$.messager.confirm( '詢問提示' '確認要清除所有用戶嗎?' function  (data) {
                 if  (data) {
                     top.$( '#user_groups' ).empty();
                 }
             });
             return  false ;
         });
     });
}

  特別說明一下,在選取用戶界面,使用的是checkbox控件,得到選中的checkbox,是使用下面的實例代碼:

1
2
< input  id="checkbox1" type="checkbox" checked>
< input  id="checkbox2" type="checkbox>

  

1
2
3
4
5
$( "#checkbox1" ).is( ":checked" // true
$( "#checkbox2" ).is( ":checked" // false
//注意不是使用下面的方法
相關文章
相關標籤/搜索