jquery.easyui.extension.js
11.3 KB
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
/*
说明:扩展EasyUI的验证规则
*/
$.extend($.fn.validatebox.defaults.rules, {
//验证身份证
idcard: {
validator: function(value) {
return /^\d{15}(\d{2}[A-Za-z0-9])?$/i.test(value);
},
message: '身份证号码格式不正确'
},
//最小长度
minLength: {
validator: function(value, param) {
return value.length >= param[0];
},
message: '请输入至少{0}个字符.'
},
maxLength: {
validator: function(value, param) {
return value.length <= param[0];
},
message: '最多只能输入{0}个字符'
},
phone: { // 验证电话号码
validator: function(value) {
return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
},
message: '格式不正确,请使用下面格式:020-88888888'
},
mobile: { // 验证手机号码
validator: function(value) {
return /^(13|15|18)\d{9}$/i.test(value);
},
message: '手机号码格式不正确'
},
currency: { // 验证货币
validator: function(value) {
return /^\d+(\.\d+)?$/i.test(value);
},
message: '货币格式不正确'
},
intOrFloat: { // 验证整数或小数
validator: function(value) {
return /^\d+(\.\d+)?$/i.test(value);
},
message: '请输入数字,并确保格式正确'
},
integer: { // 验证整数
validator: function(value) {
return /^[+]?[1-9]+\d*$/i.test(value);
},
message: '请输入整数'
},
age: { // 验证年龄
validator: function(value) {
return /^(?:[1-9][0-9]?|1[01][0-9]|120)$/i.test(value);
},
message: '年龄必须是0到120之间的整数'
},
chinese: { // 验证中文
validator: function(value) {
return /^[\Α-\¥]+$/i.test(value);
},
message: '请输入中文'
},
english: { // 验证英语
validator: function(value) {
return /^[A-Za-z]+$/i.test(value);
},
message: '请输入英文'
},
username: { // 验证用户名
validator: function(value) {
return /^[a-zA-Z][a-zA-Z0-9_]{5,15}$/i.test(value);
},
message: '用户名不合法(字母开头,允许6-16字节,允许字母数字下划线)'
},
faxno: { // 验证传真
validator: function(value) {
// return /^[+]{0,1}(\d){1,3}[ ]?([-]?((\d)|[ ]){1,12})+$/i.test(value);
return /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/i.test(value);
},
message: '传真号码不正确'
},
zip: { // 验证邮政编码
validator: function(value) {
return /^[1-9]\d{5}$/i.test(value);
},
message: '邮政编码格式不正确'
},
ip: { // 验证IP地址
validator: function(value) {
return /^(?:(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])\.){3}(?:25[0-5]|2[0-4][0-9]|1[0-9][0-9]|[1-9]?[0-9])$/.test(value);
},
message: 'IP地址格式不正确'
},
mac: {
validator: function(value) {
return /^([0-9a-fA-F]{2}-?){6}$/.test(value);
},
message: 'MAC地址不正确'
},
name: { // 验证姓名,可以是中文或英文
validator: function(value) {
return /^[\Α-\¥]+$/i.test(value) | /^\w+[\w\s]+\w+$/i.test(value);
},
message: '请输入姓名'
},
date: { // 验证姓名,可以是中文或英文
validator: function(value) {
//格式yyyy-MM-dd或yyyy-M-d
return /^(?:(?!0000)[0-9]{4}([-]?)(?:(?:0?[1-9]|1[0-2])\1(?:0?[1-9]|1[0-9]|2[0-8])|(?:0?[13-9]|1[0-2])\1(?:29|30)|(?:0?[13578]|1[02])\1(?:31))|(?:[0-9]{2}(?:0[48]|[2468][048]|[13579][26])|(?:0[48]|[2468][048]|[13579][26])00)([-]?)0?2\2(?:29))$/i.test(value);
},
message: '日期格式不正确'
},
equals: {
validator: function(value, param) {
if ($("#" + param[0]).val() != "" && value != "") {
return $("#" + param[0]).val() == value;
} else {
return true;
}
},
message: '两次输入不匹配'
},
regex: {
validator: function(value, param) {
var regex = new RegExp(param[0]);
return regex.test(value);
},
message: "格式不合法"
},
longitude: {
validator: function(value, param) {
var regex = /^[\-\+]?(0?\d{1,2}(\.\d{1,6})?|1[0-7]?\d{1}(\.\d{1,6})?|180(\.0{1,6})?)$/;
return regex.test(value);
},
message: "经度格式不合法"
},
latitude: {
validator: function(value, param) {
var regex = /^[\-\+]?([0-8]?\d{1}(\.\d{1,6})?|90(\.0{1,6})?)$/;
return regex.test(value);
},
message: "纬度格式不合法"
},
//不能为空,参数为不能取的值的数组 ["","-1","全部"]
notEmpty: {
validator: function(value, param) {
var isValidate = true;
for (var i = 0; i < param.length; i++) {
if (value == param[i]) {
return false;
}
}
return true;
},
message: "不能为空"
},
// 数值范围验证
range: {
validator: function(value, param) {
return value >= param[0] && value <= param[1];
},
message: "值必须在{0}与{1}之间"
}
});
//扩展Tab组件,添加遮罩
(function() {
$.extend($.fn.tabs.methods, {
//显示遮罩
loading: function(jq, msg) {
return jq.each(function() {
var panel = $(this).tabs("getSelected");
if (msg == undefined) {
msg = "正在加载数据,请稍候...";
}
$("<div class=\"datagrid-mask\"></div>").css({ display: "block", width: panel.width(), height: panel.height() + 30 }).appendTo(panel);
$("<div class=\"datagrid-mask-msg\"></div>").html(msg).appendTo(panel).css({ display: "block", left: (panel.width() - $("div.datagrid-mask-msg", panel).outerWidth()) / 2, top: (panel.height() - $("div.datagrid-mask-msg", panel).outerHeight()) / 2 });
});
},
//隐藏遮罩
loaded: function(jq) {
return jq.each(function() {
var panel = $(this).tabs("getSelected");
panel.find("div.datagrid-mask-msg").remove();
panel.find("div.datagrid-mask").remove();
});
}
});
})(jQuery);
// 扩展Datagrid 使用本地分页功能
(function($) {
function pagerFilter(data) {
if ($.isArray(data)) { // is array
data = {
total: data.length,
rows: data
}
}
var target = this;
var dg = $(target);
var state = dg.data('datagrid');
var opts = dg.datagrid('options');
if (!state.allRows) {
state.allRows = (data.rows);
}
if (!opts.remoteSort && opts.sortName) {
var names = opts.sortName.split(',');
var orders = opts.sortOrder.split(',');
state.allRows.sort(function(r1, r2) {
var r = 0;
for (var i = 0; i < names.length; i++) {
var sn = names[i];
var so = orders[i];
var col = $(target).datagrid('getColumnOption', sn);
var sortFunc = col.sorter || function(a, b) {
return a == b ? 0 : (a > b ? 1 : -1);
};
r = sortFunc(r1[sn], r2[sn]) * (so == 'asc' ? 1 : -1);
if (r != 0) {
return r;
}
}
return r;
});
}
var start = (opts.pageNumber - 1) * parseInt(opts.pageSize);
var end = start + parseInt(opts.pageSize);
data.rows = state.allRows.slice(start, end);
return data;
}
var loadDataMethod = $.fn.datagrid.methods.loadData;
var deleteRowMethod = $.fn.datagrid.methods.deleteRow;
$.extend($.fn.datagrid.methods, {
clientPaging: function(jq) {
return jq.each(function() {
var dg = $(this);
var state = dg.data('datagrid');
var opts = state.options;
opts.loadFilter = pagerFilter;
var onBeforeLoad = opts.onBeforeLoad;
opts.onBeforeLoad = function(param) {
state.allRows = null;
return onBeforeLoad.call(this, param);
}
var pager = dg.datagrid('getPager');
pager.pagination({
onSelectPage: function(pageNum, pageSize) {
opts.pageNumber = pageNum;
opts.pageSize = pageSize;
pager.pagination('refresh', {
pageNumber: pageNum,
pageSize: pageSize
});
dg.datagrid('loadData', state.allRows);
}
});
$(this).datagrid('loadData', state.data);
if (opts.url) {
$(this).datagrid('reload');
}
});
},
loadData: function(jq, data) {
jq.each(function() {
$(this).data('datagrid').allRows = null;
});
return loadDataMethod.call($.fn.datagrid.methods, jq, data);
},
deleteRow: function(jq, index) {
return jq.each(function() {
var row = $(this).datagrid('getRows')[index];
deleteRowMethod.call($.fn.datagrid.methods, $(this), index);
var state = $(this).data('datagrid');
if (state.options.loadFilter == pagerFilter) {
for (var i = 0; i < state.allRows.length; i++) {
if (state.allRows[i] == row) {
state.allRows.splice(i, 1);
break;
}
}
$(this).datagrid('loadData', state.allRows);
}
});
},
getAllRows: function(jq) {
return jq.data('datagrid').allRows;
}
})
})(jQuery);
// 扩展编辑器
$.extend($.fn.datagrid.defaults.editors, {
my97datepicker: {
init: function(container, options) {
var datepicker = $("<input class='Wdate' onclick='showDatePicker()' />").appendTo(container);
return input;
function showDatePicker() {
WdatePicker(options);
}
},
getValue: function(target) {
return $(target).val();
},
setValue: function(target, value) {
$(target).val(value);
},
resize: function(target, width) {
var input = $(target);
if ($.boxModel == true) {
input.width(width - (input.outerWidth() - input.width()));
} else {
input.width(width);
}
}
}
});