| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280 |
- /**
- * @author: Dennis Hernández
- * @webSite: http://djhvscf.github.io/Blog
- * @version: v1.0.0
- */
- !function ($) {
- 'use strict';
- var sprintf = function (str) {
- var args = arguments,
- flag = true,
- i = 1;
- str = str.replace(/%s/g, function () {
- var arg = args[i++];
- if (typeof arg === 'undefined') {
- flag = false;
- return '';
- }
- return arg;
- });
- return flag ? str : '';
- };
- var getFieldIndex = function (columns, field) {
- var index = -1;
- $.each(columns, function (i, column) {
- if (column.field === field) {
- index = i;
- return false;
- }
- return true;
- });
- return index;
- };
- var calculateObjectValue = function (self, name, args, defaultValue) {
- if (typeof name === 'string') {
- // support obj.func1.func2
- var names = name.split('.');
- if (names.length > 1) {
- name = window;
- $.each(names, function (i, f) {
- name = name[f];
- });
- } else {
- name = window[name];
- }
- }
- if (typeof name === 'object') {
- return name;
- }
- if (typeof name === 'function') {
- return name.apply(self, args);
- }
- return defaultValue;
- };
- $.extend($.fn.bootstrapTable.defaults, {
- filterControl: false,
- onColumnSearch: function (field, text) {
- return false;
- }
- });
- $.extend($.fn.bootstrapTable.COLUMN_DEFAULTS, {
- filterControl: undefined,
- filterData: undefined
- });
- $.extend($.fn.bootstrapTable.Constructor.EVENTS, {
- 'column-search.bs.table': 'onColumnSearch'
- });
- var BootstrapTable = $.fn.bootstrapTable.Constructor,
- _initHeader = BootstrapTable.prototype.initHeader,
- _initBody = BootstrapTable.prototype.initBody,
- _initSearch = BootstrapTable.prototype.initSearch;
- BootstrapTable.prototype.initHeader = function () {
- _initHeader.apply(this, Array.prototype.slice.apply(arguments));
- if (!this.options.filterControl) {
- return;
- }
- var addedFilterControl = false,
- that = this,
- isVisible,
- html,
- timeoutId = 0;
- $.each(this.options.columns, function (i, column) {
- isVisible = 'hidden';
- html = [];
- if (!column.filterControl) {
- html.push('<div style="height: 34px;"></div>');
- } else {
- html.push('<div style="margin: 0px 2px 2px 2px;" class="filterControl">');
- if (column.filterControl && column.searchable) {
- addedFilterControl = true;
- isVisible = 'visible'
- }
- switch (column.filterControl.toLowerCase()) {
- case 'input' :
- html.push(sprintf('<input type="text" class="form-control" style="width: 100%; visibility: %s">', isVisible));
- break;
- case 'select':
- html.push(sprintf('<select class="%s form-control" style="width: 100%; visibility: %s"></select>',
- column.field, isVisible))
- break;
- }
- }
- that.$header.find(sprintf('.th-inner:contains("%s")', column.title)).next().append(html.join(''));
- if (column.filterData !== undefined && column.filterData.toLowerCase() !== 'column') {
- var filterDataType = column.filterData.substring(0, 3);
- var filterDataSource = column.filterData.substring(4, column.filterData.length);
- var selectControl = $('.' + column.field);
- selectControl.append($("<option></option>")
- .attr("value", '')
- .text(''));
- switch (filterDataType) {
- case 'url':
- $.ajax({
- url: filterDataSource,
- dataType: 'json',
- success: function (data) {
- $.each(data, function (key, value) {
- selectControl.append($("<option></option>")
- .attr("value", key)
- .text(value));
- });
- }
- });
- break;
- case 'var':
- var variableValues = window[filterDataSource];
- for (var key in variableValues) {
- selectControl.append($("<option></option>")
- .attr("value", key)
- .text(variableValues[key]));
- }
- ;
- break;
- }
- }
- });
- if (addedFilterControl) {
- this.$header.off('keyup', 'input').on('keyup', 'input', function (event) {
- clearTimeout(timeoutId);
- timeoutId = setTimeout(function () {
- that.onColumnSearch(event);
- }, that.options.searchTimeOut);
- });
- this.$header.off('change', 'select').on('change', 'select', function (event) {
- clearTimeout(timeoutId);
- timeoutId = setTimeout(function () {
- that.onColumnSearch(event);
- }, that.options.searchTimeOut);
- });
- } else {
- this.$header.find('.filterControl').hide();
- }
- };
- BootstrapTable.prototype.initBody = function () {
- _initBody.apply(this, Array.prototype.slice.apply(arguments));
- var that = this,
- data = this.getData();
- for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
- var key,
- item = data[i];
- $.each(this.header.fields, function (j, field) {
- var value = item[field],
- column = that.options.columns[getFieldIndex(that.options.columns, field)];
- value = calculateObjectValue(that.header,
- that.header.formatters[j], [value, item, i], value);
- if ((!column.checkbox) || (!column.radio)) {
- if (column.filterControl !== undefined && column.filterControl.toLowerCase() === 'select'
- && column.searchable) {
- if (column.filterData === undefined || column.filterData.toLowerCase() === 'column') {
- var selectControl = $('.' + column.field),
- iOpt = 0,
- exitsOpt = false,
- options;
- if (selectControl !== undefined) {
- options = selectControl.get(0).options;
- if (options.length === 0) {
- //Added the default option
- selectControl.append($("<option></option>")
- .attr("value", '')
- .text(''));
- selectControl.append($("<option></option>")
- .attr("value", value)
- .text(value));
- } else {
- for (; iOpt < options.length; iOpt++) {
- if (options[iOpt].value === value) {
- exitsOpt = true;
- break;
- }
- }
- if (!exitsOpt) {
- selectControl.append($("<option></option>")
- .attr("value", value)
- .text(value));
- }
- }
- }
- }
- }
- }
- });
- }
- };
- BootstrapTable.prototype.initSearch = function () {
- _initSearch.apply(this, Array.prototype.slice.apply(arguments));
- var that = this;
- var fp = $.isEmptyObject(this.filterColumnsPartial) ? null : this.filterColumnsPartial;
- //Check partial column filter
- this.data = fp ? $.grep(this.data, function (item, i) {
- for (var key in fp) {
- var fval = fp[key].toLowerCase();
- var value = item[key];
- value = 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.onColumnSearch = function (event) {
- var text = $.trim($(event.currentTarget).val());
- var $field = $(event.currentTarget).parent().parent().parent().data('field')
- 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-search', $field, text);
- };
- }(jQuery);
|