Skip to content

Commit 24be463

Browse files
committed
通用搜索新增自定义列样式
优化通用搜索searchList 选择文件change事件新增回传数据 修复不存在编辑按钮时双击编辑功能
1 parent 3232994 commit 24be463

File tree

3 files changed

+57
-50
lines changed

3 files changed

+57
-50
lines changed

public/assets/js/bootstrap-table-commonsearch.js

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
if (that.options.searchFormTemplate) {
5454
return Template(that.options.searchFormTemplate, {columns: pColumns, table: that});
5555
}
56+
var columnClass = typeof that.options.commonSearchColumnClass === 'undefined' ? 'col-xs-12 col-sm-6 col-md-4 col-lg-3' : that.options.commonSearchColumnClass;
5657
var htmlForm = [];
5758
htmlForm.push(sprintf('<form class="form-horizontal form-commonsearch" novalidate method="post" action="%s" >', that.options.actionForm));
5859
htmlForm.push('<fieldset>');
@@ -70,7 +71,7 @@
7071
vObjCol.operate = renderDefault && operate ? operate : (typeof vObjCol.operate === 'undefined' ? '=' : vObjCol.operate);
7172
ColumnsForSearch.push(vObjCol);
7273

73-
htmlForm.push('<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">');
74+
htmlForm.push(sprintf('<div class="form-group %s %s">', columnClass, (typeof vObjCol.searchVisible === 'undefined' || vObjCol.searchVisible) ? '' : 'hidden'));
7475
htmlForm.push(sprintf('<label for="%s" class="control-label col-xs-4">%s</label>', vObjCol.field, vObjCol.title));
7576
htmlForm.push('<div class="col-xs-8">');
7677

@@ -86,23 +87,7 @@
8687
if (typeof vObjCol.searchList === 'function') {
8788
htmlForm.push(vObjCol.searchList.call(this, vObjCol));
8889
} else {
89-
var optionList = [sprintf('<option value="">%s</option>', that.options.formatCommonChoose())];
90-
if (typeof vObjCol.searchList === 'object' && typeof vObjCol.searchList.then === 'function') {
91-
(function (vObjCol, that) {
92-
$.when(vObjCol.searchList).done(function (ret) {
93-
var searchList = [];
94-
if (ret.data && ret.data.searchlist && $.isArray(ret.data.searchlist)) {
95-
searchList = ret.data.searchlist;
96-
} else if (ret.constructor === Array || ret.constructor === Object) {
97-
searchList = ret;
98-
}
99-
var optionList = createOptionList(searchList, vObjCol, that);
100-
$("form.form-commonsearch select[name='" + vObjCol.field + "']", that.$container).html(optionList.join('')).trigger("change");
101-
});
102-
})(vObjCol, that);
103-
} else {
104-
optionList = createOptionList(vObjCol.searchList, vObjCol, that);
105-
}
90+
var optionList = createOptionList(vObjCol.searchList, vObjCol, that);
10691
htmlForm.push(sprintf('<select class="%s" name="%s" %s %s>%s</select>', addClass, vObjCol.field, style, extend, optionList.join('')));
10792
}
10893
} else {
@@ -125,7 +110,7 @@
125110
htmlForm.push('</div>');
126111
}
127112
}
128-
htmlForm.push('<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">');
113+
htmlForm.push('<div class="form-group ' + columnClass + '">');
129114
htmlForm.push(createFormBtn(that).join(''));
130115
htmlForm.push('</div>');
131116
htmlForm.push('</div>');
@@ -147,16 +132,10 @@
147132
};
148133

149134
var createOptionList = function (searchList, vObjCol, that) {
150-
var isArray = searchList.constructor === Array;
151135
var optionList = [];
152136
optionList.push(sprintf('<option value="">%s</option>', that.options.formatCommonChoose()));
137+
searchList = $.fn.bootstrapTable.utils.combineSearchList(searchList);
153138
$.each(searchList, function (key, value) {
154-
if (value.constructor === Object) {
155-
key = value.id;
156-
value = value.name;
157-
} else {
158-
key = isArray ? value : key;
159-
}
160139
optionList.push(sprintf("<option value='" + Fast.api.escape(key) + "' %s>" + Fast.api.escape(value) + "</option>", key == vObjCol.defaultValue ? 'selected' : ''));
161140
});
162141
return optionList;
@@ -306,6 +285,43 @@
306285
_load = BootstrapTable.prototype.load,
307286
_initSearch = BootstrapTable.prototype.initSearch;
308287

288+
// 定义通用searchList处理方法
289+
$.fn.bootstrapTable.utils.combineSearchList = function (list) {
290+
var searchList = {};
291+
292+
if (typeof list !== 'undefined') {
293+
var isFunction = list.constructor === Function;
294+
var isAjax = typeof list === 'object' && typeof list.then === 'function';
295+
if (isFunction) {
296+
list = list();
297+
} else if (isAjax) {
298+
$.when(list).done(function (ret) {
299+
// 兼容旧版本在data中返回searchlist
300+
if (typeof ret.data.searchlist !== 'undefined') {
301+
list = ret.data.searchlist;
302+
} else {
303+
list = ret;
304+
}
305+
});
306+
}
307+
// 兼容旧版本searchList返回字符串直接渲染自定义搜索栏
308+
if (typeof list === 'string') {
309+
return searchList;
310+
}
311+
var isArray = list.constructor === Array;
312+
var i = 0;
313+
$.each(list, function (key, val) {
314+
if (typeof val !== 'undefined' && val.constructor === Object) {
315+
key = val.id;
316+
val = val.name;
317+
} else {
318+
key = isArray ? val : key;
319+
}
320+
searchList[key] = val;
321+
});
322+
}
323+
return searchList;
324+
};
309325
BootstrapTable.prototype.initHeader = function () {
310326
_initHeader.apply(this, Array.prototype.slice.apply(arguments));
311327
this.$header.find('th[data-field]').each(function (i) {

public/assets/js/require-form.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ define(['jquery', 'bootstrap', 'upload', 'validator', 'validator-lang'], functio
295295
}
296296
}
297297
var result = urlArr.join(",");
298-
inputObj.val(result).trigger("change").trigger("validate");
298+
inputObj.val(result).trigger("change", [data]).trigger("validate");
299299
} else if (input_id) {
300300
var url = Config.upload.fullmode ? Fast.api.cdnurl(data.url, true) : data.url;
301301
$("#" + input_id).val(url).trigger("change").trigger("validate");

public/assets/js/require-table.js

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,13 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
244244
if (options.dblClickToEdit) {
245245
//当双击单元格时
246246
table.on('dbl-click-row.bs.table', function (e, row, element, field) {
247-
$(Table.config.editonebtn, element).trigger("click");
247+
var editone = $(Table.config.editonebtn, element);
248+
if (editone.length > 0) {
249+
editone.trigger("click");
250+
} else if (options.extend.edit_url) {
251+
// 判断是否允许双击编辑
252+
Table.api.events.operate['click .btn-editone'].call(element, e, undefined, row, element.index());
253+
}
248254
});
249255
}
250256
//渲染内容前
@@ -855,13 +861,14 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
855861
}
856862
value = row[this.field] || value;
857863
value = value == null || value.length === 0 ? '' : value.toString();
864+
this.searchList = $.fn.bootstrapTable.utils.combineSearchList(this.searchList);
858865
var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
859-
var index = keys.indexOf(value);
866+
var keyIndex = keys.indexOf(value);
860867
var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
861868
var display = index > -1 ? this.searchList[value] : null;
862869
var icon = typeof this.icon !== 'undefined' ? this.icon : null;
863870
if (!color) {
864-
color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
871+
color = keyIndex > -1 && typeof colorArr[keyIndex] !== 'undefined' ? colorArr[keyIndex] : 'primary';
865872
}
866873
if (!display) {
867874
display = __(value.charAt(0).toUpperCase() + value.slice(1));
@@ -938,25 +945,9 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
938945
value = Fast.api.escape(customValue);
939946
field = this.customField;
940947
}
941-
if (typeof that.searchList === 'object' && typeof that.searchList.then === 'function') {
942-
$.when(that.searchList).done(function (ret) {
943-
if (ret.data && ret.data.searchlist && $.isArray(ret.data.searchlist)) {
944-
that.searchList = ret.data.searchlist;
945-
} else if (ret.constructor === Array || ret.constructor === Object) {
946-
that.searchList = ret;
947-
}
948-
})
949-
}
950-
if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
951-
var i = 0;
952-
var searchValues = Object.values(colorArr);
953-
$.each(that.searchList, function (key, val) {
954-
if (typeof colorArr[key] == 'undefined') {
955-
colorArr[key] = searchValues[i];
956-
i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
957-
}
958-
});
959-
}
948+
949+
//优化searchList
950+
that.searchList = $.fn.bootstrapTable.utils.combineSearchList(that.searchList);
960951

961952
//渲染Flag
962953
var html = [];
@@ -967,7 +958,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
967958
if (value === '')
968959
return true;
969960
color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
970-
display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
961+
display = typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
971962
value = Fast.api.escape(value);
972963
display = Fast.api.escape(display);
973964
label = '<span class="label label-' + color + '">' + display + '</span>';

0 commit comments

Comments
 (0)