|
@@ -1,9 +1,11 @@
|
|
|
/**
|
|
|
+ * FastAdmin通用搜索
|
|
|
+ *
|
|
|
* @author: pppscn <35696959@qq.com>
|
|
|
- * @version: v0.0.1
|
|
|
+ * @update 2017-05-07 <https://gitee.com/pp/fastadmin>
|
|
|
*
|
|
|
- * @update 2017-05-07 <http://git.oschina.net/pp/fastadmin>
|
|
|
- * @update 2017-09-17 <http://git.oschina.net/karson/fastadmin>
|
|
|
+ * @author: Karson <karsonzhang@163.com>
|
|
|
+ * @update 2018-04-05 <https://gitee.com/karson/fastadmin>
|
|
|
*/
|
|
|
|
|
|
!function ($) {
|
|
@@ -17,75 +19,16 @@
|
|
|
var vFormCommon = createFormCommon(pColumns, that);
|
|
|
|
|
|
var vModal = sprintf("<div class=\"commonsearch-table %s\">", that.options.searchFormVisible ? "" : "hidden");
|
|
|
- vModal += vFormCommon.join('');
|
|
|
+ vModal += vFormCommon;
|
|
|
vModal += "</div>";
|
|
|
that.$container.prepend($(vModal));
|
|
|
that.$commonsearch = $(".commonsearch-table", that.$container);
|
|
|
var form = $("form.form-commonsearch", that.$commonsearch);
|
|
|
|
|
|
- //绑定日期时间元素事件
|
|
|
- if ($(".datetimepicker", form).size() > 0) {
|
|
|
-
|
|
|
- require(['bootstrap-datetimepicker'], function () {
|
|
|
- $('.datetimepicker', form).parent().css('position', 'relative');
|
|
|
- $('.datetimepicker', form).datetimepicker({
|
|
|
- //format: 'YYYY-MM-DD',
|
|
|
- icons: {
|
|
|
- time: 'fa fa-clock-o',
|
|
|
- date: 'fa fa-calendar',
|
|
|
- up: 'fa fa-chevron-up',
|
|
|
- down: 'fa fa-chevron-down',
|
|
|
- previous: 'fa fa-chevron-left',
|
|
|
- next: 'fa fa-chevron-right',
|
|
|
- today: 'fa fa-history',
|
|
|
- clear: 'fa fa-trash',
|
|
|
- close: 'fa fa-remove'
|
|
|
- },
|
|
|
- showTodayButton: true,
|
|
|
- showClose: true
|
|
|
- });
|
|
|
- });
|
|
|
- }
|
|
|
- if ($(".datetimerange", form).size() > 0) {
|
|
|
- var ranges = {};
|
|
|
- ranges[__('Today')] = [Moment().startOf('day'), Moment().endOf('day')];
|
|
|
- ranges[__('Yesterday')] = [Moment().subtract(1, 'days').startOf('day'), Moment().subtract(1, 'days').endOf('day')];
|
|
|
- ranges[__('Last 7 Days')] = [Moment().subtract(6, 'days').startOf('day'), Moment().endOf('day')];
|
|
|
- ranges[__('Last 30 Days')] = [Moment().subtract(29, 'days').startOf('day'), Moment().endOf('day')];
|
|
|
- ranges[__('This Month')] = [Moment().startOf('month'), Moment().endOf('month')];
|
|
|
- ranges[__('Last Month')] = [Moment().subtract(1, 'month').startOf('month'), Moment().subtract(1, 'month').endOf('month')];
|
|
|
- var options = {
|
|
|
- timePicker: false,
|
|
|
- autoUpdateInput: false,
|
|
|
- timePickerSeconds: true,
|
|
|
- timePicker24Hour: true,
|
|
|
- autoApply: true,
|
|
|
- locale: {
|
|
|
- format: 'YYYY-MM-DD HH:mm:ss',
|
|
|
- customRangeLabel: __("Custom Range"),
|
|
|
- applyLabel: __("Apply"),
|
|
|
- cancelLabel: __("Clear"),
|
|
|
- },
|
|
|
- ranges: ranges,
|
|
|
- };
|
|
|
- var callback = function (start, end) {
|
|
|
- $(this.element).val(start.format(options.locale.format) + " - " + end.format(options.locale.format));
|
|
|
- };
|
|
|
- var column, index;
|
|
|
- require(['bootstrap-daterangepicker'], function () {
|
|
|
- $(".datetimerange", form).each(function () {
|
|
|
- $(this).on('apply.daterangepicker', function (ev, picker) {
|
|
|
- callback.call(picker, picker.startDate, picker.endDate);
|
|
|
- });
|
|
|
- $(this).on('cancel.daterangepicker', function (ev, picker) {
|
|
|
- $(this).val('');
|
|
|
- });
|
|
|
- index = $(this).data("index");
|
|
|
- column = pColumns[index];
|
|
|
- $(this).daterangepicker($.extend({}, options, column.options || {}), callback);
|
|
|
- });
|
|
|
- });
|
|
|
- }
|
|
|
+ require(['form'], function (Form) {
|
|
|
+ Form.api.bindevent(form);
|
|
|
+ form.validator("destroy");
|
|
|
+ });
|
|
|
|
|
|
// 表单提交
|
|
|
form.on("submit", function (event) {
|
|
@@ -103,9 +46,12 @@
|
|
|
};
|
|
|
|
|
|
var createFormCommon = function (pColumns, that) {
|
|
|
+ // 如果有使用模板则直接返回模板的内容
|
|
|
+ if (that.options.searchFormTemplate) {
|
|
|
+ return Template(that.options.searchFormTemplate, {columns: pColumns, table: that});
|
|
|
+ }
|
|
|
var htmlForm = [];
|
|
|
- var opList = ['=', '>', '>=', '<', '<=', '!=', 'FIND_IN_SET', 'LIKE', 'LIKE %...%', 'NOT LIKE', 'IN', 'NOT IN', 'IN(...)', 'NOT IN(...)', 'BETWEEN', 'NOT BETWEEN', 'RANGE', 'NOT RANGE', 'IS NULL', 'IS NOT NULL'];
|
|
|
- htmlForm.push(sprintf('<form class="form-horizontal form-commonsearch" action="%s" >', that.options.actionForm));
|
|
|
+ htmlForm.push(sprintf('<form class="form-horizontal form-commonsearch" novalidate method="post" action="%s" >', that.options.actionForm));
|
|
|
htmlForm.push('<fieldset>');
|
|
|
if (that.options.titleForm.length > 0)
|
|
|
htmlForm.push(sprintf("<legend>%s</legend>", that.options.titleForm));
|
|
@@ -114,24 +60,28 @@
|
|
|
var vObjCol = pColumns[i];
|
|
|
if (!vObjCol.checkbox && vObjCol.field !== 'operate' && vObjCol.searchable && vObjCol.operate !== false) {
|
|
|
var query = Backend.api.query(vObjCol.field);
|
|
|
- query = query ? query : '';
|
|
|
- vObjCol.defaultValue = that.options.renderDefault && query != '' ? query : (typeof vObjCol.defaultValue === 'undefined' ? '' : vObjCol.defaultValue);
|
|
|
+ var operate = Backend.api.query(vObjCol.field + "-operate");
|
|
|
+
|
|
|
+ vObjCol.defaultValue = that.options.renderDefault && query ? query : (typeof vObjCol.defaultValue === 'undefined' ? '' : vObjCol.defaultValue);
|
|
|
+ vObjCol.operate = that.options.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('<label for="%s" class="control-label col-xs-4">%s</label>', vObjCol.field, vObjCol.title));
|
|
|
htmlForm.push('<div class="col-xs-8">');
|
|
|
|
|
|
- vObjCol.operate = (typeof vObjCol.operate === 'undefined' || $.inArray(vObjCol.operate.toUpperCase(), opList) === -1) ? '=' : vObjCol.operate.toUpperCase();
|
|
|
- htmlForm.push(sprintf('<input type="hidden" class="form-control operate" name="field-%s" data-name="%s" value="%s" readonly>', vObjCol.field, vObjCol.field, vObjCol.operate));
|
|
|
+ vObjCol.operate = vObjCol.operate ? vObjCol.operate.toUpperCase() : '=';
|
|
|
+ htmlForm.push(sprintf('<input type="hidden" class="form-control operate" name="%s-operate" data-name="%s" value="%s" readonly>', vObjCol.field, vObjCol.field, vObjCol.operate));
|
|
|
|
|
|
+ var addClass = typeof vObjCol.addClass === 'undefined' ? (typeof vObjCol.addclass === 'undefined' ? 'form-control' : 'form-control ' + vObjCol.addclass) : 'form-control ' + vObjCol.addClass;
|
|
|
+ var extend = typeof vObjCol.extend === 'undefined' ? '' : vObjCol.extend;
|
|
|
var style = typeof vObjCol.style === 'undefined' ? '' : sprintf('style="%s"', vObjCol.style);
|
|
|
+ extend = typeof vObjCol.data !== 'undefined' && extend == '' ? vObjCol.data : extend;
|
|
|
if (vObjCol.searchList) {
|
|
|
if (typeof vObjCol.searchList === 'object' && typeof vObjCol.searchList.then === 'function') {
|
|
|
- htmlForm.push(sprintf('<select class="form-control" name="%s" %s>%s</select>', vObjCol.field, style, sprintf('<option value="">%s</option>', that.options.formatCommonChoose())));
|
|
|
+ htmlForm.push(sprintf('<select class="%s" name="%s" %s %s>%s</select>', addClass, vObjCol.field, style, extend, sprintf('<option value="">%s</option>', that.options.formatCommonChoose())));
|
|
|
(function (vObjCol, that) {
|
|
|
$.when(vObjCol.searchList).done(function (ret) {
|
|
|
-
|
|
|
var isArray = false;
|
|
|
if (ret.data && ret.data.searchlist && $.isArray(ret.data.searchlist)) {
|
|
|
var resultlist = {};
|
|
@@ -160,23 +110,21 @@
|
|
|
var isSelect = (isArray ? value : key) == vObjCol.defaultValue ? 'selected' : '';
|
|
|
searchList.push(sprintf("<option value='" + (isArray ? value : key) + "' %s>" + value + "</option>", isSelect));
|
|
|
});
|
|
|
- htmlForm.push(sprintf('<select class="form-control" name="%s" %s>%s</select>', vObjCol.field, style, searchList.join('')));
|
|
|
+ htmlForm.push(sprintf('<select class="%s" name="%s" %s %s>%s</select>', addClass, vObjCol.field, style, extend, searchList.join('')));
|
|
|
}
|
|
|
} else {
|
|
|
var placeholder = typeof vObjCol.placeholder === 'undefined' ? vObjCol.title : vObjCol.placeholder;
|
|
|
var type = typeof vObjCol.type === 'undefined' ? 'text' : vObjCol.type;
|
|
|
- var addclass = typeof vObjCol.addclass === 'undefined' ? 'form-control' : 'form-control ' + vObjCol.addclass;
|
|
|
- var data = typeof vObjCol.data === 'undefined' ? '' : vObjCol.data;
|
|
|
var defaultValue = typeof vObjCol.defaultValue === 'undefined' ? '' : vObjCol.defaultValue;
|
|
|
if (/BETWEEN$/.test(vObjCol.operate)) {
|
|
|
var defaultValueArr = defaultValue.toString().match(/\|/) ? defaultValue.split('|') : ['', ''];
|
|
|
var placeholderArr = placeholder.toString().match(/\|/) ? placeholder.split('|') : [placeholder, placeholder];
|
|
|
htmlForm.push('<div class="row row-between">');
|
|
|
- htmlForm.push(sprintf('<div class="col-xs-6 11"><input type="%s" class="%s" name="%s" value="%s" placeholder="%s" id="%s" data-index="%s" %s %s></div>', type, addclass, vObjCol.field, defaultValueArr[0], placeholderArr[0], vObjCol.field, i, style, data));
|
|
|
- htmlForm.push(sprintf('<div class="col-xs-6 22"><input type="%s" class="%s" name="%s" value="%s" placeholder="%s" id="%s" data-index="%s" %s %s></div>', type, addclass, vObjCol.field, defaultValueArr[1], placeholderArr[1], vObjCol.field, i, style, data));
|
|
|
+ htmlForm.push(sprintf('<div class="col-xs-6"><input type="%s" class="%s" name="%s" value="%s" placeholder="%s" id="%s" data-index="%s" %s %s></div>', type, addClass, vObjCol.field, defaultValueArr[0], placeholderArr[0], vObjCol.field, i, style, extend));
|
|
|
+ htmlForm.push(sprintf('<div class="col-xs-6"><input type="%s" class="%s" name="%s" value="%s" placeholder="%s" id="%s" data-index="%s" %s %s></div>', type, addClass, vObjCol.field, defaultValueArr[1], placeholderArr[1], vObjCol.field, i, style, extend));
|
|
|
htmlForm.push('</div>');
|
|
|
} else {
|
|
|
- htmlForm.push(sprintf('<input type="%s" class="%s" name="%s" value="%s" placeholder="%s" id="%s" data-index="%s" %s %s>', type, addclass, vObjCol.field, defaultValue, placeholder, vObjCol.field, i, style, data));
|
|
|
+ htmlForm.push(sprintf('<input type="%s" class="%s" name="%s" value="%s" placeholder="%s" id="%s" data-index="%s" %s %s>', type, addClass, vObjCol.field, defaultValue, placeholder, vObjCol.field, i, style, extend));
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -191,7 +139,7 @@
|
|
|
htmlForm.push('</fieldset>');
|
|
|
htmlForm.push('</form>');
|
|
|
|
|
|
- return htmlForm;
|
|
|
+ return htmlForm.join('');
|
|
|
};
|
|
|
|
|
|
var createFormBtn = function (that) {
|
|
@@ -199,7 +147,7 @@
|
|
|
var searchSubmit = that.options.formatCommonSubmitButton();
|
|
|
var searchReset = that.options.formatCommonResetButton();
|
|
|
htmlBtn.push('<div class="col-sm-8 col-xs-offset-4">');
|
|
|
- htmlBtn.push(sprintf('<button type="submit" class="btn btn-success" >%s</button> ', searchSubmit));
|
|
|
+ htmlBtn.push(sprintf('<button type="submit" class="btn btn-success" formnovalidate>%s</button> ', searchSubmit));
|
|
|
htmlBtn.push(sprintf('<button type="reset" class="btn btn-default" >%s</button> ', searchReset));
|
|
|
htmlBtn.push('</div>');
|
|
|
return htmlBtn;
|
|
@@ -219,16 +167,17 @@
|
|
|
var op = {};
|
|
|
var filter = {};
|
|
|
var value = '';
|
|
|
- $("form.form-commonsearch input.operate", that.$commonsearch).each(function (i) {
|
|
|
+ $("form.form-commonsearch .operate", that.$commonsearch).each(function (i) {
|
|
|
var name = $(this).data("name");
|
|
|
- var sym = $(this).val().toUpperCase();
|
|
|
+ var sym = $(this).is("select") ? $("option:selected", this).val() : $(this).val().toUpperCase();
|
|
|
var obj = $("[name='" + name + "']", that.$commonsearch);
|
|
|
if (obj.size() == 0)
|
|
|
return true;
|
|
|
var vObjCol = ColumnsForSearch[i];
|
|
|
if (obj.size() > 1) {
|
|
|
if (/BETWEEN$/.test(sym)) {
|
|
|
- var value_begin = $.trim($("[name='" + name + "']:first", that.$commonsearch).val()), value_end = $.trim($("[name='" + name + "']:last", that.$commonsearch).val());
|
|
|
+ var value_begin = $.trim($("[name='" + name + "']:first", that.$commonsearch).val()),
|
|
|
+ value_end = $.trim($("[name='" + name + "']:last", that.$commonsearch).val());
|
|
|
if (value_begin.length || value_end.length) {
|
|
|
if (typeof vObjCol.process === 'function') {
|
|
|
value_begin = vObjCol.process(value_begin, 'begin');
|
|
@@ -244,11 +193,12 @@
|
|
|
}
|
|
|
} else {
|
|
|
value = $("[name='" + name + "']:checked", that.$commonsearch).val();
|
|
|
+ value = (vObjCol && typeof vObjCol.process === 'function') ? vObjCol.process(obj.val()) : obj.val();
|
|
|
}
|
|
|
} else {
|
|
|
- value = (vObjCol && typeof vObjCol.process === 'function') ? vObjCol.process(obj.val()) : (sym == 'LIKE %...%' ? obj.val().replace(/\*/g, '%') : obj.val());
|
|
|
+ value = (vObjCol && typeof vObjCol.process === 'function') ? vObjCol.process(obj.val()) : obj.val();
|
|
|
}
|
|
|
- if (removeempty && value == '' && sym.indexOf("NULL") == -1) {
|
|
|
+ if (removeempty && (value == '' || value == null || ($.isArray(value) && value.length == 0)) && !sym.match(/null/i)) {
|
|
|
return true;
|
|
|
}
|
|
|
|
|
@@ -267,7 +217,7 @@
|
|
|
//移除empty的值
|
|
|
if (removeempty) {
|
|
|
$.each(params.filter, function (i, j) {
|
|
|
- if (j === '') {
|
|
|
+ if ((j == '' || j == null || ($.isArray(j) && j.length == 0)) && !params.op[i].match(/null/i)) {
|
|
|
delete params.filter[i];
|
|
|
delete params.op[i];
|
|
|
}
|
|
@@ -282,8 +232,10 @@
|
|
|
commonSearch: false,
|
|
|
titleForm: "Common search",
|
|
|
actionForm: "",
|
|
|
+ searchFormTemplate: "",
|
|
|
searchFormVisible: true,
|
|
|
searchClass: 'searchit',
|
|
|
+ showSearch: true,
|
|
|
renderDefault: true,
|
|
|
onCommonSearch: function (field, text) {
|
|
|
return false;
|
|
@@ -322,10 +274,10 @@
|
|
|
$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
|
|
|
|
|
|
var BootstrapTable = $.fn.bootstrapTable.Constructor,
|
|
|
- _initHeader = BootstrapTable.prototype.initHeader,
|
|
|
- _initToolbar = BootstrapTable.prototype.initToolbar,
|
|
|
- _load = BootstrapTable.prototype.load,
|
|
|
- _initSearch = BootstrapTable.prototype.initSearch;
|
|
|
+ _initHeader = BootstrapTable.prototype.initHeader,
|
|
|
+ _initToolbar = BootstrapTable.prototype.initToolbar,
|
|
|
+ _load = BootstrapTable.prototype.load,
|
|
|
+ _initSearch = BootstrapTable.prototype.initSearch;
|
|
|
|
|
|
BootstrapTable.prototype.initHeader = function () {
|
|
|
_initHeader.apply(this, Array.prototype.slice.apply(arguments));
|
|
@@ -344,12 +296,13 @@
|
|
|
}
|
|
|
|
|
|
var that = this,
|
|
|
- html = [];
|
|
|
- html.push(sprintf('<div class="columns-%s pull-%s" style="margin-top:10px;margin-bottom:10px;">', this.options.buttonsAlign, this.options.buttonsAlign));
|
|
|
- html.push(sprintf('<button class="btn btn-default%s' + '" type="button" name="commonSearch" title="%s">', that.options.iconSize === undefined ? '' : ' btn-' + that.options.iconSize, that.options.formatCommonSearch()));
|
|
|
- html.push(sprintf('<i class="%s %s"></i>', that.options.iconsPrefix, that.options.icons.commonSearchIcon))
|
|
|
- html.push('</button></div>');
|
|
|
-
|
|
|
+ html = [];
|
|
|
+ if(that.options.showSearch){
|
|
|
+ html.push(sprintf('<div class="columns-%s pull-%s" style="margin-top:10px;margin-bottom:10px;">', this.options.buttonsAlign, this.options.buttonsAlign));
|
|
|
+ html.push(sprintf('<button class="btn btn-default%s' + '" type="button" name="commonSearch" title="%s">', that.options.iconSize === undefined ? '' : ' btn-' + that.options.iconSize, that.options.formatCommonSearch()));
|
|
|
+ html.push(sprintf('<i class="%s %s"></i>', that.options.iconsPrefix, that.options.icons.commonSearchIcon))
|
|
|
+ html.push('</button></div>');
|
|
|
+ }
|
|
|
if (that.$toolbar.find(".pull-right").size() > 0) {
|
|
|
$(html.join('')).insertBefore(that.$toolbar.find(".pull-right:first"));
|
|
|
} else {
|
|
@@ -359,7 +312,7 @@
|
|
|
initCommonSearch(that.columns, that);
|
|
|
|
|
|
that.$toolbar.find('button[name="commonSearch"]')
|
|
|
- .off('click').on('click', function () {
|
|
|
+ .off('click').on('click', function () {
|
|
|
that.$commonsearch.toggleClass("hidden");
|
|
|
return;
|
|
|
});
|
|
@@ -374,7 +327,7 @@
|
|
|
var queryParams = that.options.queryParams;
|
|
|
//匹配默认搜索值
|
|
|
this.options.queryParams = function (params) {
|
|
|
- return queryParams(getQueryParams(params, getSearchQuery(this, true)));
|
|
|
+ return queryParams(getQueryParams(params, getSearchQuery(that, true)));
|
|
|
};
|
|
|
this.trigger('post-common-search', that);
|
|
|
|
|
@@ -409,8 +362,8 @@
|
|
|
var fval = fp[key].toLowerCase();
|
|
|
var value = item[key];
|
|
|
value = $.fn.bootstrapTable.utils.calculateObjectValue(that.header,
|
|
|
- that.header.formatters[$.inArray(key, that.header.fields)],
|
|
|
- [value, item, i], value);
|
|
|
+ that.header.formatters[$.inArray(key, that.header.fields)],
|
|
|
+ [value, item, i], value);
|
|
|
|
|
|
if (!($.inArray(key, that.header.fields) !== -1 &&
|
|
|
(typeof value === 'string' || typeof value === 'number') &&
|