通用搜索新增自定义列样式

优化通用搜索searchList
选择文件change事件新增回传数据
修复不存在编辑按钮时双击编辑功能
pull/517/head
Karson 2026-01-20 10:53:34 +08:00
parent 32329947a3
commit 24be463fae
3 changed files with 57 additions and 50 deletions

View File

@ -53,6 +53,7 @@
if (that.options.searchFormTemplate) {
return Template(that.options.searchFormTemplate, {columns: pColumns, table: that});
}
var columnClass = typeof that.options.commonSearchColumnClass === 'undefined' ? 'col-xs-12 col-sm-6 col-md-4 col-lg-3' : that.options.commonSearchColumnClass;
var htmlForm = [];
htmlForm.push(sprintf('<form class="form-horizontal form-commonsearch" novalidate method="post" action="%s" >', that.options.actionForm));
htmlForm.push('<fieldset>');
@ -70,7 +71,7 @@
vObjCol.operate = renderDefault && operate ? operate : (typeof vObjCol.operate === 'undefined' ? '=' : vObjCol.operate);
ColumnsForSearch.push(vObjCol);
htmlForm.push('<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">');
htmlForm.push(sprintf('<div class="form-group %s %s">', columnClass, (typeof vObjCol.searchVisible === 'undefined' || vObjCol.searchVisible) ? '' : 'hidden'));
htmlForm.push(sprintf('<label for="%s" class="control-label col-xs-4">%s</label>', vObjCol.field, vObjCol.title));
htmlForm.push('<div class="col-xs-8">');
@ -86,23 +87,7 @@
if (typeof vObjCol.searchList === 'function') {
htmlForm.push(vObjCol.searchList.call(this, vObjCol));
} else {
var optionList = [sprintf('<option value="">%s</option>', that.options.formatCommonChoose())];
if (typeof vObjCol.searchList === 'object' && typeof vObjCol.searchList.then === 'function') {
(function (vObjCol, that) {
$.when(vObjCol.searchList).done(function (ret) {
var searchList = [];
if (ret.data && ret.data.searchlist && $.isArray(ret.data.searchlist)) {
searchList = ret.data.searchlist;
} else if (ret.constructor === Array || ret.constructor === Object) {
searchList = ret;
}
var optionList = createOptionList(searchList, vObjCol, that);
$("form.form-commonsearch select[name='" + vObjCol.field + "']", that.$container).html(optionList.join('')).trigger("change");
});
})(vObjCol, that);
} else {
optionList = createOptionList(vObjCol.searchList, vObjCol, that);
}
var optionList = createOptionList(vObjCol.searchList, vObjCol, that);
htmlForm.push(sprintf('<select class="%s" name="%s" %s %s>%s</select>', addClass, vObjCol.field, style, extend, optionList.join('')));
}
} else {
@ -125,7 +110,7 @@
htmlForm.push('</div>');
}
}
htmlForm.push('<div class="form-group col-xs-12 col-sm-6 col-md-4 col-lg-3">');
htmlForm.push('<div class="form-group ' + columnClass + '">');
htmlForm.push(createFormBtn(that).join(''));
htmlForm.push('</div>');
htmlForm.push('</div>');
@ -147,16 +132,10 @@
};
var createOptionList = function (searchList, vObjCol, that) {
var isArray = searchList.constructor === Array;
var optionList = [];
optionList.push(sprintf('<option value="">%s</option>', that.options.formatCommonChoose()));
searchList = $.fn.bootstrapTable.utils.combineSearchList(searchList);
$.each(searchList, function (key, value) {
if (value.constructor === Object) {
key = value.id;
value = value.name;
} else {
key = isArray ? value : key;
}
optionList.push(sprintf("<option value='" + Fast.api.escape(key) + "' %s>" + Fast.api.escape(value) + "</option>", key == vObjCol.defaultValue ? 'selected' : ''));
});
return optionList;
@ -306,6 +285,43 @@
_load = BootstrapTable.prototype.load,
_initSearch = BootstrapTable.prototype.initSearch;
// 定义通用searchList处理方法
$.fn.bootstrapTable.utils.combineSearchList = function (list) {
var searchList = {};
if (typeof list !== 'undefined') {
var isFunction = list.constructor === Function;
var isAjax = typeof list === 'object' && typeof list.then === 'function';
if (isFunction) {
list = list();
} else if (isAjax) {
$.when(list).done(function (ret) {
// 兼容旧版本在data中返回searchlist
if (typeof ret.data.searchlist !== 'undefined') {
list = ret.data.searchlist;
} else {
list = ret;
}
});
}
// 兼容旧版本searchList返回字符串直接渲染自定义搜索栏
if (typeof list === 'string') {
return searchList;
}
var isArray = list.constructor === Array;
var i = 0;
$.each(list, function (key, val) {
if (typeof val !== 'undefined' && val.constructor === Object) {
key = val.id;
val = val.name;
} else {
key = isArray ? val : key;
}
searchList[key] = val;
});
}
return searchList;
};
BootstrapTable.prototype.initHeader = function () {
_initHeader.apply(this, Array.prototype.slice.apply(arguments));
this.$header.find('th[data-field]').each(function (i) {

View File

@ -295,7 +295,7 @@ define(['jquery', 'bootstrap', 'upload', 'validator', 'validator-lang'], functio
}
}
var result = urlArr.join(",");
inputObj.val(result).trigger("change").trigger("validate");
inputObj.val(result).trigger("change", [data]).trigger("validate");
} else if (input_id) {
var url = Config.upload.fullmode ? Fast.api.cdnurl(data.url, true) : data.url;
$("#" + input_id).val(url).trigger("change").trigger("validate");

View File

@ -244,7 +244,13 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
if (options.dblClickToEdit) {
//当双击单元格时
table.on('dbl-click-row.bs.table', function (e, row, element, field) {
$(Table.config.editonebtn, element).trigger("click");
var editone = $(Table.config.editonebtn, element);
if (editone.length > 0) {
editone.trigger("click");
} else if (options.extend.edit_url) {
// 判断是否允许双击编辑
Table.api.events.operate['click .btn-editone'].call(element, e, undefined, row, element.index());
}
});
}
//渲染内容前
@ -855,13 +861,14 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
}
value = row[this.field] || value;
value = value == null || value.length === 0 ? '' : value.toString();
this.searchList = $.fn.bootstrapTable.utils.combineSearchList(this.searchList);
var keys = typeof this.searchList === 'object' ? Object.keys(this.searchList) : [];
var index = keys.indexOf(value);
var keyIndex = keys.indexOf(value);
var color = value && typeof custom[value] !== 'undefined' ? custom[value] : null;
var display = index > -1 ? this.searchList[value] : null;
var icon = typeof this.icon !== 'undefined' ? this.icon : null;
if (!color) {
color = index > -1 && typeof colorArr[index] !== 'undefined' ? colorArr[index] : 'primary';
color = keyIndex > -1 && typeof colorArr[keyIndex] !== 'undefined' ? colorArr[keyIndex] : 'primary';
}
if (!display) {
display = __(value.charAt(0).toUpperCase() + value.slice(1));
@ -938,25 +945,9 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
value = Fast.api.escape(customValue);
field = this.customField;
}
if (typeof that.searchList === 'object' && typeof that.searchList.then === 'function') {
$.when(that.searchList).done(function (ret) {
if (ret.data && ret.data.searchlist && $.isArray(ret.data.searchlist)) {
that.searchList = ret.data.searchlist;
} else if (ret.constructor === Array || ret.constructor === Object) {
that.searchList = ret;
}
})
}
if (typeof that.searchList === 'object' && typeof that.custom === 'undefined') {
var i = 0;
var searchValues = Object.values(colorArr);
$.each(that.searchList, function (key, val) {
if (typeof colorArr[key] == 'undefined') {
colorArr[key] = searchValues[i];
i = typeof searchValues[i + 1] === 'undefined' ? 0 : i + 1;
}
});
}
//优化searchList
that.searchList = $.fn.bootstrapTable.utils.combineSearchList(that.searchList);
//渲染Flag
var html = [];
@ -967,7 +958,7 @@ define(['jquery', 'bootstrap', 'moment', 'moment/locale/zh-cn', 'bootstrap-table
if (value === '')
return true;
color = value && typeof colorArr[value] !== 'undefined' ? colorArr[value] : 'primary';
display = typeof that.searchList !== 'undefined' && typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
display = typeof that.searchList[value] !== 'undefined' ? that.searchList[value] : __(value.charAt(0).toUpperCase() + value.slice(1));
value = Fast.api.escape(value);
display = Fast.api.escape(display);
label = '<span class="label label-' + color + '">' + display + '</span>';