From 24be463fae6740dc961113abfd5fc98444e6778e Mon Sep 17 00:00:00 2001 From: Karson Date: Tue, 20 Jan 2026 10:53:34 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=9A=E7=94=A8=E6=90=9C=E7=B4=A2=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E8=87=AA=E5=AE=9A=E4=B9=89=E5=88=97=E6=A0=B7=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 优化通用搜索searchList 选择文件change事件新增回传数据 修复不存在编辑按钮时双击编辑功能 --- .../assets/js/bootstrap-table-commonsearch.js | 68 ++++++++++++------- public/assets/js/require-form.js | 2 +- public/assets/js/require-table.js | 37 ++++------ 3 files changed, 57 insertions(+), 50 deletions(-) diff --git a/public/assets/js/bootstrap-table-commonsearch.js b/public/assets/js/bootstrap-table-commonsearch.js index a69c559d..428f707f 100644 --- a/public/assets/js/bootstrap-table-commonsearch.js +++ b/public/assets/js/bootstrap-table-commonsearch.js @@ -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('
', that.options.actionForm)); htmlForm.push('
'); @@ -70,7 +71,7 @@ vObjCol.operate = renderDefault && operate ? operate : (typeof vObjCol.operate === 'undefined' ? '=' : vObjCol.operate); ColumnsForSearch.push(vObjCol); - htmlForm.push('
'); + htmlForm.push(sprintf('
', columnClass, (typeof vObjCol.searchVisible === 'undefined' || vObjCol.searchVisible) ? '' : 'hidden')); htmlForm.push(sprintf('', vObjCol.field, vObjCol.title)); htmlForm.push('
'); @@ -86,23 +87,7 @@ if (typeof vObjCol.searchList === 'function') { htmlForm.push(vObjCol.searchList.call(this, vObjCol)); } else { - var optionList = [sprintf('', 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('', addClass, vObjCol.field, style, extend, optionList.join(''))); } } else { @@ -125,7 +110,7 @@ htmlForm.push('
'); } } - htmlForm.push('
'); + htmlForm.push('
'); htmlForm.push(createFormBtn(that).join('')); htmlForm.push('
'); htmlForm.push('
'); @@ -147,16 +132,10 @@ }; var createOptionList = function (searchList, vObjCol, that) { - var isArray = searchList.constructor === Array; var optionList = []; optionList.push(sprintf('', 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("", 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) { diff --git a/public/assets/js/require-form.js b/public/assets/js/require-form.js index ac125a8f..e46d928b 100755 --- a/public/assets/js/require-form.js +++ b/public/assets/js/require-form.js @@ -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"); diff --git a/public/assets/js/require-table.js b/public/assets/js/require-table.js index bed68dae..cb52c49a 100644 --- a/public/assets/js/require-table.js +++ b/public/assets/js/require-table.js @@ -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 = '' + display + '';