Skip to content

Commit 3d0eb2d

Browse files
committed
dialog表单绑定支持radio
1 parent 8cdc65a commit 3d0eb2d

File tree

5 files changed

+131
-95
lines changed

5 files changed

+131
-95
lines changed

resource/js/backend.js

+127-91
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,60 @@
1-
window.stop_ajax=false;
1+
window.stop_ajax = false;
22

3-
function radio_tab(radios,lists,prefix) {
4-
$(radios).on('change',function (e) {
5-
if(!$(this).is(':checked'))return;
6-
var curval=$(this).val();
3+
function radio_tab(radios, lists, prefix) {
4+
$(radios).on('change', function (e) {
5+
if (!$(this).is(':checked')) return;
6+
var curval = $(this).val();
77
$(lists).hide();
8-
$('.'+prefix+curval).show();
8+
$('.' + prefix + curval).show();
99
}).filter(':checked').trigger('change');
1010
}
1111

12+
1213
//绑定数据
13-
function bindData(body,data) {
14-
for(var i in data){
15-
body.find('[name='+i+']').val(data[i]);
14+
function bindData(body, data) {
15+
for (var i in data) {
16+
var field = body.find('[name=' + i + ']')
17+
if (field.attr('type') == 'radio') {
18+
field.each(function () {
19+
if ($(this).val() == data[i]) {
20+
$(this).prop('checked', true)
21+
}
22+
})
23+
} else if (field.attr('type') == 'checkbox') {
24+
field.each(function () {
25+
if (data[i].indexOf($(this).val()) > -1) {
26+
$(this).prop('checked', true)
27+
}
28+
})
29+
} else {
30+
body.find('[name=' + i + ']').val(data[i]);
31+
}
1632
}
1733
}
1834

1935
//获取表单数据
2036
function getData(body) {
21-
var data={};
22-
var fields=body.find('[name]');
23-
for(var i=0;i<fields.length;i++){
24-
data[fields.eq(i).attr('name')]=fields.eq(i).val();
37+
var data = {};
38+
var fields = body.find('[name]');
39+
for (var i = 0; i < fields.length; i++) {
40+
var field = fields.eq(i)
41+
if (field.attr('type') == 'radio') {
42+
field.each(function () {
43+
if ($(this).prop('checked')) {
44+
data[$(this).attr('name')] = $(this).val();
45+
}
46+
})
47+
} else if (field.attr('type') == 'checkbox') {
48+
var val = []
49+
field.each(function () {
50+
if ($(this).prop('checked')) {
51+
val.push($(this).val());
52+
}
53+
})
54+
data[field.attr('name')] = val
55+
} else {
56+
data[field.attr('name')] = field.val();
57+
}
2558
}
2659
return data;
2760
}
@@ -53,7 +86,7 @@ jQuery(function ($) {
5386
bread.html(html.join("\n"));
5487
}
5588

56-
$(window).on('scroll',function (e) {
89+
$(window).on('scroll', function (e) {
5790

5891
}).trigger('scroll');
5992

@@ -115,58 +148,58 @@ jQuery(function ($) {
115148
});
116149

117150
// 状态切换 input[hidden]
118-
$('.radiostatus').click(function(e){
151+
$('.radiostatus').click(function (e) {
119152
var init = $(this).data('init');
120153
var hid = $(this).find('input');
121154
var openText = hid.data('open');
122155
var closeText = hid.data('close');
123156
var value = hid.val();
124-
if(!init){
125-
$(this).data('init',1);
157+
if (!init) {
158+
$(this).data('init', 1);
126159
$(this).append('<span></span>');
127-
}else{
160+
} else {
128161
value = value == '1' ? 0 : 1;
129162
hid.val(value);
130163
}
131-
if(value == '1'){
164+
if (value == '1') {
132165
$(this).find('span').text(openText)
133-
$(this).prop('title','点击'+closeText);
166+
$(this).prop('title', '点击' + closeText);
134167
$(this).removeClass('off')
135-
}else{
168+
} else {
136169
$(this).find('span').text(closeText)
137-
$(this).prop('title','点击'+openText);
170+
$(this).prop('title', '点击' + openText);
138171
$(this).addClass('off')
139172
}
140173

141174
}).trigger('click');
142175

143176
// 状态切换按钮 切换后跳转
144177
$('.chgstatus').click(function (e) {
145-
if($(this).data('ajaxing'))return;
146-
$(this).data('ajaxing',1);
147-
var self=$(this);
148-
var parent=self.parents('td');
149-
var id=parent.data('id');
150-
var status=self.data('status');
178+
if ($(this).data('ajaxing')) return;
179+
$(this).data('ajaxing', 1);
180+
var self = $(this);
181+
var parent = self.parents('td');
182+
var id = parent.data('id');
183+
var status = self.data('status');
151184
$.ajax({
152-
url:parent.data('url'),
153-
type:'POST',
154-
dataType:'JSON',
155-
data:{
156-
id:id,
157-
status:status
185+
url: parent.data('url'),
186+
type: 'POST',
187+
dataType: 'JSON',
188+
data: {
189+
id: id,
190+
status: status
158191
},
159-
success:function (json) {
160-
self.data('ajaxing',0);
161-
if(json.code==1){
192+
success: function (json) {
193+
self.data('ajaxing', 0);
194+
if (json.code == 1) {
162195
dialog.success(json.msg);
163196
self.toggleClass('off');
164-
var totext=self.attr('title').replace('点击','');
197+
var totext = self.attr('title').replace('点击', '');
165198
self.text(totext);
166199
setTimeout(function () {
167200
location.reload();
168-
},1000);
169-
}else{
201+
}, 1000);
202+
} else {
170203
dialog.error(json.msg);
171204
}
172205
}
@@ -188,43 +221,46 @@ jQuery(function ($) {
188221
onshow: function (body) {
189222
$.ajax({
190223
url: self.attr('href'),
191-
beforeSend: function(request) {
192-
request.setRequestHeader("X-Requested-With","htmlhttp");
224+
beforeSend: function (request) {
225+
request.setRequestHeader("X-Requested-With", "htmlhttp");
193226
},
194227
success: function (text) {
195228
body.html(text);
196229
}
197230
});
198231
}
199-
}).show('<p class="loading">'+lang('loading...')+'</p>', title);
232+
}).show('<p class="loading">' + lang('loading...') + '</p>', title);
200233

201234
});
202235

203236
//确认操作
204237
$('.link-confirm').click(function (e) {
205238
e.preventDefault();
206239
e.stopPropagation();
207-
var text=$(this).data('confirm');
208-
var url=$(this).attr('href');
209-
if(text)text=text.replace(/(\\n|\n)+/g,"<br />");
210-
if(!text)text=lang('Confirm operation?');
240+
var text = $(this).data('confirm');
241+
var url = $(this).attr('href');
242+
if (text) text = text.replace(/(\\n|\n)+/g, "<br />");
243+
if (!text) text = lang('Confirm operation?');
244+
var method = $(this).data('method')
245+
if (!method) method = 'GET'
211246

212-
dialog.confirm(text,function () {
247+
dialog.confirm(text, function () {
213248
$.ajax({
214-
url:url,
215-
dataType:'JSON',
216-
success:function (json) {
217-
dialog.alert(json.msg,function(){
218-
if(json.code==1){
219-
if(json.url){
220-
location.href=json.url;
221-
}else{
249+
url: url,
250+
type: method,
251+
dataType: 'JSON',
252+
success: function (json) {
253+
dialog.alert(json.msg, function () {
254+
if (json.code == 1) {
255+
if (json.url) {
256+
location.href = json.url;
257+
} else {
222258
location.reload();
223259
}
224260
}
225261
});
226262
},
227-
error:function () {
263+
error: function () {
228264
dialog.alert(lang('Server error.'));
229265
}
230266
})
@@ -235,11 +271,11 @@ jQuery(function ($) {
235271
$('.img-view').click(function (e) {
236272
e.preventDefault();
237273
e.stopPropagation();
238-
var url=$(this).attr('href');
239-
if(!url)url=$(this).data('img');
274+
var url = $(this).attr('href');
275+
if (!url) url = $(this).data('img');
240276
var dlg = new Dialog({
241277
btns: ['确定']
242-
}).show('<a href="'+url+'" class="d-block text-center" target="_blank"><img class="img-fluid" src="'+url+'" /></a><div class="text-muted text-center">点击图片在新页面放大查看</div>', '查看图片');
278+
}).show('<a href="' + url + '" class="d-block text-center" target="_blank"><img class="img-fluid" src="' + url + '" /></a><div class="text-muted text-center">点击图片在新页面放大查看</div>', '查看图片');
243279
});
244280

245281
//tab切换效果
@@ -250,19 +286,19 @@ jQuery(function ($) {
250286

251287
//上传框
252288
window.fileInputHander = function () {
253-
var self=$(this);
254-
if(!this.files || !this.files[0])return;
255-
var inputgroup=$(this).parents('.input-group').eq(0);
256-
var parent=inputgroup.parents('div').eq(0);
289+
var self = $(this);
290+
if (!this.files || !this.files[0]) return;
291+
var inputgroup = $(this).parents('.input-group').eq(0);
292+
var parent = inputgroup.parents('div').eq(0);
257293
var label = $(this).parents('.custom-file').find('.custom-file-label');
258-
if(!label.data('origtext')){
259-
label.data('origtext',label.text());
294+
if (!label.data('origtext')) {
295+
label.data('origtext', label.text());
260296
}
261297
label.text($(this).val());
262298

263-
if(!window.URL && !window.URL.createObjectURL)return;
264-
var file=self[0].files[0];
265-
var is_img=file.type && file.type.match(/(\.|\/)(jpe?g|png|gif|webp)$/);
299+
if (!window.URL && !window.URL.createObjectURL) return;
300+
var file = self[0].files[0];
301+
var is_img = file.type && file.type.match(/(\.|\/)(jpe?g|png|gif|webp)$/);
266302

267303

268304
var figure = parent.find('.figure');
@@ -273,7 +309,7 @@ jQuery(function ($) {
273309
' </figure>');
274310
figure = parent.find('.figure');
275311
}
276-
if(is_img) {
312+
if (is_img) {
277313
var img = figure.find('img');
278314
var origurl = img.data('origurl');
279315
if (!origurl) {
@@ -296,7 +332,7 @@ jQuery(function ($) {
296332
dialog.confirm('取消上传该文件?', function () {
297333
self.val('');
298334
label.text(label.data('origtext'));
299-
if(is_img) {
335+
if (is_img) {
300336
img.attr('src', origurl);
301337
figcap.text(figcap.data('origtext'));
302338
}
@@ -311,20 +347,20 @@ jQuery(function ($) {
311347
//表单Ajax提交
312348
$('.btn-primary[type=submit]').click(function (e) {
313349
var form = $(this).parents('form');
314-
if(form.is('.noajax'))return true;
350+
if (form.is('.noajax')) return true;
315351
var btn = this;
316352

317-
var isbtn=$(btn).prop('tagName').toUpperCase()=='BUTTON';
318-
var origText=isbtn?$(btn).text():$(btn).val();
353+
var isbtn = $(btn).prop('tagName').toUpperCase() == 'BUTTON';
354+
var origText = isbtn ? $(btn).text() : $(btn).val();
319355
var options = {
320356
url: $(form).attr('action'),
321357
type: 'POST',
322358
dataType: 'JSON',
323359
success: function (json) {
324-
window.stop_ajax=false;
325-
isbtn?$(btn).text(origText):$(btn).val(origText);
360+
window.stop_ajax = false;
361+
isbtn ? $(btn).text(origText) : $(btn).val(origText);
326362
if (json.code == 1) {
327-
dialog.alert(json.msg,function(){
363+
dialog.alert(json.msg, function () {
328364
if (json.url) {
329365
location.href = json.url;
330366
} else {
@@ -337,27 +373,27 @@ jQuery(function ($) {
337373
}
338374
},
339375
error: function (xhr) {
340-
window.stop_ajax=false;
341-
isbtn?$(btn).text(origText):$(btn).val(origText);
376+
window.stop_ajax = false;
377+
isbtn ? $(btn).text(origText) : $(btn).val(origText);
342378
$(btn).removeAttr('disabled');
343379
dialog.error('服务器处理错误');
344380
}
345381
};
346382
if (form.attr('enctype') === 'multipart/form-data') {
347383
if (!FormData) {
348-
window.stop_ajax=false;
384+
window.stop_ajax = false;
349385
return true;
350386
}
351387
options.data = new FormData(form[0]);
352388
options.cache = false;
353389
options.processData = false;
354390
options.contentType = false;
355-
options.xhr= function() { //用以显示上传进度
391+
options.xhr = function () { //用以显示上传进度
356392
var xhr = $.ajaxSettings.xhr();
357393
if (xhr.upload) {
358-
xhr.upload.addEventListener('progress', function(event) {
394+
xhr.upload.addEventListener('progress', function (event) {
359395
var percent = Math.floor(event.loaded / event.total * 100);
360-
$(btn).text(origText+' ('+percent+'%)');
396+
$(btn).text(origText + ' (' + percent + '%)');
361397
}, false);
362398
}
363399
return xhr;
@@ -368,7 +404,7 @@ jQuery(function ($) {
368404

369405
e.preventDefault();
370406
$(this).attr('disabled', true);
371-
window.stop_ajax=true;
407+
window.stop_ajax = true;
372408
$.ajax(options);
373409
});
374410

@@ -377,18 +413,18 @@ jQuery(function ($) {
377413
var group = $(this).parents('.input-group');
378414
var idele = group.find('[name=member_id]');
379415
var infoele = group.find('[name=member_info]');
380-
dialog.pickUser( function (user) {
416+
dialog.pickUser(function (user) {
381417
idele.val(user.id);
382418
infoele.val('[' + user.id + '] ' + user.username + (user.mobile ? (' / ' + user.mobile) : ''));
383419
}, $(this).data('filter'));
384420
});
385421

386422
//位置选择按钮绑定
387-
$('.pick-locate').click(function(e){
388-
var group=$(this).parents('.input-group');
389-
var idele=group.find('input[type=text]');
390-
dialog.pickLocate('qq',function(locate){
391-
idele.val(locate.lng+','+locate.lat);
392-
},idele.val());
423+
$('.pick-locate').click(function (e) {
424+
var group = $(this).parents('.input-group');
425+
var idele = group.find('input[type=text]');
426+
dialog.pickLocate('qq', function (locate) {
427+
idele.val(locate.lng + ',' + locate.lat);
428+
}, idele.val());
393429
});
394430
});

src/public/static/admin/js/app.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/static/admin/js/app.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/static/js/init.min.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/public/static/js/init.min.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)