提交 | 用户 | 时间
|
722af2
|
1 |
$(function() { |
X |
2 |
|
|
3 |
// init date tables |
|
4 |
var userListTable = $("#user_list").dataTable({ |
|
5 |
"deferRender": true, |
|
6 |
"processing" : true, |
|
7 |
"serverSide": true, |
|
8 |
"ajax": { |
|
9 |
url: base_url + "/user/pageList", |
|
10 |
type:"post", |
|
11 |
data : function ( d ) { |
|
12 |
var obj = {}; |
|
13 |
obj.username = $('#username').val(); |
|
14 |
obj.role = $('#role').val(); |
|
15 |
obj.start = d.start; |
|
16 |
obj.length = d.length; |
|
17 |
return obj; |
|
18 |
} |
|
19 |
}, |
|
20 |
"searching": false, |
|
21 |
"ordering": false, |
|
22 |
//"scrollX": true, // scroll x,close self-adaption |
|
23 |
"columns": [ |
|
24 |
{ |
|
25 |
"data": 'id', |
|
26 |
"visible" : false, |
|
27 |
"width":'10%' |
|
28 |
}, |
|
29 |
{ |
|
30 |
"data": 'username', |
|
31 |
"visible" : true, |
|
32 |
"width":'20%' |
|
33 |
}, |
|
34 |
{ |
|
35 |
"data": 'password', |
|
36 |
"visible" : false, |
|
37 |
"width":'20%', |
|
38 |
"render": function ( data, type, row ) { |
|
39 |
return '*********'; |
|
40 |
} |
|
41 |
}, |
|
42 |
{ |
|
43 |
"data": 'role', |
|
44 |
"visible" : true, |
|
45 |
"width":'10%', |
|
46 |
"render": function ( data, type, row ) { |
|
47 |
if (data == 1) { |
|
48 |
return I18n.user_role_admin |
|
49 |
} else { |
|
50 |
return I18n.user_role_normal |
|
51 |
} |
|
52 |
} |
|
53 |
}, |
|
54 |
{ |
|
55 |
"data": 'permission', |
|
56 |
"width":'10%', |
|
57 |
"visible" : false |
|
58 |
}, |
|
59 |
{ |
|
60 |
"data": I18n.system_opt , |
|
61 |
"width":'15%', |
|
62 |
"render": function ( data, type, row ) { |
|
63 |
return function(){ |
|
64 |
// html |
|
65 |
tableData['key'+row.id] = row; |
|
66 |
var html = '<p id="'+ row.id +'" >'+ |
|
67 |
'<button class="btn btn-warning btn-xs update" type="button">'+ I18n.system_opt_edit +'</button> '+ |
|
68 |
'<button class="btn btn-danger btn-xs delete" type="button">'+ I18n.system_opt_del +'</button> '+ |
|
69 |
'</p>'; |
|
70 |
|
|
71 |
return html; |
|
72 |
}; |
|
73 |
} |
|
74 |
} |
|
75 |
], |
|
76 |
"language" : { |
|
77 |
"sProcessing" : I18n.dataTable_sProcessing , |
|
78 |
"sLengthMenu" : I18n.dataTable_sLengthMenu , |
|
79 |
"sZeroRecords" : I18n.dataTable_sZeroRecords , |
|
80 |
"sInfo" : I18n.dataTable_sInfo , |
|
81 |
"sInfoEmpty" : I18n.dataTable_sInfoEmpty , |
|
82 |
"sInfoFiltered" : I18n.dataTable_sInfoFiltered , |
|
83 |
"sInfoPostFix" : "", |
|
84 |
"sSearch" : I18n.dataTable_sSearch , |
|
85 |
"sUrl" : "", |
|
86 |
"sEmptyTable" : I18n.dataTable_sEmptyTable , |
|
87 |
"sLoadingRecords" : I18n.dataTable_sLoadingRecords , |
|
88 |
"sInfoThousands" : ",", |
|
89 |
"oPaginate" : { |
|
90 |
"sFirst" : I18n.dataTable_sFirst , |
|
91 |
"sPrevious" : I18n.dataTable_sPrevious , |
|
92 |
"sNext" : I18n.dataTable_sNext , |
|
93 |
"sLast" : I18n.dataTable_sLast |
|
94 |
}, |
|
95 |
"oAria" : { |
|
96 |
"sSortAscending" : I18n.dataTable_sSortAscending , |
|
97 |
"sSortDescending" : I18n.dataTable_sSortDescending |
|
98 |
} |
|
99 |
} |
|
100 |
}); |
|
101 |
|
|
102 |
// table data |
|
103 |
var tableData = {}; |
|
104 |
|
|
105 |
// search btn |
|
106 |
$('#searchBtn').on('click', function(){ |
|
107 |
userListTable.fnDraw(); |
|
108 |
}); |
|
109 |
|
|
110 |
// job operate |
|
111 |
$("#user_list").on('click', '.delete',function() { |
|
112 |
var id = $(this).parent('p').attr("id"); |
|
113 |
|
|
114 |
layer.confirm( I18n.system_ok + I18n.system_opt_del + '?', { |
|
115 |
icon: 3, |
|
116 |
title: I18n.system_tips , |
|
117 |
btn: [ I18n.system_ok, I18n.system_cancel ] |
|
118 |
}, function(index){ |
|
119 |
layer.close(index); |
|
120 |
|
|
121 |
$.ajax({ |
|
122 |
type : 'POST', |
|
123 |
url : base_url + "/user/remove", |
|
124 |
data : { |
|
125 |
"id" : id |
|
126 |
}, |
|
127 |
dataType : "json", |
|
128 |
success : function(data){ |
|
129 |
if (data.code == 200) { |
|
130 |
layer.msg( I18n.system_success ); |
|
131 |
userListTable.fnDraw(false); |
|
132 |
} else { |
|
133 |
layer.msg( data.msg || I18n.system_opt_del + I18n.system_fail ); |
|
134 |
} |
|
135 |
} |
|
136 |
}); |
|
137 |
}); |
|
138 |
}); |
|
139 |
|
|
140 |
// add role |
|
141 |
$("#addModal .form input[name=role]").change(function () { |
|
142 |
var role = $(this).val(); |
|
143 |
if (role == 1) { |
|
144 |
$("#addModal .form input[name=permission]").parents('.form-group').hide(); |
|
145 |
} else { |
|
146 |
$("#addModal .form input[name=permission]").parents('.form-group').show(); |
|
147 |
} |
|
148 |
$("#addModal .form input[name='permission']").prop("checked",false); |
|
149 |
}); |
|
150 |
|
|
151 |
jQuery.validator.addMethod("myValid01", function(value, element) { |
|
152 |
var length = value.length; |
|
153 |
var valid = /^[a-z][a-z0-9]*$/; |
|
154 |
return this.optional(element) || valid.test(value); |
|
155 |
}, I18n.user_username_valid ); |
|
156 |
|
|
157 |
// add |
|
158 |
$(".add").click(function(){ |
|
159 |
$('#addModal').modal({backdrop: false, keyboard: false}).modal('show'); |
|
160 |
}); |
|
161 |
var addModalValidate = $("#addModal .form").validate({ |
|
162 |
errorElement : 'span', |
|
163 |
errorClass : 'help-block', |
|
164 |
focusInvalid : true, |
|
165 |
rules : { |
|
166 |
username : { |
|
167 |
required : true, |
|
168 |
rangelength:[4, 20], |
|
169 |
myValid01: true |
|
170 |
}, |
|
171 |
password : { |
|
172 |
required : true, |
|
173 |
rangelength:[4, 20] |
|
174 |
} |
|
175 |
}, |
|
176 |
messages : { |
|
177 |
username : { |
|
178 |
required : I18n.system_please_input + I18n.user_username, |
|
179 |
rangelength: I18n.system_lengh_limit + "[4-20]" |
|
180 |
}, |
|
181 |
password : { |
|
182 |
required : I18n.system_please_input + I18n.user_password, |
|
183 |
rangelength: I18n.system_lengh_limit + "[4-20]" |
|
184 |
} |
|
185 |
}, |
|
186 |
highlight : function(element) { |
|
187 |
$(element).closest('.form-group').addClass('has-error'); |
|
188 |
}, |
|
189 |
success : function(label) { |
|
190 |
label.closest('.form-group').removeClass('has-error'); |
|
191 |
label.remove(); |
|
192 |
}, |
|
193 |
errorPlacement : function(error, element) { |
|
194 |
element.parent('div').append(error); |
|
195 |
}, |
|
196 |
submitHandler : function(form) { |
|
197 |
|
|
198 |
var permissionArr = []; |
|
199 |
$("#addModal .form input[name=permission]:checked").each(function(){ |
|
200 |
permissionArr.push($(this).val()); |
|
201 |
}); |
|
202 |
|
|
203 |
var paramData = { |
|
204 |
"username": $("#addModal .form input[name=username]").val(), |
|
205 |
"password": $("#addModal .form input[name=password]").val(), |
|
206 |
"role": $("#addModal .form input[name=role]:checked").val(), |
|
207 |
"permission": permissionArr.join(',') |
|
208 |
}; |
|
209 |
|
|
210 |
$.post(base_url + "/user/add", paramData, function(data, status) { |
|
211 |
if (data.code == "200") { |
|
212 |
$('#addModal').modal('hide'); |
|
213 |
|
|
214 |
layer.msg( I18n.system_add_suc ); |
|
215 |
userListTable.fnDraw(); |
|
216 |
} else { |
|
217 |
layer.open({ |
|
218 |
title: I18n.system_tips , |
|
219 |
btn: [ I18n.system_ok ], |
|
220 |
content: (data.msg || I18n.system_add_fail), |
|
221 |
icon: '2' |
|
222 |
}); |
|
223 |
} |
|
224 |
}); |
|
225 |
} |
|
226 |
}); |
|
227 |
$("#addModal").on('hide.bs.modal', function () { |
|
228 |
$("#addModal .form")[0].reset(); |
|
229 |
addModalValidate.resetForm(); |
|
230 |
$("#addModal .form .form-group").removeClass("has-error"); |
|
231 |
$(".remote_panel").show(); // remote |
|
232 |
|
|
233 |
$("#addModal .form input[name=permission]").parents('.form-group').show(); |
|
234 |
}); |
|
235 |
|
|
236 |
// update role |
|
237 |
$("#updateModal .form input[name=role]").change(function () { |
|
238 |
var role = $(this).val(); |
|
239 |
if (role == 1) { |
|
240 |
$("#updateModal .form input[name=permission]").parents('.form-group').hide(); |
|
241 |
} else { |
|
242 |
$("#updateModal .form input[name=permission]").parents('.form-group').show(); |
|
243 |
} |
|
244 |
$("#updateModal .form input[name='permission']").prop("checked",false); |
|
245 |
}); |
|
246 |
|
|
247 |
// update |
|
248 |
$("#user_list").on('click', '.update',function() { |
|
249 |
|
|
250 |
var id = $(this).parent('p').attr("id"); |
|
251 |
var row = tableData['key'+id]; |
|
252 |
|
|
253 |
// base data |
|
254 |
$("#updateModal .form input[name='id']").val( row.id ); |
|
255 |
$("#updateModal .form input[name='username']").val( row.username ); |
|
256 |
$("#updateModal .form input[name='password']").val( '' ); |
|
257 |
$("#updateModal .form input[name='role'][value='"+ row.role +"']").click(); |
|
258 |
var permissionArr = []; |
|
259 |
if (row.permission) { |
|
260 |
permissionArr = row.permission.split(","); |
|
261 |
} |
|
262 |
$("#updateModal .form input[name='permission']").each(function () { |
|
263 |
if($.inArray($(this).val(), permissionArr) > -1) { |
|
264 |
$(this).prop("checked",true); |
|
265 |
} else { |
|
266 |
$(this).prop("checked",false); |
|
267 |
} |
|
268 |
}); |
|
269 |
|
|
270 |
// show |
|
271 |
$('#updateModal').modal({backdrop: false, keyboard: false}).modal('show'); |
|
272 |
}); |
|
273 |
var updateModalValidate = $("#updateModal .form").validate({ |
|
274 |
errorElement : 'span', |
|
275 |
errorClass : 'help-block', |
|
276 |
focusInvalid : true, |
|
277 |
highlight : function(element) { |
|
278 |
$(element).closest('.form-group').addClass('has-error'); |
|
279 |
}, |
|
280 |
success : function(label) { |
|
281 |
label.closest('.form-group').removeClass('has-error'); |
|
282 |
label.remove(); |
|
283 |
}, |
|
284 |
errorPlacement : function(error, element) { |
|
285 |
element.parent('div').append(error); |
|
286 |
}, |
|
287 |
submitHandler : function(form) { |
|
288 |
|
|
289 |
var permissionArr =[]; |
|
290 |
$("#updateModal .form input[name=permission]:checked").each(function(){ |
|
291 |
permissionArr.push($(this).val()); |
|
292 |
}); |
|
293 |
|
|
294 |
var paramData = { |
|
295 |
"id": $("#updateModal .form input[name=id]").val(), |
|
296 |
"username": $("#updateModal .form input[name=username]").val(), |
|
297 |
"password": $("#updateModal .form input[name=password]").val(), |
|
298 |
"role": $("#updateModal .form input[name=role]:checked").val(), |
|
299 |
"permission": permissionArr.join(',') |
|
300 |
}; |
|
301 |
|
|
302 |
$.post(base_url + "/user/update", paramData, function(data, status) { |
|
303 |
if (data.code == "200") { |
|
304 |
$('#updateModal').modal('hide'); |
|
305 |
|
|
306 |
layer.msg( I18n.system_update_suc ); |
|
307 |
userListTable.fnDraw(); |
|
308 |
} else { |
|
309 |
layer.open({ |
|
310 |
title: I18n.system_tips , |
|
311 |
btn: [ I18n.system_ok ], |
|
312 |
content: (data.msg || I18n.system_update_fail), |
|
313 |
icon: '2' |
|
314 |
}); |
|
315 |
} |
|
316 |
}); |
|
317 |
} |
|
318 |
}); |
|
319 |
$("#updateModal").on('hide.bs.modal', function () { |
|
320 |
$("#updateModal .form")[0].reset(); |
|
321 |
updateModalValidate.resetForm(); |
|
322 |
$("#updateModal .form .form-group").removeClass("has-error"); |
|
323 |
$(".remote_panel").show(); // remote |
|
324 |
|
|
325 |
$("#updateModal .form input[name=permission]").parents('.form-group').show(); |
|
326 |
}); |
|
327 |
|
|
328 |
}); |