新增普通搜索bootstrap-table-commonsearch

pull/280969/MERGE^2
PPPSCN 2017-05-07 11:17:43 +08:00
parent 82cb0ef6d1
commit 99c091467a
10 changed files with 1214 additions and 915 deletions

View File

@ -36,6 +36,7 @@ return [
'Reset' => '重置',
'Execute' => '执行',
'Close' => '关闭',
'Choose' => '选择',
'Search' => '搜索',
'Refresh' => '刷新',
'First' => '首页',
@ -82,6 +83,7 @@ return [
'Network error' => '网络错误!',
'Issues & Wiki' => '问题交流',
'Advanced search' => '高级搜索',
'Common search' => '普通搜索',
'%d second%s ago' => '%d秒前',
'%d minute%s ago' => '%d分钟前',
'%d hour%s ago' => '%d小时前',

View File

@ -35,7 +35,10 @@ define(['jquery', 'bootstrap', 'backend', 'form', 'table', 'config'], function (
{field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime},
{field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
],
//普通搜索
commonSearch: true,
titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
});
// 为表格绑定事件

View File

@ -23,25 +23,27 @@ define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefin
columns: [
[
{field: 'state', checkbox: true},
{field: 'id', title: __('Id')},
{field: 'category_id', title: __('Category_id')},
{field: 'title', title: __('Title')},
{field: 'keywords', title: __('Keywords')},
{field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag},
{field: 'image', title: __('Image'), formatter: Table.api.formatter.image},
{field: 'icon', title: __('Icon'), formatter: Table.api.formatter.icon},
{field: 'views', title: __('Views')},
{field: 'comments', title: __('Comments')},
{field: 'weigh', title: __('Weigh')},
{field: 'id', title: __('Id'), operate: false},
{field: 'category_id', title: __('Category_id'), operate: '='},
{field: 'title', title: __('Title'), operate: 'LIKE %...%'},
{field: 'keywords', title: __('Keywords'), operate: 'LIKE %...%'},
{field: 'flag', title: __('Flag'), formatter: Table.api.formatter.flag, operate: false},
{field: 'image', title: __('Image'), formatter: Table.api.formatter.image, operate: false},
{field: 'icon', title: __('Icon'), formatter: Table.api.formatter.icon, operate: false},
{field: 'views', title: __('Views'), operate: false},
{field: 'comments', title: __('Comments'), operate: false},
{field: 'weigh', title: __('Weigh'), operate: false},
{field: 'status', title: __('Status'), formatter: Table.api.formatter.status},
{field: 'operate', title: __('Operate'), events: Table.api.events.operate, formatter: Table.api.formatter.operate}
]
]
],
//普通搜索
commonSearch: true,
titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
});
// 为表格绑定事件
Table.api.bindevent(table);
parent.window.Backend.api.sidebar({'auth/admin':44});
},
add: function () {
Controller.api.bindevent();

View File

@ -152,19 +152,19 @@
});
$.extend($.fn.bootstrapTable.locales, {
formatAdvancedSearch: function () {
return 'Advanced search';
return __('Advanced search');
},
formatAdvancedSubmitButton: function () {
return "Submit";
return __("Submit");
},
formatAdvancedResetButton: function () {
return "Reset";
return __("Reset");
},
formatAdvancedCloseButton: function () {
return "Close";
return __("Close");
},
formatAdvancedChoose: function () {
return "Choose";
return __("Choose");
}
});

View File

@ -0,0 +1,270 @@
/**
* @author: pppscn <35696959@qq.com>
* @version: v0.0.1
*
* @update 2017-05-07 <http://git.oschina.net/pp/fastadmin>
*/
!function ($) {
'use strict';
var firstLoad = false;
var sprintf = $.fn.bootstrapTable.utils.sprintf;
var showCommonSearch = function (pColumns, that) {
var vFormCommon = createFormCommon(pColumns, that), timeoutId = 0;
var vModal = sprintf("<div id=\"commonSearchModalContent_%s\" class=\"well bs-component\">", that.options.idTable);
vModal += vFormCommon.join('');
vModal += "</div>";
$("#myTabContent").before($(vModal));
if (that.options.sidePagination != 'server' || !that.options.url) {
$('#' + that.options.idForm).off('keyup blur', 'input').on('keyup blur', 'input', function (event) {
clearTimeout(timeoutId);
timeoutId = setTimeout(function () {
that.onColumnCommonSearch(event);
}, that.options.searchTimeOut);
});
}
// 提交搜索
$("#btnSubmitCommon" + "_" + that.options.idTable).click(function (event) {
that.onColumnCommonSearch();
});
// 重置搜索
$("#btnResetCommon" + "_" + that.options.idTable).click(function () {
$("#commonSearchModalContent" + "_" + that.options.idTable + " form")[0].reset();
that.onColumnCommonSearch();
});
};
var createFormCommon = function (pColumns, that) {
var htmlForm = [];
var opList = ['=', '>', '>=', '<', '<=', '!=', 'LIKE', 'LIKE %...%', 'NOT LIKE', 'IN(...)', 'NOT IN(...)', 'BETWEEN', 'NOT BETWEEN', 'IS NULL', 'IS NOT NULL'];
//var selectList = [];
//for (var i = 0; i < opList.length; i++) {
// selectList.push("<option value='" + opList[i] + "'>" + opList[i] + "</option>");
//}
//var selectHtml = selectList.join('');
htmlForm.push(sprintf('<form class="form-inline" id="%s" action="%s" >', that.options.idForm, that.options.actionForm));
htmlForm.push('<fieldset>');
if (that.options.titleForm.length > 0)
htmlForm.push(sprintf("<legend>%s</legend>", that.options.titleForm));
for (var i in pColumns) {
var vObjCol = pColumns[i];
if (!vObjCol.checkbox && vObjCol.field !== 'operate' && vObjCol.visible && vObjCol.searchable && vObjCol.operate !== false) {
htmlForm.push('<div class="form-group" style="margin:0 5px;">');
htmlForm.push(sprintf('<label for="%s" class="control-label" style="padding:0 10px">%s</label>', vObjCol.field, vObjCol.title));
if (that.options.sidePagination == 'server' && that.options.url) {
//htmlForm.push('<div class="col-sm-2">');
//htmlForm.push(sprintf('<select class="form-control" name="field-%s" data-name="%s">%s</select>', vObjCol.field, vObjCol.field, selectHtml));
vObjCol.operate = (typeof vObjCol.operate === 'undefined' || $.inArray(vObjCol.operate, opList) === -1) ? '=' : vObjCol.operate;
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));
//htmlForm.push('</div>');
}
//htmlForm.push('<div class="col-sm-8">');
if (vObjCol.searchList) {
if (typeof vObjCol.searchList == 'function') {
htmlForm.push(vObjCol.searchList.call(this, vObjCol));
} else {
var isArray = vObjCol.searchList.constructor === Array;
var searchList = [];
searchList.push(sprintf('<option value="">%s</option>', $.fn.bootstrapTable.locales.formatCommonChoose()));
$.each(vObjCol.searchList, function (key, value) {
searchList.push("<option value='" + (isArray ? value : key) + "'>" + value + "</option>");
});
htmlForm.push(sprintf('<select class="form-control" name="%s">%s</select>', vObjCol.field, searchList.join('')));
}
} else {
htmlForm.push(sprintf('<input type="text" class="form-control" name="%s" placeholder="%s" id="%s">', vObjCol.field, vObjCol.title, vObjCol.field));
}
//htmlForm.push('</div>');
htmlForm.push('</div>');
}
}
htmlForm.push('<div class="form-group" style="margin:0 5px;">');
htmlForm.push(createFormBtn(that).join(''));
htmlForm.push('</div>');
htmlForm.push('</fieldset>');
htmlForm.push('</form>');
return htmlForm;
};
var createFormBtn = function (that) {
var htmlBtn = [];
var searchSubmit = that.options.formatCommonSubmitButton();
var searchReset = that.options.formatCommonResetButton();
var searchClose = that.options.formatCommonCloseButton();
htmlBtn.push('<div class="form-group">');
htmlBtn.push('<div class="col-sm-12 text-center">');
if (that.options.sidePagination == 'server' && that.options.url) {
htmlBtn.push(sprintf('<button type="button" id="btnSubmitCommon%s" class="btn btn-success" >%s</button> ', "_" + that.options.idTable, searchSubmit));
htmlBtn.push(sprintf('<button type="button" id="btnResetCommon%s" class="btn btn-default" >%s</button> ', "_" + that.options.idTable, searchReset));
} else {
htmlBtn.push(sprintf('<button type="button" id="btnCloseCommon%s" data-dismiss="modal" class="btn btn-default" >%s</button> ', "_" + that.options.idTable, searchClose));
}
htmlBtn.push('</div>');
htmlBtn.push('</div>');
return htmlBtn;
};
$.extend($.fn.bootstrapTable.defaults, {
commonSearch: false,
idForm: 'commonSearch',
titleForm: __("Common search"),
actionForm: '',
idTable: undefined,
onColumnCommonSearch: function (field, text) {
return false;
}
});
$.extend($.fn.bootstrapTable.defaults.icons, {
commonSearchIcon: 'glyphicon-search'
});
$.extend($.fn.bootstrapTable.Constructor.EVENTS, {
'column-common-search.bs.table': 'onColumnCommonSearch'
});
$.extend($.fn.bootstrapTable.locales, {
formatCommonSearch: function () {
return __("Common search");
},
formatCommonSubmitButton: function () {
return __("Submit");
},
formatCommonResetButton: function () {
return __("Reset");
},
formatCommonCloseButton: function () {
return __("Close");
},
formatCommonChoose: function () {
return __("Choose");
}
});
$.extend($.fn.bootstrapTable.defaults, $.fn.bootstrapTable.locales);
var BootstrapTable = $.fn.bootstrapTable.Constructor,
_initToolbar = BootstrapTable.prototype.initToolbar,
_load = BootstrapTable.prototype.load,
_initSearch = BootstrapTable.prototype.initSearch;
BootstrapTable.prototype.initToolbar = function () {
_initToolbar.apply(this, Array.prototype.slice.apply(arguments));
if (!this.options.search) {
return;
}
if (!this.options.commonSearch) {
return;
}
if (!this.options.idTable) {
return;
}
var that = this;
showCommonSearch(that.columns, that);
};
BootstrapTable.prototype.load = function (data) {
_load.apply(this, Array.prototype.slice.apply(arguments));
if (!this.options.commonSearch) {
return;
}
if (typeof this.options.idTable === 'undefined') {
return;
} else {
if (!firstLoad) {
var height = parseInt($(".bootstrap-table").height());
height += 10;
$("#" + this.options.idTable).bootstrapTable("resetView", {height: height});
firstLoad = true;
}
}
};
BootstrapTable.prototype.initSearch = function () {
_initSearch.apply(this, Array.prototype.slice.apply(arguments));
if (!this.options.commonSearch) {
return;
}
var that = this;
var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
this.data = fp ? $.grep(this.data, function (item, i) {
for (var key in fp) {
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);
if (!($.inArray(key, that.header.fields) !== -1 &&
(typeof value === 'string' || typeof value === 'number') &&
(value + '').toLowerCase().indexOf(fval) !== -1)) {
return false;
}
}
return true;
}) : this.data;
};
BootstrapTable.prototype.onColumnCommonSearch = function (event) {
if (typeof event === 'undefined') {
var op = {};
var filter = {};
$("#commonSearchModalContent_" + this.options.idTable + " input.operate").each(function () {
var name = $(this).data("name");
var sym = $(this).val();
var obj = $("[name='" + name + "']");
if (obj.size() == 0)
return true;
var value = obj.size() > 1 ? $("[name='" + name + "']:checked").val() : obj.val();
if (value == '' && sym.indexOf("NULL") == -1) {
return true;
}
op[name] = sym;
filter[name] = value;
});
// 追加查询关键字
this.options.pageNumber = 1;
this.refresh({query: {filter: JSON.stringify(filter), op: JSON.stringify(op)}});
} else {
var text = $.trim($(event.currentTarget).val());
var $field = $(event.currentTarget)[0].id;
if ($.isEmptyObject(this.filterColumnsPartial)) {
this.filterColumnsPartial = {};
}
if (text) {
this.filterColumnsPartial[$field] = text;
} else {
delete this.filterColumnsPartial[$field];
}
this.options.pageNumber = 1;
this.onSearch(event);
// this.updatePagination();
this.trigger('column-common-search', $field, text);
}
};
}(jQuery);

View File

@ -34,6 +34,7 @@ require.config({
'bootstrap-table-export': '../libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min',
'bootstrap-table-mobile': '../libs/bootstrap-table/dist/extensions/mobile/bootstrap-table-mobile',
'bootstrap-table-advancedsearch': 'bootstrap-table-advancedsearch',
'bootstrap-table-commonsearch': 'bootstrap-table-commonsearch',
'bootstrap-table-lang': '../libs/bootstrap-table/dist/locale/bootstrap-table-zh-CN',
'typeahead': '../libs/typeahead.js/dist/typeahead.jquery.min',
'bloodhound': '../libs/typeahead.js/dist/bloodhound.min',
@ -78,6 +79,10 @@ require.config({
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'bootstrap-table-commonsearch': {
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'tableexport': {
deps: ['jquery'],
exports: '$.fn.extend'

View File

@ -51,6 +51,7 @@ require.config({
'bootstrap-table-export': '../libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min',
'bootstrap-table-mobile': '../libs/bootstrap-table/dist/extensions/mobile/bootstrap-table-mobile',
'bootstrap-table-advancedsearch': 'bootstrap-table-advancedsearch',
'bootstrap-table-commonsearch': 'bootstrap-table-commonsearch',
'bootstrap-table-lang': '../libs/bootstrap-table/dist/locale/bootstrap-table-zh-CN',
'typeahead': '../libs/typeahead.js/dist/typeahead.jquery.min',
'bloodhound': '../libs/typeahead.js/dist/bloodhound.min',
@ -95,6 +96,10 @@ require.config({
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'bootstrap-table-commonsearch': {
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'tableexport': {
deps: ['jquery'],
exports: '$.fn.extend'

View File

@ -34,6 +34,7 @@ require.config({
'bootstrap-table-export': '../libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min',
'bootstrap-table-mobile': '../libs/bootstrap-table/dist/extensions/mobile/bootstrap-table-mobile',
'bootstrap-table-advancedsearch': 'bootstrap-table-advancedsearch',
'bootstrap-table-commonsearch': 'bootstrap-table-commonsearch',
'bootstrap-table-lang': '../libs/bootstrap-table/dist/locale/bootstrap-table-zh-CN',
'typeahead': '../libs/typeahead.js/dist/typeahead.jquery.min',
'bloodhound': '../libs/typeahead.js/dist/bloodhound.min',
@ -78,6 +79,10 @@ require.config({
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'bootstrap-table-commonsearch': {
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'tableexport': {
deps: ['jquery'],
exports: '$.fn.extend'

View File

@ -51,6 +51,7 @@ require.config({
'bootstrap-table-export': '../libs/bootstrap-table/dist/extensions/export/bootstrap-table-export.min',
'bootstrap-table-mobile': '../libs/bootstrap-table/dist/extensions/mobile/bootstrap-table-mobile',
'bootstrap-table-advancedsearch': 'bootstrap-table-advancedsearch',
'bootstrap-table-commonsearch': 'bootstrap-table-commonsearch',
'bootstrap-table-lang': '../libs/bootstrap-table/dist/locale/bootstrap-table-zh-CN',
'typeahead': '../libs/typeahead.js/dist/typeahead.jquery.min',
'bloodhound': '../libs/typeahead.js/dist/bloodhound.min',
@ -95,6 +96,10 @@ require.config({
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'bootstrap-table-commonsearch': {
deps: ['bootstrap-table'],
exports: '$.fn.bootstrapTable.defaults'
},
'tableexport': {
deps: ['jquery'],
exports: '$.fn.extend'

View File

@ -1,4 +1,4 @@
define(['jquery', 'bootstrap', 'backend', 'config', 'toastr', 'moment', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-mobile', 'bootstrap-table-export', 'bootstrap-table-advancedsearch'], function ($, undefined, Backend, Config, Toastr, Moment) {
define(['jquery', 'bootstrap', 'backend', 'config', 'toastr', 'moment', 'bootstrap-table', 'bootstrap-table-lang', 'bootstrap-table-mobile', 'bootstrap-table-export', 'bootstrap-table-advancedsearch', 'bootstrap-table-commonsearch'], function ($, undefined, Backend, Config, Toastr, Moment) {
var Table = {
list: {},
@ -10,7 +10,9 @@ define(['jquery', 'bootstrap', 'backend', 'config', 'toastr', 'moment', 'bootstr
toolbar: "#toolbar",
search: true,
cache: false,
advancedSearch: true,
advancedSearch: false,
commonSearch: false,
titleForm: '', //为空则不显示标题,不定义默认显示:普通搜索
idTable: 'advancedTable',
showExport: true,
exportDataType: "all",