-
Notifications
You must be signed in to change notification settings - Fork 1
/
Back.Util.js
325 lines (299 loc) · 10.3 KB
/
Back.Util.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
//公用后台工具
var SimpleCommonUtil = {
//设置select中文本为text的项被选中
setSelectTextCheck: function (elems, text) {
elems.each(function () {
var count = $(this).find('option').length;
for (var i = 0; i < count; i++) {
if ($(this).get(0).options[i].text == text) {
var tempOption = $(this).get(0).options[i];
var option0 = $(this).get(0).options[0];
var optionText = option0.text;
var optionValue = option0.value;
option0.text = tempOption.text;
option0.value = tempOption.value;
tempOption.text = optionText;
tempOption.value = optionValue;
option0.selected = true;
break;
}
}
});
},
//判断select中是否存在值为value的项
isExistOption: function (id, value) {
var isExist = false;
var count = $('#' + id).find('option').length;
for (var i = 0; i < count; i++) {
if ($('#' + id).get(0).options[i].value == value) {
isExist = true;
break;
}
}
return isExist;
},
//点击按钮触发一处页面元素联动的逻辑
clickToggle: function (elem, callback) {
var isVisible = $(elem).is(':visible');
if (!isVisible) {
if ($.isFunction(callback)) {
callback.apply(this);
}
} else {
$(elem).html('');
}
$(elem).toggle();
},
//通用数据提交回传处理
AjaxSubmitDataConfig: {
callback: false,
errorMsg: "操作失败",
succMsg: "操作成功",
isShowSuccMsg: true
},
//通用ajax提交回传数据处理
ajaxSubmitDataProcess: function (data, config) {
config = $.extend({}, SimpleCommonUtil.AjaxSubmitDataConfig, config);
var obj = $.parseJSON(data);
if (!!obj) {
if (obj.errorCode == 0) {
if (config.isShowSuccMsg) {
if (obj.errorMsg != '') {
alert(obj.errorMsg);
} else {
alert(config.succMsg);
}
}
if ($.isFunction(config.callback)) {
config.callback.apply(this);
}
} else {
if (obj.errorMsg != '') {
alert(obj.errorMsg);
} else {
alert(config.errorMsg);
}
}
}
},
//ajax填充下拉框默认配置
AjaxboxConfig: {
url: '',
postData: {},
dataFormat: '.data',
defaultValue: { name: '请选择', value: '-1' },
itemFormat: { name: 'name', value: 'value' },
isSimpleFormat: false,
userDefaultValue: false,
errorCallback: false,
successCallback: false,
errorMsg: '后台数据出错'
},
//ajax方式填充单选框
ajaxFillBox: function (target, config) {
//判断传入的selectbox的合法性
if (!target || !target.length) {
return;
}
//应用默认配置
config = $.extend({}, SimpleCommonUtil.AjaxboxConfig, config);
//ajax请求
$.ajax({
type: 'post',
url: config.url,
data: config.postData,
async: false,
success: function (msg) {
var obj;
try {
obj = $.parseJSON(msg);
} catch (e) {
alert(config.errorMsg);
return;
}
if (obj) {
if (obj.errorCode != 0 && obj.errorMsg != '') {
alert(obj.errorMsg);
return;
}
//清空原表格
target.empty();
if (!!config.userDefaultValue) {
target.append("<option selected='selected' value='" + config.defaultValue.value + "'>" + config.defaultValue.name + "</option>");
}
//获取填充数据
var values = eval('obj' + config.dataFormat);
if (!!values) {
//使用简单方式填充
if (config.isSimpleFormat) {
$.each(values, function (idx, item) {
target.append("<option value='" + idx + "'>" + item + "</option>");
});
//需要自己指定填充格式
} else {
$.each(values, function (idx, item) {
target.append("<option value='" + item[config.itemFormat.value] + "'>" + item[config.itemFormat.name] + "</option>");
});
}
if ($.isFunction(config.successCallback)) {
config.successCallback(obj);
}
}
}
},
//错误处理
error: function () {
if ($.isFunction(config.errorCallback)) {
config.errorCallback.apply(this);
}
}
});
},
//ajax方式上传文件,url:后台地址,fileElemId:控件id,successCallback:上传成功后触发事件
fileUpload: function (url, fileElemId, successCallback) {
$.ajaxFileUpload({
url: url,
fileElementId: 'fileUpload',
dataType: 'string',
success: function (msg, status) {
successCallback(msg, status);
}
});
},
//获取url参数
getQueryString: function (name) {
var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
var r = window.location.search.substr(1).match(reg);
if (r != null) return unescape(r[2]); return '';
},
//获取当前日期
currentDate: function (date) {
var now = date || new Date();
var year = now.getFullYear(); //年
var month = now.getMonth() + 1; //月
var day = now.getDate(); //日
var clock = year + "-";
if (month < 10)
clock += "0";
clock += month + "-";
if (day < 10)
clock += "0";
clock += day;
return (clock);
},
//获取当前时间
curentTime: function (isUseDate) {
var now = new Date();
var hh = now.getHours(); //时
var mm = now.getMinutes(); //分
var clock = '';
if (hh < 10)
clock += "0";
clock += hh + ":";
if (mm < 10) clock += '0';
clock += mm;
if (isUseDate) {
clock = SimpleCommonUtil.CurrentDate() + ' ' + clock;
}
return (clock);
},
//设置页面标题
setPageTitle: function (title, context) {
var curContext = context || window.parent;
curContext.document.title = title;
}
};
//form表单工具类
var SimpleFormUtil = {
//form表单进行导出excel,适合post方式传递的excel导出
formDownloadExtend: function (data, formName, action) {
var theForm = document.forms[formName];
if (!theForm) {
theForm = eval('document.' + formName);
}
var jForm = $("form[name='" + formName + "']");
jForm.empty();
if (!!action) {
jForm.attr("action", action);
}
$.each(data, function (idx, elem) {
jForm.append("<input type='hidden' name='" + idx + "' value='" + elem + "'/>");
});
theForm.submit();
},
//form表单进行导出excel,适合post方式传递的excel导出
formDownload: function (data, formName) {
var theForm = document.forms[formName];
if (!theForm) {
theForm = eval('document.' + formName);
}
$.each(data, function (idx, elem) {
var viewElem = eval('theForm.' + elem.name);
viewElem.value = elem.value;
});
theForm.submit();
}
};
//flexgrid表格工具类
var FlexGridUtil = {
toSubmitData: function (data) {
if (!data) return [];
var result = [];
$.each(data, function (idx, item) {
result.push({ name: idx, value: item });
});
return result;
}
};
//页面渲染工具类
var SimplePageRenderUtil = {
//渲染动态页面
renderDynamicView: function (obj) {
$.each(obj, function (key, value) {
var tempObj = $("#" + key);
var tagName = tempObj[0].tagName;
if (tagName) tagName = tagName.toLowerCase();
if (tagName == 'input') {
tempObj.val(value);
} else if (tagName == 'select') {
if (SimpleCommonUtil.isExistOption(key, value)) {
tempObj.val(value);
}
}
else if (tagName == 'a') {
tempObj.attr("href", value);
} else if (tagName == 'span') {
tempObj.text(value);
} else {
tempObj.html(value);
}
});
},
//渲染flexgrid
renderFlexgridSimple: function (colModel, data) {
var a = [];
var b = [];
var trRows = [];
var thLength = colModel.length;
var tabContent = '<table class="table table-striped"><tr>';
for (var i = 0; i < thLength; i++) {
tabContent += '<th>' + colModel[i].display + '</th>';
};
tabContent += '</tr>';
$.each(data.rows, function (i, row) {
b[0] = "<tr";
if (((i & 1) == 0)) {
b[1] = " style='background:#c1c2b2'>";
} else {
b[1] = ">";
}
for (var idx = 0; idx < thLength; idx++) {
a[0] = '<td><div>', a[1] = row.cell[idx], a[2] = '</div></td>';
b[2 + idx] = (a.join(""));
}
b[2 + idx] = "</tr>";
trRows.push(b.join(""));
});
return tabContent + trRows.join("") + '</table>';
}
};