forms.js
7.6 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
$(function() {
$( "#sortable" ).sortable();
$( "#sortable" ).disableSelection();
var sortList;
var sort_list = [];
var checkbox_html;
sort_list[1] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
sort_list[2] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
sort_list[3] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
sort_list[4] = '';
sort_list[5] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
sort_list[6] = '<div class="f_formTextarea"><div class="f_textareaContent"><textarea class="f_textarea"></textarea></div></div>';
sort_list[7] = '<div class="f_formSelect"><div class="f_selectContent"><select><option>请选择</option></select></div></div>';
sort_list[8] = '<div class="f_formText"><input type="file" disabled class="f_input"/></div>';
sort_list[9] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
sort_list[10] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
sort_list[11] = '\n' +
' <div class="f_formSelect">\n' +
' <div class="f_selectContent">\n' +
' <select>\n' +
' <option>请选择</option>\n' +
' </select>\n' +
' </div>\n' +
' <div class="f_selectContent">\n' +
' <select>\n' +
' <option>请选择</option>\n' +
' </select>\n' +
' </div>\n' +
' <div class="f_selectContent">\n' +
' <select>\n' +
' <option>请选择</option>\n' +
' </select>\n' +
' </div>\n' +
' </div>';
sort_list[12] = '<div class="f_formText"><input type="text" disabled class="f_input"/></div>';
// 添加表单元素
$('.f_item').click(function(){
var id = $(this).data('id');
var sort = $(this).data('type');
var name = $(this).data('name');
var html = forms(id, sort, name);
if($(this).hasClass('active')) {
$(this).removeClass('active');
$('#item'+id).remove();
$('#check'+id).parent('.checkbox').remove();
if($('.f_content').html() == '') {
$('.f_submit').hide();
}
} else {
$(this).addClass('active');
$('.f_content').append(html);
$('.checkbox_content').append(checkbox_html);
$('.f_submit').show();
}
});
// 切换选中
$('.f_body').on('click','.f_group',function(){
var id = $(this).data('id');
$('.checkbox').hide();
$('#check'+id).parent('.checkbox').show();
$('.f_group').siblings().find('.f_component').removeClass('f_clicked');
$(this).find('.f_component').addClass('f_clicked');
});
// 添加渲染内容
function forms(id, sort, name, status = 0){
sortList = sort_list[sort];
if(sort == 4) {
var data = {
id: id,
sort: sort
};
getRadioData(data);
}
var html = `<div class="f_group" id="item${id}" data-id="${id}" data-type="${sort}" data-status="${status}">
<div class="f_component">
<div class="f_mask"></div>
<div class="f_contentText">
<p class="f_title">
<span>${name}</span>
</p>
</div>
<div class="f_contentBody">
${sortList}
</div>
</div>
<div class="f_remove"></div>
<div class="f_clear"></div>
</div>`;
checkbox_html = `<div class="checkbox" style="display: none;">
<label id="check${id}"><input type="checkbox">是否为必填项</label>
</div>`;
return html;
}
// 获取单选内容数据
function getRadioData(data) {
$.ajax({
async: false,
url: radio_url,
type: "POST",
data: data,
dataType: "JSON",
success: function(res){
if(res.code == 1) {
html = '';
$(res.data).each(function(index,element){
html += '<div class="f_radioContent">' +
' <span class="">'+element+'</span>\n' +
'</div>';
});
sortList = '<div class="f_formRadio">\n'+
html + '</div>';
}
}
})
}
// 移除元素
$('.f_body').on('click','.f_remove',function(){
$(this).parent().remove();
});
// 提交选择的表单数据
$('.btn-primary').click(function(){
var data = [];
var btn = $(this);
var text = $(this).html();
$('.f_group').each(function(index,element){
var id = $(this).data('id');
var type = $(this).data('type');
var status = $(this).data('status');
var checked = 0;
if($('#check'+id).find('input').prop("checked")) {
checked = 1;
}
param = {
id: $(this).data('id'),
type: $(this).data('type'),
status: $(this).data('status'),
checked: checked
};
data.push(param);
});
console.log({
name: $('input[name=name]').val(),
icon: $('input[name=icon]').val(),
param: data
});
$.ajax({
url: url,
type: "POST",
data: {
name: $('input[name=name]').val(),
icon: $('input[name=icon]').val(),
param: data
},
dataType: "JSON",
beforeSend: function(){
btn.attr('disabled',true);
btn.text(text+'中...');
},
success: function(data){
new Noty({
text: data.msg,
type: 'success',
layout: 'topCenter',
modal: true,
animation: {
open: 'animated bounceInDown', // Animate.css class names
close: 'animated bounceOutUp', // Animate.css class names
},
timeout: 1,
callbacks: {
afterClose: function () {
if (data.url) {
//返回带跳转地址
window.location.href = data.url;
} else {
if (data.code == 1) {
//刷新当前页
reloadPage(window);
}
}
}
}
}).show();
},
complete: function(res){
btn.attr('disabled',false);
btn.text(text);
}
});
});
});