bootstrap table支持高度百分比的实例代码
程序员文章站
2022-04-21 08:41:57
更改bootstraptable.prototype.resetview 方法,以支持高度百分比定义,适应不同高度屏幕
bootstraptable.protot...
更改bootstraptable.prototype.resetview
方法,以支持高度百分比定义,适应不同高度屏幕
bootstraptable.prototype.resetview = function (params) { var padding = 0; if (params && params.height) { this.options.height = params.height; } this.$selectall.prop('checked', this.$selectitem.length > 0 && this.$selectitem.length === this.$selectitem.filter(':checked').length); if (this.options.height) { var toolbarheight = this.$toolbar.outerheight(true), paginationheight = this.$pagination.outerheight(true), height = this.options.height; //关键代码 if (this.options.height.tostring().indexof("%") != -1) { height = $(window).height() * (parsefloat(this.options.height) / 100); } height = height - toolbarheight - paginationheight; this.$tablecontainer.css('height', height + 'px'); } if (this.options.cardview) { // remove the element css this.$el.css('margin-top', '0'); this.$tablecontainer.css('padding-bottom', '0'); this.$tablefooter.hide(); return; } if (this.options.showheader && this.options.height) { this.$tableheader.show(); this.resetheader(); padding += this.$header.outerheight(); } else { this.$tableheader.hide(); this.trigger('post-header'); } if (this.options.showfooter) { this.resetfooter(); if (this.options.height) { padding += this.$tablefooter.outerheight() + 1; } } // assign the correct sortable arrow this.getcaret(); this.$tablecontainer.css('padding-bottom', padding + 'px'); this.trigger('reset-view'); };
更改后的bootstrap-table.js的完整代码:
/** * @author zhixin wen <wenzhixin2010@gmail.com> * version: 1.11.1 * https://github.com/wenzhixin/bootstrap-table/ */ (function ($) { 'use strict'; // tools definition // ====================== var cachedwidth = null; // it only does '%s', and return '' when arguments are undefined 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 getpropertyfromother = function (list, from, to, value) { var result = ''; $.each(list, function (i, item) { if (item[from] === value) { result = item[to]; return false; } return true; }); return result; }; 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; }; // http://jsfiddle.net/wenyi/47nz7ez9/3/ var setfieldindex = function (columns) { var i, j, k, totalcol = 0, flag = []; for (i = 0; i < columns[0].length; i++) { totalcol += columns[0][i].colspan || 1; } for (i = 0; i < columns.length; i++) { flag[i] = []; for (j = 0; j < totalcol; j++) { flag[i][j] = false; } } for (i = 0; i < columns.length; i++) { for (j = 0; j < columns[i].length; j++) { var r = columns[i][j], rowspan = r.rowspan || 1, colspan = r.colspan || 1, index = $.inarray(false, flag[i]); if (colspan === 1) { r.fieldindex = index; // when field is undefined, use index instead if (typeof r.field === 'undefined') { r.field = index; } } for (k = 0; k < rowspan; k++) { flag[i + k][index] = true; } for (k = 0; k < colspan; k++) { flag[i][index + k] = true; } } } }; var getscrollbarwidth = function () { if (cachedwidth === null) { var inner = $('<p/>').addclass('fixed-table-scroll-inner'), outer = $('<div/>').addclass('fixed-table-scroll-outer'), w1, w2; outer.append(inner); $('body').append(outer); w1 = inner[0].offsetwidth; outer.css('overflow', 'scroll'); w2 = inner[0].offsetwidth; if (w1 === w2) { w2 = outer[0].clientwidth; } outer.remove(); cachedwidth = w1 - w2; } return cachedwidth; }; var calculateobjectvalue = function (self, name, args, defaultvalue) { var func = name; if (typeof name === 'string') { // support obj.func1.func2 var names = name.split('.'); if (names.length > 1) { func = window; $.each(names, function (i, f) { func = func[f]; }); } else { func = window[name]; } } if (typeof func === 'object') { return func; } if (typeof func === 'function') { return func.apply(self, args || []); } if (!func && typeof name === 'string' && sprintf.apply(this, [name].concat(args))) { return sprintf.apply(this, [name].concat(args)); } return defaultvalue; }; var compareobjects = function (objecta, objectb, comparelength) { // create arrays of property names var objectaproperties = object.getownpropertynames(objecta), objectbproperties = object.getownpropertynames(objectb), propname = ''; if (comparelength) { // if number of properties is different, objects are not equivalent if (objectaproperties.length !== objectbproperties.length) { return false; } } for (var i = 0; i < objectaproperties.length; i++) { propname = objectaproperties[i]; // if the property is not in the object b properties, continue with the next property if ($.inarray(propname, objectbproperties) > -1) { // if values of same property are not equal, objects are not equivalent if (objecta[propname] !== objectb[propname]) { return false; } } } // if we made it this far, objects are considered equivalent return true; }; var escapehtml = function (text) { if (typeof text === 'string') { return text .replace(/&/g, '&') .replace(/</g, '<') .replace(/>/g, '>') .replace(/"/g, '"') .replace(/'/g, ''') .replace(/`/g, '`'); } return text; }; var getrealdataattr = function (dataattr) { for (var attr in dataattr) { var auxattr = attr.split(/(?=[a-z])/).join('-').tolowercase(); if (auxattr !== attr) { dataattr[auxattr] = dataattr[attr]; delete dataattr[attr]; } } return dataattr; }; var getitemfield = function (item, field, escape) { var value = item; if (typeof field !== 'string' || item.hasownproperty(field)) { return escape ? escapehtml(item[field]) : item[field]; } var props = field.split('.'); for (var p in props) { if (props.hasownproperty(p)) { value = value && value[props[p]]; } } return escape ? escapehtml(value) : value; }; var isiebrowser = function () { return !!(navigator.useragent.indexof("msie ") > 0 || !!navigator.useragent.match(/trident.*rv\:11\./)); }; var objectkeys = function () { // from https://developer.mozilla.org/en-us/docs/web/javascript/reference/global_objects/object/keys if (!object.keys) { object.keys = (function () { var hasownproperty = object.prototype.hasownproperty, hasdontenumbug = !({ tostring: null }).propertyisenumerable('tostring'), dontenums = [ 'tostring', 'tolocalestring', 'valueof', 'hasownproperty', 'isprototypeof', 'propertyisenumerable', 'constructor' ], dontenumslength = dontenums.length; return function (obj) { if (typeof obj !== 'object' && (typeof obj !== 'function' || obj === null)) { throw new typeerror('object.keys called on non-object'); } var result = [], prop, i; for (prop in obj) { if (hasownproperty.call(obj, prop)) { result.push(prop); } } if (hasdontenumbug) { for (i = 0; i < dontenumslength; i++) { if (hasownproperty.call(obj, dontenums[i])) { result.push(dontenums[i]); } } } return result; }; }()); } }; // bootstrap table class definition // ====================== var bootstraptable = function (el, options) { this.options = options; this.$el = $(el); this.$el_ = this.$el.clone(); this.timeoutid_ = 0; this.timeoutfooter_ = 0; this.init(); }; bootstraptable.defaults = { classes: 'table table-hover', sortclass: undefined, locale: undefined, height: undefined, undefinedtext: '-', sortname: undefined, sortorder: 'asc', sortstable: false, striped: false, columns: [[]], data: [], totalfield: 'total', datafield: 'rows', method: 'get', url: undefined, ajax: undefined, cache: true, contenttype: 'application/json', datatype: 'json', ajaxoptions: {}, queryparams: function (params) { return params; }, queryparamstype: 'limit', // undefined responsehandler: function (res) { return res; }, pagination: false, onlyinfopagination: false, paginationloop: true, sidepagination: 'client', // client or server totalrows: 0, // server side need to set pagenumber: 1, pagesize: 10, pagelist: [10, 25, 50, 100], paginationhalign: 'right', //right, left paginationvalign: 'bottom', //bottom, top, both paginationdetailhalign: 'left', //right, left paginationpretext: '‹', paginationnexttext: '›', search: false, searchonenterkey: false, strictsearch: false, searchalign: 'right', selectitemname: 'btselectitem', showheader: true, showfooter: false, showcolumns: false, showpaginationswitch: false, showrefresh: false, showtoggle: false, buttonsalign: 'right', smartdisplay: true, escape: false, minimumcountcolumns: 1, idfield: undefined, uniqueid: undefined, cardview: false, detailview: false, detailformatter: function (index, row) { return ''; }, trimonsearch: true, clicktoselect: false, singleselect: false, toolbar: undefined, toolbaralign: 'left', checkboxheader: true, sortable: true, silentsort: true, maintainselected: false, searchtimeout: 500, searchtext: '', iconsize: undefined, buttonsclass: 'default', iconsprefix: 'glyphicon', // glyphicon of fa (font awesome) icons: { paginationswitchdown: 'glyphicon-collapse-down icon-chevron-down', paginationswitchup: 'glyphicon-collapse-up icon-chevron-up', refresh: 'glyphicon-refresh icon-refresh', toggle: 'glyphicon-list-alt icon-list-alt', columns: 'glyphicon-th icon-th', detailopen: 'glyphicon-plus icon-plus', detailclose: 'glyphicon-minus icon-minus' }, customsearch: $.noop, customsort: $.noop, rowstyle: function (row, index) { return {}; }, rowattributes: function (row, index) { return {}; }, footerstyle: function (row, index) { return {}; }, onall: function (name, args) { return false; }, onclickcell: function (field, value, row, $element) { return false; }, ondblclickcell: function (field, value, row, $element) { return false; }, onclickrow: function (item, $element) { return false; }, ondblclickrow: function (item, $element) { return false; }, onsort: function (name, order) { return false; }, oncheck: function (row) { return false; }, onuncheck: function (row) { return false; }, oncheckall: function (rows) { return false; }, onuncheckall: function (rows) { return false; }, onchecksome: function (rows) { return false; }, onunchecksome: function (rows) { return false; }, onloadsuccess: function (data) { return false; }, onloaderror: function (status) { return false; }, oncolumnswitch: function (field, checked) { return false; }, onpagechange: function (number, size) { return false; }, onsearch: function (text) { return false; }, ontoggle: function (cardview) { return false; }, onprebody: function (data) { return false; }, onpostbody: function () { return false; }, onpostheader: function () { return false; }, onexpandrow: function (index, row, $detail) { return false; }, oncollapserow: function (index, row) { return false; }, onrefreshoptions: function (options) { return false; }, onrefresh: function (params) { return false; }, onresetview: function () { return false; } }; bootstraptable.locales = {}; bootstraptable.locales['en-us'] = bootstraptable.locales.en = { formatloadingmessage: function () { return 'loading, please wait...'; }, formatrecordsperpage: function (pagenumber) { return sprintf('%s rows per page', pagenumber); }, formatshowingrows: function (pagefrom, pageto, totalrows) { return sprintf('showing %s to %s of %s rows', pagefrom, pageto, totalrows); }, formatdetailpagination: function (totalrows) { return sprintf('showing %s rows', totalrows); }, formatsearch: function () { return 'search'; }, formatnomatches: function () { return 'no matching records found'; }, formatpaginationswitch: function () { return 'hide/show pagination'; }, formatrefresh: function () { return 'refresh'; }, formattoggle: function () { return 'toggle'; }, formatcolumns: function () { return 'columns'; }, formatallrows: function () { return 'all'; } }; $.extend(bootstraptable.defaults, bootstraptable.locales['en-us']); bootstraptable.column_defaults = { radio: false, checkbox: false, checkboxenabled: true, field: undefined, title: undefined, titletooltip: undefined, 'class': undefined, align: undefined, // left, right, center halign: undefined, // left, right, center falign: undefined, // left, right, center valign: undefined, // top, middle, bottom width: undefined, sortable: false, order: 'asc', // asc, desc visible: true, switchable: true, clicktoselect: true, formatter: undefined, footerformatter: undefined, events: undefined, sorter: undefined, sortname: undefined, cellstyle: undefined, searchable: true, searchformatter: true, cardvisible: true, escape: false }; bootstraptable.events = { 'all.bs.table': 'onall', 'click-cell.bs.table': 'onclickcell', 'dbl-click-cell.bs.table': 'ondblclickcell', 'click-row.bs.table': 'onclickrow', 'dbl-click-row.bs.table': 'ondblclickrow', 'sort.bs.table': 'onsort', 'check.bs.table': 'oncheck', 'uncheck.bs.table': 'onuncheck', 'check-all.bs.table': 'oncheckall', 'uncheck-all.bs.table': 'onuncheckall', 'check-some.bs.table': 'onchecksome', 'uncheck-some.bs.table': 'onunchecksome', 'load-success.bs.table': 'onloadsuccess', 'load-error.bs.table': 'onloaderror', 'column-switch.bs.table': 'oncolumnswitch', 'page-change.bs.table': 'onpagechange', 'search.bs.table': 'onsearch', 'toggle.bs.table': 'ontoggle', 'pre-body.bs.table': 'onprebody', 'post-body.bs.table': 'onpostbody', 'post-header.bs.table': 'onpostheader', 'expand-row.bs.table': 'onexpandrow', 'collapse-row.bs.table': 'oncollapserow', 'refresh-options.bs.table': 'onrefreshoptions', 'reset-view.bs.table': 'onresetview', 'refresh.bs.table': 'onrefresh' }; bootstraptable.prototype.init = function () { this.initlocale(); this.initcontainer(); this.inittable(); this.initheader(); this.initdata(); this.inithiddenrows(); this.initfooter(); this.inittoolbar(); this.initpagination(); this.initbody(); this.initsearchtext(); this.initserver(); }; bootstraptable.prototype.initlocale = function () { if (this.options.locale) { var parts = this.options.locale.split(/-|_/); parts[0].tolowercase(); if (parts[1]) parts[1].touppercase(); if ($.fn.bootstraptable.locales[this.options.locale]) { // locale as requested $.extend(this.options, $.fn.bootstraptable.locales[this.options.locale]); } else if ($.fn.bootstraptable.locales[parts.join('-')]) { // locale with sep set to - (in case original was specified with _) $.extend(this.options, $.fn.bootstraptable.locales[parts.join('-')]); } else if ($.fn.bootstraptable.locales[parts[0]]) { // short locale language code (i.e. 'en') $.extend(this.options, $.fn.bootstraptable.locales[parts[0]]); } } }; bootstraptable.prototype.initcontainer = function () { this.$container = $([ '<div class="bootstrap-table">', '<div class="fixed-table-toolbar"></div>', this.options.paginationvalign === 'top' || this.options.paginationvalign === 'both' ? '<div class="fixed-table-pagination" style="clear: both;"></div>' : '', '<div class="fixed-table-container">', '<div class="fixed-table-header"><table></table></div>', '<div class="fixed-table-body">', '<div class="fixed-table-loading">', this.options.formatloadingmessage(), '</div>', '</div>', '<div class="fixed-table-footer"><table><tr></tr></table></div>', this.options.paginationvalign === 'bottom' || this.options.paginationvalign === 'both' ? '<div class="fixed-table-pagination"></div>' : '', '</div>', '</div>' ].join('')); this.$container.insertafter(this.$el); this.$tablecontainer = this.$container.find('.fixed-table-container'); this.$tableheader = this.$container.find('.fixed-table-header'); this.$tablebody = this.$container.find('.fixed-table-body'); this.$tableloading = this.$container.find('.fixed-table-loading'); this.$tablefooter = this.$container.find('.fixed-table-footer'); this.$toolbar = this.$container.find('.fixed-table-toolbar'); this.$pagination = this.$container.find('.fixed-table-pagination'); this.$tablebody.append(this.$el); this.$container.after('<div class="clearfix"></div>'); this.$el.addclass(this.options.classes); if (this.options.striped) { this.$el.addclass('table-striped'); } if ($.inarray('table-no-bordered', this.options.classes.split(' ')) !== -1) { this.$tablecontainer.addclass('table-no-bordered'); } }; bootstraptable.prototype.inittable = function () { var that = this, columns = [], data = []; this.$header = this.$el.find('>thead'); if (!this.$header.length) { this.$header = $('<thead></thead>').appendto(this.$el); } this.$header.find('tr').each(function () { var column = []; $(this).find('th').each(function () { // fix #2014 - getfieldindex and elsewhere assume this is string, causes issues if not if (typeof $(this).data('field') !== 'undefined') { $(this).data('field', $(this).data('field') + ''); } column.push($.extend({}, { title: $(this).html(), 'class': $(this).attr('class'), titletooltip: $(this).attr('title'), rowspan: $(this).attr('rowspan') ? +$(this).attr('rowspan') : undefined, colspan: $(this).attr('colspan') ? +$(this).attr('colspan') : undefined }, $(this).data())); }); columns.push(column); }); if (!$.isarray(this.options.columns[0])) { this.options.columns = [this.options.columns]; } this.options.columns = $.extend(true, [], columns, this.options.columns); this.columns = []; setfieldindex(this.options.columns); $.each(this.options.columns, function (i, columns) { $.each(columns, function (j, column) { column = $.extend({}, bootstraptable.column_defaults, column); if (typeof column.fieldindex !== 'undefined') { that.columns[column.fieldindex] = column; } that.options.columns[i][j] = column; }); }); // if options.data is setting, do not process tbody data if (this.options.data.length) { return; } var m = []; this.$el.find('>tbody>tr').each(function (y) { var row = {}; // save tr's id, class and data-* attributes row._id = $(this).attr('id'); row._class = $(this).attr('class'); row._data = getrealdataattr($(this).data()); $(this).find('>td').each(function (x) { var $this = $(this), cspan = +$this.attr('colspan') || 1, rspan = +$this.attr('rowspan') || 1, tx, ty; for (; m[y] && m[y][x]; x++); //skip already occupied cells in current row for (tx = x; tx < x + cspan; tx++) { //mark matrix elements occupied by current cell with true for (ty = y; ty < y + rspan; ty++) { if (!m[ty]) { //fill missing rows m[ty] = []; } m[ty][tx] = true; } } var field = that.columns[x].field; row[field] = $(this).html(); // save td's id, class and data-* attributes row['_' + field + '_id'] = $(this).attr('id'); row['_' + field + '_class'] = $(this).attr('class'); row['_' + field + '_rowspan'] = $(this).attr('rowspan'); row['_' + field + '_colspan'] = $(this).attr('colspan'); row['_' + field + '_title'] = $(this).attr('title'); row['_' + field + '_data'] = getrealdataattr($(this).data()); }); data.push(row); }); this.options.data = data; if (data.length) this.fromhtml = true; }; bootstraptable.prototype.initheader = function () { var that = this, visiblecolumns = {}, html = []; this.header = { fields: [], styles: [], classes: [], formatters: [], events: [], sorters: [], sortnames: [], cellstyles: [], searchables: [] }; $.each(this.options.columns, function (i, columns) { html.push('<tr>'); if (i === 0 && !that.options.cardview && that.options.detailview) { html.push(sprintf('<th class="detail" rowspan="%s"><div class="fht-cell"></div></th>', that.options.columns.length)); } $.each(columns, function (j, column) { var text = '', halign = '', // header align style align = '', // body align style style = '', class_ = sprintf(' class="%s"', column['class']), order = that.options.sortorder || column.order, unitwidth = 'px', width = column.width; if (column.width !== undefined && (!that.options.cardview)) { if (typeof column.width === 'string') { if (column.width.indexof('%') !== -1) { unitwidth = '%'; } } } if (column.width && typeof column.width === 'string') { width = column.width.replace('%', '').replace('px', ''); } halign = sprintf('text-align: %s; ', column.halign ? column.halign : column.align); align = sprintf('text-align: %s; ', column.align); style = sprintf('vertical-align: %s; ', column.valign); style += sprintf('width: %s; ', (column.checkbox || column.radio) && !width ? '36px' : (width ? width + unitwidth : undefined)); if (typeof column.fieldindex !== 'undefined') { that.header.fields[column.fieldindex] = column.field; that.header.styles[column.fieldindex] = align + style; that.header.classes[column.fieldindex] = class_; that.header.formatters[column.fieldindex] = column.formatter; that.header.events[column.fieldindex] = column.events; that.header.sorters[column.fieldindex] = column.sorter; that.header.sortnames[column.fieldindex] = column.sortname; that.header.cellstyles[column.fieldindex] = column.cellstyle; that.header.searchables[column.fieldindex] = column.searchable; if (!column.visible) { return; } if (that.options.cardview && (!column.cardvisible)) { return; } visiblecolumns[column.field] = column; } html.push('<th' + sprintf(' title="%s"', column.titletooltip), column.checkbox || column.radio ? sprintf(' class="bs-checkbox %s"', column['class'] || '') : class_, sprintf(' style="%s"', halign + style), sprintf(' rowspan="%s"', column.rowspan), sprintf(' colspan="%s"', column.colspan), sprintf(' data-field="%s"', column.field), '>'); html.push(sprintf('<div class="th-inner %s">', that.options.sortable && column.sortable ? 'sortable both' : '')); text = that.options.escape ? escapehtml(column.title) : column.title; if (column.checkbox) { if (!that.options.singleselect && that.options.checkboxheader) { text = '<input name="btselectall" type="checkbox" />'; } that.header.statefield = column.field; } if (column.radio) { text = ''; that.header.statefield = column.field; that.options.singleselect = true; } html.push(text); html.push('</div>'); html.push('<div class="fht-cell"></div>'); html.push('</div>'); html.push('</th>'); }); html.push('</tr>'); }); this.$header.html(html.join('')); this.$header.find('th[data-field]').each(function (i) { $(this).data(visiblecolumns[$(this).data('field')]); }); this.$container.off('click', '.th-inner').on('click', '.th-inner', function (event) { var target = $(this); if (that.options.detailview) { if (target.closest('.bootstrap-table')[0] !== that.$container[0]) return false; } if (that.options.sortable && target.parent().data().sortable) { that.onsort(event); } }); this.$header.children().children().off('keypress').on('keypress', function (event) { if (that.options.sortable && $(this).data().sortable) { var code = event.keycode || event.which; if (code == 13) { //enter keycode that.onsort(event); } } }); $(window).off('resize.bootstrap-table'); if (!this.options.showheader || this.options.cardview) { this.$header.hide(); this.$tableheader.hide(); this.$tableloading.css('top', 0); } else { this.$header.show(); this.$tableheader.show(); this.$tableloading.css('top', this.$header.outerheight() + 1); // assign the correct sortable arrow this.getcaret(); $(window).on('resize.bootstrap-table', $.proxy(this.resetwidth, this)); } this.$selectall = this.$header.find('[name="btselectall"]'); this.$selectall.off('click').on('click', function () { var checked = $(this).prop('checked'); that[checked ? 'checkall' : 'uncheckall'](); that.updateselected(); }); }; bootstraptable.prototype.initfooter = function () { if (!this.options.showfooter || this.options.cardview) { this.$tablefooter.hide(); } else { this.$tablefooter.show(); } }; /** * @param data * @param type: append / prepend */ bootstraptable.prototype.initdata = function (data, type) { if (type === 'append') { this.data = this.data.concat(data); } else if (type === 'prepend') { this.data = [].concat(data).concat(this.data); } else { this.data = data || this.options.data; } // fix #839 records deleted when adding new row on filtered table if (type === 'append') { this.options.data = this.options.data.concat(data); } else if (type === 'prepend') { this.options.data = [].concat(data).concat(this.options.data); } else { this.options.data = this.data; } if (this.options.sidepagination === 'server') { return; } this.initsort(); }; bootstraptable.prototype.initsort = function () { var that = this, name = this.options.sortname, order = this.options.sortorder === 'desc' ? -1 : 1, index = $.inarray(this.options.sortname, this.header.fields), timeoutid = 0; if (this.options.customsort !== $.noop) { this.options.customsort.apply(this, [this.options.sortname, this.options.sortorder]); return; } if (index !== -1) { if (this.options.sortstable) { $.each(this.data, function (i, row) { if (!row.hasownproperty('_position')) row._position = i; }); } this.data.sort(function (a, b) { if (that.header.sortnames[index]) { name = that.header.sortnames[index]; } var aa = getitemfield(a, name, that.options.escape), bb = getitemfield(b, name, that.options.escape), value = calculateobjectvalue(that.header, that.header.sorters[index], [aa, bb]); if (value !== undefined) { return order * value; } // fix #161: undefined or null string sort bug. if (aa === undefined || aa === null) { aa = ''; } if (bb === undefined || bb === null) { bb = ''; } if (that.options.sortstable && aa === bb) { aa = a._position; bb = b._position; } // if both values are numeric, do a numeric comparison if ($.isnumeric(aa) && $.isnumeric(bb)) { // convert numerical values form string to float. aa = parsefloat(aa); bb = parsefloat(bb); if (aa < bb) { return order * -1; } return order; } if (aa === bb) { return 0; } // if value is not a string, convert to string if (typeof aa !== 'string') { aa = aa.tostring(); } if (aa.localecompare(bb) === -1) { return order * -1; } return order; }); if (this.options.sortclass !== undefined) { cleartimeout(timeoutid); timeoutid = settimeout(function () { that.$el.removeclass(that.options.sortclass); var index = that.$header.find(sprintf('[data-field="%s"]', that.options.sortname).index() + 1); that.$el.find(sprintf('tr td:nth-child(%s)', index)) .addclass(that.options.sortclass); }, 250); } } }; bootstraptable.prototype.onsort = function (event) { var $this = event.type === "keypress" ? $(event.currenttarget) : $(event.currenttarget).parent(), $this_ = this.$header.find('th').eq($this.index()); this.$header.add(this.$header_).find('span.order').remove(); if (this.options.sortname === $this.data('field')) { this.options.sortorder = this.options.sortorder === 'asc' ? 'desc' : 'asc'; } else { this.options.sortname = $this.data('field'); this.options.sortorder = $this.data('order') === 'asc' ? 'desc' : 'asc'; } this.trigger('sort', this.options.sortname, this.options.sortorder); $this.add($this_).data('order', this.options.sortorder); // assign the correct sortable arrow this.getcaret(); if (this.options.sidepagination === 'server') { this.initserver(this.options.silentsort); return; } this.initsort(); this.initbody(); }; bootstraptable.prototype.inittoolbar = function () { var that = this, html = [], timeoutid = 0, $keepopen, $search, switchablecount = 0; if (this.$toolbar.find('.bs-bars').children().length) { $('body').append($(this.options.toolbar)); } this.$toolbar.html(''); if (typeof this.options.toolbar === 'string' || typeof this.options.toolbar === 'object') { $(sprintf('<div class="bs-bars pull-%s"></div>', this.options.toolbaralign)) .appendto(this.$toolbar) .append($(this.options.toolbar)); } // showcolumns, showtoggle, showrefresh html = [sprintf('<div class="columns columns-%s btn-group pull-%s">', this.options.buttonsalign, this.options.buttonsalign)]; if (typeof this.options.icons === 'string') { this.options.icons = calculateobjectvalue(null, this.options.icons); } if (this.options.showpaginationswitch) { html.push(sprintf('<button class="btn' + sprintf(' btn-%s', this.options.buttonsclass) + sprintf(' btn-%s', this.options.iconsize) + '" type="button" name="paginationswitch" aria-label="pagination switch" title="%s">', this.options.formatpaginationswitch()), sprintf('<i class="%s %s"></i>', this.options.iconsprefix, this.options.icons.paginationswitchdown), '</button>'); } if (this.options.showrefresh) { html.push(sprintf('<button class="btn' + sprintf(' btn-%s', this.options.buttonsclass) + sprintf(' btn-%s', this.options.iconsize) + '" type="button" name="refresh" aria-label="refresh" title="%s">', this.options.formatrefresh()), sprintf('<i class="%s %s"></i>', this.options.iconsprefix, this.options.icons.refresh), '</button>'); } if (this.options.showtoggle) { html.push(sprintf('<button class="btn' + sprintf(' btn-%s', this.options.buttonsclass) + sprintf(' btn-%s', this.options.iconsize) + '" type="button" name="toggle" aria-label="toggle" title="%s">', this.options.formattoggle()), sprintf('<i class="%s %s"></i>', this.options.iconsprefix, this.options.icons.toggle), '</button>'); } if (this.options.showcolumns) { html.push(sprintf('<div class="keep-open btn-group" title="%s">', this.options.formatcolumns()), '<button type="button" aria-label="columns" class="btn' + sprintf(' btn-%s', this.options.buttonsclass) + sprintf(' btn-%s', this.options.iconsize) + ' dropdown-toggle" data-toggle="dropdown">', sprintf('<i class="%s %s"></i>', this.options.iconsprefix, this.options.icons.columns), ' <span class="caret"></span>', '</button>', '<ul class="dropdown-menu" role="menu">'); $.each(this.columns, function (i, column) { if (column.radio || column.checkbox) { return; } if (that.options.cardview && !column.cardvisible) { return; } var checked = column.visible ? ' checked="checked"' : ''; if (column.switchable) { html.push(sprintf('<li role="menuitem">' + '<label><input type="checkbox" data-field="%s" value="%s"%s> %s</label>' + '</li>', column.field, i, checked, column.title)); switchablecount++; } }); html.push('</ul>', '</div>'); } html.push('</div>'); // fix #188: this.showtoolbar is for extensions if (this.showtoolbar || html.length > 2) { this.$toolbar.append(html.join('')); } if (this.options.showpaginationswitch) { this.$toolbar.find('button[name="paginationswitch"]') .off('click').on('click', $.proxy(this.togglepagination, this)); } if (this.options.showrefresh) { this.$toolbar.find('button[name="refresh"]') .off('click').on('click', $.proxy(this.refresh, this)); } if (this.options.showtoggle) { this.$toolbar.find('button[name="toggle"]') .off('click').on('click', function () { that.toggleview(); }); } if (this.options.showcolumns) { $keepopen = this.$toolbar.find('.keep-open'); if (switchablecount <= this.options.minimumcountcolumns) { $keepopen.find('input').prop('disabled', true); } $keepopen.find('li').off('click').on('click', function (event) { event.stopimmediatepropagation(); }); $keepopen.find('input').off('click').on('click', function () { var $this = $(this); that.togglecolumn($(this).val(), $this.prop('checked'), false); that.trigger('column-switch', $(this).data('field'), $this.prop('checked')); }); } if (this.options.search) { html = []; html.push( '<div class="pull-' + this.options.searchalign + ' search">', sprintf('<input class="form-control' + sprintf(' input-%s', this.options.iconsize) + '" type="text" placeholder="%s">', this.options.formatsearch()), '</div>'); this.$toolbar.append(html.join('')); $search = this.$toolbar.find('.search input'); $search.off('keyup drop blur').on('keyup drop blur', function (event) { if (that.options.searchonenterkey && event.keycode !== 13) { return; } if ($.inarray(event.keycode, [37, 38, 39, 40]) > -1) { return; } cleartimeout(timeoutid); // doesn't matter if it's 0 timeoutid = settimeout(function () { that.onsearch(event); }, that.options.searchtimeout); }); if (isiebrowser()) { $search.off('mouseup').on('mouseup', function (event) { cleartimeout(timeoutid); // doesn't matter if it's 0 timeoutid = settimeout(function () { that.onsearch(event); }, that.options.searchtimeout); }); } } }; bootstraptable.prototype.onsearch = function (event) { var text = $.trim($(event.currenttarget).val()); // trim search input if (this.options.trimonsearch && $(event.currenttarget).val() !== text) { $(event.currenttarget).val(text); } if (text === this.searchtext) { return; } this.searchtext = text; this.options.searchtext = text; this.options.pagenumber = 1; this.initsearch(); this.updatepagination(); this.trigger('search', text); }; bootstraptable.prototype.initsearch = function () { var that = this; if (this.options.sidepagination !== 'server') { if (this.options.customsearch !== $.noop) { this.options.customsearch.apply(this, [this.searchtext]); return; } var s = this.searchtext && (this.options.escape ? escapehtml(this.searchtext) : this.searchtext).tolowercase(); var f = $.isemptyobject(this.filtercolumns) ? null : this.filtercolumns; // check filter this.data = f ? $.grep(this.options.data, function (item, i) { for (var key in f) { if ($.isarray(f[key]) && $.inarray(item[key], f[key]) === -1 || !$.isarray(f[key]) && item[key] !== f[key]) { return false; } } return true; }) : this.options.data; this.data = s ? $.grep(this.data, function (item, i) { for (var j = 0; j < that.header.fields.length; j++) { if (!that.header.searchables[j]) { continue; } var key = $.isnumeric(that.header.fields[j]) ? parseint(that.header.fields[j], 10) : that.header.fields[j]; var column = that.columns[getfieldindex(that.columns, key)]; var value; if (typeof key === 'string') { value = item; var props = key.split('.'); for (var prop_index = 0; prop_index < props.length; prop_index++) { value = value[props[prop_index]]; } // fix #142: respect searchforamtter boolean if (column && column.searchformatter) { value = calculateobjectvalue(column, that.header.formatters[j], [value, item, i], value); } } else { value = item[key]; } if (typeof value === 'string' || typeof value === 'number') { if (that.options.strictsearch) { if ((value + '').tolowercase() === s) { return true; } } else { if ((value + '').tolowercase().indexof(s) !== -1) { return true; } } } } return false; }) : this.data; } }; bootstraptable.prototype.initpagination = function () { if (!this.options.pagination) { this.$pagination.hide(); return; } else { this.$pagination.show(); } var that = this, html = [], $allselected = false, i, from, to, $pagelist, $first, $pre, $next, $last, $number, data = this.getdata(), pagelist = this.options.pagelist; if (this.options.sidepagination !== 'server') { this.options.totalrows = data.length; } this.totalpages = 0; if (this.options.totalrows) { if (this.options.pagesize === this.options.formatallrows()) { this.options.pagesize = this.options.totalrows; $allselected = true; } else if (this.options.pagesize === this.options.totalrows) { // fix #667 table with pagination, // multiple pages and a search that matches to one page throws exception var pagelst = typeof this.options.pagelist === 'string' ? this.options.pagelist.replace('[', '').replace(']', '') .replace(/ /g, '').tolowercase().split(',') : this.options.pagelist; if ($.inarray(this.options.formatallrows().tolowercase(), pagelst) > -1) { $allselected = true; } } this.totalpages = ~~((this.options.totalrows - 1) / this.options.pagesize) + 1; this.options.totalpages = this.totalpages; } if (this.totalpages > 0 && this.options.pagenumber > this.totalpages) { this.options.pagenumber = this.totalpages; } this.pagefrom = (this.options.pagenumber - 1) * this.options.pagesize + 1; this.pageto = this.options.pagenumber * this.options.pagesize; if (this.pageto > this.options.totalrows) { this.pageto = this.options.totalrows; } html.push( '<div class="pull-' + this.options.paginationdetailhalign + ' pagination-detail">', '<span class="pagination-info">', this.options.onlyinfopagination ? this.options.formatdetailpagination(this.options.totalrows) : this.options.formatshowingrows(this.pagefrom, this.pageto, this.options.totalrows), '</span>'); if (!this.options.onlyinfopagination) { html.push('<span class="page-list">'); var pagenumber = [ sprintf('<span class="btn-group %s">', this.options.paginationvalign === 'top' || this.options.paginationvalign === 'both' ? 'dropdown' : 'dropup'), '<button type="button" class="btn' + sprintf(' btn-%s', this.options.buttonsclass) + sprintf(' btn-%s', this.options.iconsize) + ' dropdown-toggle" data-toggle="dropdown">', '<span class="page-size">', $allselected ? this.options.formatallrows() : this.options.pagesize, '</span>', ' <span class="caret"></span>', '</button>', '<ul class="dropdown-menu" role="menu">' ]; if (typeof this.options.pagelist === 'string') { var list = this.options.pagelist.replace('[', '').replace(']', '') .replace(/ /g, '').split(','); pagelist = []; $.each(list, function (i, value) { pagelist.push(value.touppercase() === that.options.formatallrows().touppercase() ? that.options.formatallrows() : +value); }); } $.each(pagelist, function (i, page) { if (!that.options.smartdisplay || i === 0 || pagelist[i - 1] < that.options.totalrows) { var active; if ($allselected) { active = page === that.options.formatallrows() ? ' class="active"' : ''; } else { active = page === that.options.pagesize ? ' class="active"' : ''; } pagenumber.push(sprintf('<li role="menuitem"%s><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >%s</a></li>', active, page)); } }); pagenumber.push('</ul></span>'); html.push(this.options.formatrecordsperpage(pagenumber.join(''))); html.push('</span>'); html.push('</div>', '<div class="pull-' + this.options.paginationhalign + ' pagination">', '<ul class="pagination' + sprintf(' pagination-%s', this.options.iconsize) + '">', '<li class="page-pre"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >' + this.options.paginationpretext + '</a></li>'); if (this.totalpages < 5) { from = 1; to = this.totalpages; } else { from = this.options.pagenumber - 2; to = from + 4; if (from < 1) { from = 1; to = 5; } if (to > this.totalpages) { to = this.totalpages; from = to - 4; } } if (this.totalpages >= 6) { if (this.options.pagenumber >= 3) { html.push('<li class="page-first' + (1 === this.options.pagenumber ? ' active' : '') + '">', '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >', 1, '</a>', '</li>'); from++; } if (this.options.pagenumber >= 4) { if (this.options.pagenumber == 4 || this.totalpages == 6 || this.totalpages == 7) { from--; } else { html.push('<li class="page-first-separator disabled">', '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a>', '</li>'); } to--; } } if (this.totalpages >= 7) { if (this.options.pagenumber >= (this.totalpages - 2)) { from--; } } if (this.totalpages == 6) { if (this.options.pagenumber >= (this.totalpages - 2)) { to++; } } else if (this.totalpages >= 7) { if (this.totalpages == 7 || this.options.pagenumber >= (this.totalpages - 3)) { to++; } } for (i = from; i <= to; i++) { html.push('<li class="page-number' + (i === this.options.pagenumber ? ' active' : '') + '">', '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >', i, '</a>', '</li>'); } if (this.totalpages >= 8) { if (this.options.pagenumber <= (this.totalpages - 4)) { html.push('<li class="page-last-separator disabled">', '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >...</a>', '</li>'); } } if (this.totalpages >= 6) { if (this.options.pagenumber <= (this.totalpages - 3)) { html.push('<li class="page-last' + (this.totalpages === this.options.pagenumber ? ' active' : '') + '">', '<a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >', this.totalpages, '</a>', '</li>'); } } html.push( '<li class="page-next"><a href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >' + this.options.paginationnexttext + '</a></li>', '</ul>', '</div>'); } this.$pagination.html(html.join('')); if (!this.options.onlyinfopagination) { $pagelist = this.$pagination.find('.page-list a'); $first = this.$pagination.find('.page-first'); $pre = this.$pagination.find('.page-pre'); $next = this.$pagination.find('.page-next'); $last = this.$pagination.find('.page-last'); $number = this.$pagination.find('.page-number'); if (this.options.smartdisplay) { if (this.totalpages <= 1) { this.$pagination.find('div.pagination').hide(); } if (pagelist.length < 2 || this.options.totalrows <= pagelist[0]) { this.$pagination.find('span.page-list').hide(); } // when data is empty, hide the pagination this.$pagination[this.getdata().length ? 'show' : 'hide'](); } if (!this.options.paginationloop) { if (this.options.pagenumber === 1) { $pre.addclass('disabled'); } if (this.options.pagenumber === this.totalpages) { $next.addclass('disabled'); } } if ($allselected) { this.options.pagesize = this.options.formatallrows(); } $pagelist.off('click').on('click', $.proxy(this.onpagelistchange, this)); $first.off('click').on('click', $.proxy(this.onpagefirst, this)); $pre.off('click').on('click', $.proxy(this.onpagepre, this)); $next.off('click').on('click', $.proxy(this.onpagenext, this)); $last.off('click').on('click', $.proxy(this.onpagelast, this)); $number.off('click').on('click', $.proxy(this.onpagenumber, this)); } }; bootstraptable.prototype.updatepagination = function (event) { // fix #171: ie disabled button can be clicked bug. if (event && $(event.currenttarget).hasclass('disabled')) { return; } if (!this.options.maintainselected) { this.resetrows(); } this.initpagination(); if (this.options.sidepagination === 'server') { this.initserver(); } else { this.initbody(); } this.trigger('page-change', this.options.pagenumber, this.options.pagesize); }; bootstraptable.prototype.onpagelistchange = function (event) { var $this = $(event.currenttarget); $this.parent().addclass('active').siblings().removeclass('active'); this.options.pagesize = $this.text().touppercase() === this.options.formatallrows().touppercase() ? this.options.formatallrows() : +$this.text(); this.$toolbar.find('.page-size').text(this.options.pagesize); this.updatepagination(event); return false; }; bootstraptable.prototype.onpagefirst = function (event) { this.options.pagenumber = 1; this.updatepagination(event); return false; }; bootstraptable.prototype.onpagepre = function (event) { if ((this.options.pagenumber - 1) === 0) { this.options.pagenumber = this.options.totalpages; } else { this.options.pagenumber--; } this.updatepagination(event); return false; }; bootstraptable.prototype.onpagenext = function (event) { if ((this.options.pagenumber + 1) > this.options.totalpages) { this.options.pagenumber = 1; } else { this.options.pagenumber++; } this.updatepagination(event); return false; }; bootstraptable.prototype.onpagelast = function (event) { this.options.pagenumber = this.totalpages; this.updatepagination(event); return false; }; bootstraptable.prototype.onpagenumber = function (event) { if (this.options.pagenumber === +$(event.currenttarget).text()) { return; } this.options.pagenumber = +$(event.currenttarget).text(); this.updatepagination(event); return false; }; bootstraptable.prototype.initrow = function (item, i, data, parentdom) { var that = this, key, html = [], style = {}, csses = [], data_ = '', attributes = {}, htmlattributes = []; if ($.inarray(item, this.hiddenrows) > -1) { return; } style = calculateobjectvalue(this.options, this.options.rowstyle, [item, i], style); if (style && style.css) { for (key in style.css) { csses.push(key + ': ' + style.css[key]); } } attributes = calculateobjectvalue(this.options, this.options.rowattributes, [item, i], attributes); if (attributes) { for (key in attributes) { htmlattributes.push(sprintf('%s="%s"', key, escapehtml(attributes[key]))); } } if (item._data && !$.isemptyobject(item._data)) { $.each(item._data, function (k, v) { // ignore data-index if (k === 'index') { return; } data_ += sprintf(' data-%s="%s"', k, v); }); } html.push('<tr', sprintf(' %s', htmlattributes.join(' ')), sprintf(' id="%s"', $.isarray(item) ? undefined : item._id), sprintf(' class="%s"', style.classes || ($.isarray(item) ? undefined : item._class)), sprintf(' data-index="%s"', i), sprintf(' data-uniqueid="%s"', item[this.options.uniqueid]), sprintf('%s', data_), '>' ); if (this.options.cardview) { html.push(sprintf('<td colspan="%s"><div class="card-views">', this.header.fields.length)); } if (!this.options.cardview && this.options.detailview) { html.push('<td>', '<a class="detail-icon" href="#" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" rel="external nofollow" >', sprintf('<i class="%s %s"></i>', this.options.iconsprefix, this.options.icons.detailopen), '</a>', '</td>'); } $.each(this.header.fields, function (j, field) { var text = '', value_ = getitemfield(item, field, that.options.escape), value = '', type = '', cellstyle = {}, id_ = '', class_ = that.header.classes[j], data_ = '', rowspan_ = '', colspan_ = '', title_ = '', column = that.columns[j]; if (that.fromhtml && typeof value_ === 'undefined') { return; } if (!column.visible) { return; } if (that.options.cardview && (!column.cardvisible)) { return; } if (column.escape) { value_ = escapehtml(value_); } style = sprintf('style="%s"', csses.concat(that.header.styles[j]).join('; ')); // handle td's id and class if (item['_' + field + '_id']) { id_ = sprintf(' id="%s"', item['_' + field + '_id']); } if (item['_' + field + '_class']) { class_ = sprintf(' class="%s"', item['_' + field + '_class']); } if (item['_' + field + '_rowspan']) { rowspan_ = sprintf(' rowspan="%s"', item['_' + field + '_rowspan']); } if (item['_' + field + '_colspan']) { colspan_ = sprintf(' colspan="%s"', item['_' + field + '_colspan']); } if (item['_' + field + '_title']) { title_ = sprintf(' title="%s"', item['_' + field + '_title']); } cellstyle = calculateobjectvalue(that.header, that.header.cellstyles[j], [value_, item, i, field], cellstyle); if (cellstyle.classes) { class_ = sprintf(' class="%s"', cellstyle.classes); } if (cellstyle.css) { var csses_ = []; for (var key in cellstyle.css) { csses_.push(key + ': ' + cellstyle.css[key]); } style = sprintf('style="%s"', csses_.concat(that.header.styles[j]).join('; ')); } value = calculateobjectvalue(column, that.header.formatters[j], [value_, item, i], value_); if (item['_' + field + '_data'] && !$.isemptyobject(item['_' + field + '_data'])) { $.each(item['_' + field + '_data'], function (k, v) { // ignore data-index if (k === 'index') { return; } data_ += sprintf(' data-%s="%s"', k, v); }); } if (column.checkbox || column.radio) { type = column.checkbox ? 'checkbox' : type; type = column.radio ? 'radio' : type; text = [sprintf(that.options.cardview ? '<div class="card-view %s">' : '<td class="bs-checkbox %s">', column['class'] || ''), '<input' + sprintf(' data-index="%s"', i) + sprintf(' name="%s"', that.options.selectitemname) + sprintf(' type="%s"', type) + sprintf(' value="%s"', item[that.options.idfield]) + sprintf(' checked="%s"', value === true || (value_ || value && value.checked) ? 'checked' : undefined) + sprintf(' disabled="%s"', !column.checkboxenabled || (value && value.disabled) ? 'disabled' : undefined) + ' />', that.header.formatters[j] && typeof value === 'string' ? value : '', that.options.cardview ? '</div>' : '</td>' ].join(''); item[that.header.statefield] = value === true || (value && value.checked); } else { value = typeof value === 'undefined' || value === null ? that.options.undefinedtext : value; text = that.options.cardview ? ['<div class="card-view">', that.options.showheader ? sprintf('<span class="title" %s>%s</span>', style, getpropertyfromother(that.columns, 'field', 'title', field)) : '', sprintf('<span class="value">%s</span>', value), '</div>' ].join('') : [sprintf('<td%s %s %s %s %s %s %s>', id_, class_, style, data_, rowspan_, colspan_, title_), value, '</td>' ].join(''); // hide empty data on card view when smartdisplay is set to true. if (that.options.cardview && that.options.smartdisplay && value === '') { // should set a placeholder for event binding correct fieldindex text = '<div class="card-view"></div>'; } } html.push(text); }); if (this.options.cardview) { html.push('</div></td>'); } html.push('</tr>'); return html.join(' '); }; bootstraptable.prototype.initbody = function (fixedscroll) { var that = this, html = [], data = this.getdata(); this.trigger('pre-body', data); this.$body = this.$el.find('>tbody'); if (!this.$body.length) { this.$body = $('<tbody></tbody>').appendto(this.$el); } //fix #389 bootstrap-table-flatjson is not working if (!this.options.pagination || this.options.sidepagination === 'server') { this.pagefrom = 1; this.pageto = data.length; } var trfragments = $(document.createdocumentfragment()); var hastr; for (var i = this.pagefrom - 1; i < this.pageto; i++) { var item = data[i]; var tr = this.initrow(item, i, data, trfragments); hastr = hastr || !!tr; if (tr && tr !== true) { trfragments.append(tr); } } // show no records if (!hastr) { trfragments.append('<tr class="no-records-found">' + sprintf('<td colspan="%s">%s</td>', this.$header.find('th').length, this.options.formatnomatches()) + '</tr>'); } this.$body.html(trfragments); if (!fixedscroll) { this.scrollto(0); } // click to select by column this.$body.find('> tr[data-index] > td').off('click dblclick').on('click dblclick', function (e) { var $td = $(this), $tr = $td.parent(), item = that.data[$tr.data('index')], index = $td[0].cellindex, fields = that.getvisiblefields(), field = fields[that.options.detailview && !that.options.cardview ? index - 1 : index], column = that.columns[getfieldindex(that.columns, field)], value = getitemfield(item, field, that.options.escape); if ($td.find('.detail-icon').length) { return; } that.trigger(e.type === 'click' ? 'click-cell' : 'dbl-click-cell', field, value, item, $td); that.trigger(e.type === 'click' ? 'click-row' : 'dbl-click-row', item, $tr, field); // if click to select - then trigger the checkbox/radio click if (e.type === 'click' && that.options.clicktoselect && column.clicktoselect) { var $selectitem = $tr.find(sprintf('[name="%s"]', that.options.selectitemname)); if ($selectitem.length) { $selectitem[0].click(); // #144: .trigger('click') bug } } }); this.$body.find('> tr[data-index] > td > .detail-icon').off('click').on('click', function () { var $this = $(this), $tr = $this.parent().parent(), index = $tr.data('index'), row = data[index]; // fix #980 detail view, when searching, returns wrong row // remove and update if ($tr.next().is('tr.detail-view')) { $this.find('i').attr('class', sprintf('%s %s', that.options.iconsprefix, that.options.icons.detailopen)); that.trigger('collapse-row', index, row); $tr.next().remove(); } else { $this.find('i').attr('class', sprintf('%s %s', that.options.iconsprefix, that.options.icons.detailclose)); $tr.after(sprintf('<tr class="detail-view"><td colspan="%s"></td></tr>', $tr.find('td').length)); var $element = $tr.next().find('td'); var content = calculateobjectvalue(that.options, that.options.detailformatter, [index, row, $element], ''); if ($element.length === 1) { $element.append(content); } that.trigger('expand-row', index, row, $element); } that.resetview(); return false; }); this.$selectitem = this.$body.find(sprintf('[name="%s"]', this.options.selectitemname)); this.$selectitem.off('click').on('click', function (event) { event.stopimmediatepropagation(); var $this = $(this), checked = $this.prop('checked'), row = that.data[$this.data('index')]; if (that.options.maintainselected && $(this).is(':radio')) { $.each(that.options.data, function (i, row) { row[that.header.statefield] = false; }); } row[that.header.statefield] = checked; if (that.options.singleselect) { that.$selectitem.not(this).each(function () { that.data[$(this).data('index')][that.header.statefield] = false; }); that.$selectitem.filter(':checked').not(this).prop('checked', false); } that.updateselected(); that.trigger(checked ? 'check' : 'uncheck', row, $this); }); $.each(this.header.events, function (i, events) { if (!events) { return; } // fix bug, if events is defined with namespace if (typeof events === 'string') { events = calculateobjectvalue(null, events); } var field = that.header.fields[i], fieldindex = $.inarray(field, that.getvisiblefields()); if (that.options.detailview && !that.options.cardview) { fieldindex += 1; } for (var key in events) { that.$body.find('>tr:not(.no-records-found)').each(function () { var $tr = $(this), $td = $tr.find(that.options.cardview ? '.card-view' : 'td').eq(fieldindex), index = key.indexof(' '), name = key.substring(0, index), el = key.substring(index + 1), func = events[key]; $td.find(el).off(name).on(name, function (e) { var index = $tr.data('index'), row = that.data[index], value = row[field]; func.apply(this, [e, value, row, index]); }); }); } }); this.updateselected(); this.resetview(); this.trigger('post-body', data); }; bootstraptable.prototype.initserver = function (silent, query, url) { var that = this, data = {}, params = { searchtext: this.searchtext, sortname: this.options.sortname, sortorder: this.options.sortorder }, request; if (this.options.pagination) { params.pagesize = this.options.pagesize === this.options.formatallrows() ? this.options.totalrows : this.options.pagesize; params.pagenumber = this.options.pagenumber; } if (!(url || this.options.url) && !this.options.ajax) { return; } if (this.options.queryparamstype === 'limit') { params = { search: params.searchtext, sort: params.sortname, order: params.sortorder }; if (this.options.pagination) { params.offset = this.options.pagesize === this.options.formatallrows() ? 0 : this.options.pagesize * (this.options.pagenumber - 1); params.limit = this.options.pagesize === this.options.formatallrows() ? this.options.totalrows : this.options.pagesize; } } if (!($.isemptyobject(this.filtercolumnspartial))) { params.filter = json.stringify(this.filtercolumnspartial, null); } data = calculateobjectvalue(this.options, this.options.queryparams, [params], data); $.extend(data, query || {}); // false to stop request if (data === false) { return; } if (!silent) { this.$tableloading.show(); } request = $.extend({}, calculateobjectvalue(null, this.options.ajaxoptions), { type: this.options.method, url: url || this.options.url, data: this.options.contenttype === 'application/json' && this.options.method === 'post' ? json.stringify(data) : data, cache: this.options.cache, contenttype: this.options.contenttype, datatype: this.options.datatype, success: function (res) { res = calculateobjectvalue(that.options, that.options.responsehandler, [res], res); that.load(res); that.trigger('load-success', res); if (!silent) that.$tableloading.hide(); }, error: function (res) { that.trigger('load-error', res.status, res); if (!silent) that.$tableloading.hide(); } }); if (this.options.ajax) { calculateobjectvalue(this, this.options.ajax, [request], null); } else { if (this._xhr && this._xhr.readystate !== 4) { this._xhr.abort(); } this._xhr = $.ajax(request); } }; bootstraptable.prototype.initsearchtext = function () { if (this.options.search) { if (this.options.searchtext !== '') { var $search = this.$toolbar.find('.search input'); $search.val(this.options.searchtext); this.onsearch({ currenttarget: $search }); } } }; bootstraptable.prototype.getcaret = function () { var that = this; $.each(this.$header.find('th'), function (i, th) { $(th).find('.sortable').removeclass('desc asc').addclass($(th).data('field') === that.options.sortname ? that.options.sortorder : 'both'); }); }; bootstraptable.prototype.updateselected = function () { var checkall = this.$selectitem.filter(':enabled').length && this.$selectitem.filter(':enabled').length === this.$selectitem.filter(':enabled').filter(':checked').length; this.$selectall.add(this.$selectall_).prop('checked', checkall); this.$selectitem.each(function () { $(this).closest('tr')[$(this).prop('checked') ? 'addclass' : 'removeclass']('selected'); }); }; bootstraptable.prototype.updaterows = function () { var that = this; this.$selectitem.each(function () { that.data[$(this).data('index')][that.header.statefield] = $(this).prop('checked'); }); }; bootstraptable.prototype.resetrows = function () { var that = this; $.each(this.data, function (i, row) { that.$selectall.prop('checked', false); that.$selectitem.prop('checked', false); if (that.header.statefield) { row[that.header.statefield] = false; } }); this.inithiddenrows(); }; bootstraptable.prototype.trigger = function (name) { var args = array.prototype.slice.call(arguments, 1); name += '.bs.table'; this.options[bootstraptable.events[name]].apply(this.options, args); this.$el.trigger($.event(name), args); this.options.onall(name, args); this.$el.trigger($.event('all.bs.table'), [name, args]); }; bootstraptable.prototype.resetheader = function () { // fix #61: the hidden table reset header bug. // fix bug: get $el.css('width') error sometime (height = 500) cleartimeout(this.timeoutid_); this.timeoutid_ = settimeout($.proxy(this.fitheader, this), this.$el.is(':hidden') ? 100 : 0); }; bootstraptable.prototype.fitheader = function () { var that = this, fixedbody, scrollwidth, focused, focusedtemp; if (that.$el.is(':hidden')) { that.timeoutid_ = settimeout($.proxy(that.fitheader, that), 100); return; } fixedbody = this.$tablebody.get(0); scrollwidth = fixedbody.scrollwidth > fixedbody.clientwidth && fixedbody.scrollheight > fixedbody.clientheight + this.$header.outerheight() ? getscrollbarwidth() : 0; this.$el.css('margin-top', -this.$header.outerheight()); focused = $(':focus'); if (focused.length > 0) { var $th = focused.parents('th'); if ($th.length > 0) { var datafield = $th.attr('data-field'); if (datafield !== undefined) { var $headerth = this.$header.find("[data-field='" + datafield + "']"); if ($headerth.length > 0) { $headerth.find(":input").addclass("focus-temp"); } } } } this.$header_ = this.$header.clone(true, true); this.$selectall_ = this.$header_.find('[name="btselectall"]'); this.$tableheader.css({ 'margin-right': scrollwidth }).find('table').css('width', this.$el.outerwidth()) .html('').attr('class', this.$el.attr('class')) .append(this.$header_); focusedtemp = $('.focus-temp:visible:eq(0)'); if (focusedtemp.length > 0) { focusedtemp.focus(); this.$header.find('.focus-temp').removeclass('focus-temp'); } // fix bug: $.data() is not working as expected after $.append() this.$header.find('th[data-field]').each(function (i) { that.$header_.find(sprintf('th[data-field="%s"]', $(this).data('field'))).data($(this).data()); }); var visiblefields = this.getvisiblefields(), $ths = this.$header_.find('th'); this.$body.find('>tr:first-child:not(.no-records-found) > *').each(function (i) { var $this = $(this), index = i; if (that.options.detailview && !that.options.cardview) { if (i === 0) { that.$header_.find('th.detail').find('.fht-cell').width($this.innerwidth()); } index = i - 1; } var $th = that.$header_.find(sprintf('th[data-field="%s"]', visiblefields[index])); if ($th.length > 1) { $th = $($ths[$this[0].cellindex]); } $th.find('.fht-cell').width($this.innerwidth()); }); // horizontal scroll event // todo: it's probably better improving the layout than binding to scroll event this.$tablebody.off('scroll').on('scroll', function () { that.$tableheader.scrollleft($(this).scrollleft()); if (that.options.showfooter && !that.options.cardview) { that.$tablefooter.scrollleft($(this).scrollleft()); } }); that.trigger('post-header'); }; bootstraptable.prototype.resetfooter = function () { var that = this, data = that.getdata(), html = []; if (!this.options.showfooter || this.options.cardview) { //do nothing return; } if (!this.options.cardview && this.options.detailview) { html.push('<td><div class="th-inner"> </div><div class="fht-cell"></div></td>'); } $.each(this.columns, function (i, column) { var key, falign = '', // footer align style valign = '', csses = [], style = {}, class_ = sprintf(' class="%s"', column['class']); if (!column.visible) { return; } if (that.options.cardview && (!column.cardvisible)) { return; } falign = sprintf('text-align: %s; ', column.falign ? column.falign : column.align); valign = sprintf('vertical-align: %s; ', column.valign); style = calculateobjectvalue(null, that.options.footerstyle); if (style && style.css) { for (key in style.css) { csses.push(key + ': ' + style.css[key]); } } html.push('<td', class_, sprintf(' style="%s"', falign + valign + csses.concat().join('; ')), '>'); html.push('<div class="th-inner">'); html.push(calculateobjectvalue(column, column.footerformatter, [data], ' ') || ' '); html.push('</div>'); html.push('<div class="fht-cell"></div>'); html.push('</div>'); html.push('</td>'); }); this.$tablefooter.find('tr').html(html.join('')); this.$tablefooter.show(); cleartimeout(this.timeoutfooter_); this.timeoutfooter_ = settimeout($.proxy(this.fitfooter, this), this.$el.is(':hidden') ? 100 : 0); }; bootstraptable.prototype.fitfooter = function () { var that = this, $footertd, elwidth, scrollwidth; cleartimeout(this.timeoutfooter_); if (this.$el.is(':hidden')) { this.timeoutfooter_ = settimeout($.proxy(this.fitfooter, this), 100); return; } elwidth = this.$el.css('width'); scrollwidth = elwidth > this.$tablebody.width() ? getscrollbarwidth() : 0; this.$tablefooter.css({ 'margin-right': scrollwidth }).find('table').css('width', elwidth) .attr('class', this.$el.attr('class')); $footertd = this.$tablefooter.find('td'); this.$body.find('>tr:first-child:not(.no-records-found) > *').each(function (i) { var $this = $(this); $footertd.eq(i).find('.fht-cell').width($this.innerwidth()); }); }; bootstraptable.prototype.togglecolumn = function (index, checked, needupdate) { if (index === -1) { return; } this.columns[index].visible = checked; this.initheader(); this.initsearch(); this.initpagination(); this.initbody(); if (this.options.showcolumns) { var $items = this.$toolbar.find('.keep-open input').prop('disabled', false); if (needupdate) { $items.filter(sprintf('[value="%s"]', index)).prop('checked', checked); } if ($items.filter(':checked').length <= this.options.minimumcountcolumns) { $items.filter(':checked').prop('disabled', true); } } }; bootstraptable.prototype.getvisiblefields = function () { var that = this, visiblefields = []; $.each(this.header.fields, function (j, field) { var column = that.columns[getfieldindex(that.columns, field)]; if (!column.visible) { return; } visiblefields.push(field); }); return visiblefields; }; // public function definition // ======================= bootstraptable.prototype.resetview = function (params) { var padding = 0; if (params && params.height) { this.options.height = params.height; } this.$selectall.prop('checked', this.$selectitem.length > 0 && this.$selectitem.length === this.$selectitem.filter(':checked').length); if (this.options.height) { var toolbarheight = this.$toolbar.outerheight(true), paginationheight = this.$pagination.outerheight(true), height = this.options.height; if (this.options.height.tostring().indexof("%") != -1) { height = $(window).height() * (parsefloat(this.options.height) / 100); } height = height - toolbarheight - paginationheight; this.$tablecontainer.css('height', height + 'px'); } if (this.options.cardview) { // remove the element css this.$el.css('margin-top', '0'); this.$tablecontainer.css('padding-bottom', '0'); this.$tablefooter.hide(); return; } if (this.options.showheader && this.options.height) { this.$tableheader.show(); this.resetheader(); padding += this.$header.outerheight(); } else { this.$tableheader.hide(); this.trigger('post-header'); } if (this.options.showfooter) { this.resetfooter(); if (this.options.height) { padding += this.$tablefooter.outerheight() + 1; } } // assign the correct sortable arrow this.getcaret(); this.$tablecontainer.css('padding-bottom', padding + 'px'); this.trigger('reset-view'); }; bootstraptable.prototype.getdata = function (usecurrentpage) { return (this.searchtext || !$.isemptyobject(this.filtercolumns) || !$.isemptyobject(this.filtercolumnspartial)) ? (usecurrentpage ? this.data.slice(this.pagefrom - 1, this.pageto) : this.data) : (usecurrentpage ? this.options.data.slice(this.pagefrom - 1, this.pageto) : this.options.data); }; bootstraptable.prototype.load = function (data) { var fixedscroll = false; // #431: support pagination if (this.options.sidepagination === 'server') { this.options.totalrows = data[this.options.totalfield]; fixedscroll = data.fixedscroll; data = data[this.options.datafield]; } else if (!$.isarray(data)) { // support fixedscroll fixedscroll = data.fixedscroll; data = data.data; } this.initdata(data); this.initsearch(); this.initpagination(); this.initbody(fixedscroll); }; bootstraptable.prototype.append = function (data) { this.initdata(data, 'append'); this.initsearch(); this.initpagination(); this.initsort(); this.initbody(true); }; bootstraptable.prototype.prepend = function (data) { this.initdata(data, 'prepend'); this.initsearch(); this.initpagination(); this.initsort(); this.initbody(true); }; bootstraptable.prototype.remove = function (params) { var len = this.options.data.length, i, row; if (!params.hasownproperty('field') || !params.hasownproperty('values')) { return; } for (i = len - 1; i >= 0; i--) { row = this.options.data[i]; if (!row.hasownproperty(params.field)) { continue; } if ($.inarray(row[params.field], params.values) !== -1) { this.options.data.splice(i, 1); if (this.options.sidepagination === 'server') { this.options.totalrows -= 1; } } } if (len === this.options.data.length) { return; } this.initsearch(); this.initpagination(); this.initsort(); this.initbody(true); }; bootstraptable.prototype.removeall = function () { if (this.options.data.length > 0) { this.options.data.splice(0, this.options.data.length); this.initsearch(); this.initpagination(); this.initbody(true); } }; bootstraptable.prototype.getrowbyuniqueid = function (id) { var uniqueid = this.options.uniqueid, len = this.options.data.length, datarow = null, i, row, rowuniqueid; for (i = len - 1; i >= 0; i--) { row = this.options.data[i]; if (row.hasownproperty(uniqueid)) { // uniqueid is a column rowuniqueid = row[uniqueid]; } else if (row._data.hasownproperty(uniqueid)) { // uniqueid is a row data property rowuniqueid = row._data[uniqueid]; } else { continue; } if (typeof rowuniqueid === 'string') { id = id.tostring(); } else if (typeof rowuniqueid === 'number') { if ((number(rowuniqueid) === rowuniqueid) && (rowuniqueid % 1 === 0)) { id = parseint(id); } else if ((rowuniqueid === number(rowuniqueid)) && (rowuniqueid !== 0)) { id = parsefloat(id); } } if (rowuniqueid === id) { datarow = row; break; } } return datarow; }; bootstraptable.prototype.removebyuniqueid = function (id) { var len = this.options.data.length, row = this.getrowbyuniqueid(id); if (row) { this.options.data.splice(this.options.data.indexof(row), 1); } if (len === this.options.data.length) { return; } this.initsearch(); this.initpagination(); this.initbody(true); }; bootstraptable.prototype.updatebyuniqueid = function (params) { var that = this; var allparams = $.isarray(params) ? params : [params]; $.each(allparams, function (i, params) { var rowid; if (!params.hasownproperty('id') || !params.hasownproperty('row')) { return; } rowid = $.inarray(that.getrowbyuniqueid(params.id), that.options.data); if (rowid === -1) { return; } $.extend(that.options.data[rowid], params.row); }); this.initsearch(); this.initpagination(); this.initsort(); this.initbody(true); }; bootstraptable.prototype.insertrow = function (params) { if (!params.hasownproperty('index') || !params.hasownproperty('row')) { return; } this.data.splice(params.index, 0, params.row); this.initsearch(); this.initpagination(); this.initsort(); this.initbody(true); }; bootstraptable.prototype.updaterow = function (params) { var that = this; var allparams = $.isarray(params) ? params : [params]; $.each(allparams, function (i, params) { if (!params.hasownproperty('index') || !params.hasownproperty('row')) { return; } $.extend(that.options.data[params.index], params.row); }); this.initsearch(); this.initpagination(); this.initsort(); this.initbody(true); }; bootstraptable.prototype.inithiddenrows = function () { this.hiddenrows = []; }; bootstraptable.prototype.showrow = function (params) { this.togglerow(params, true); }; bootstraptable.prototype.hiderow = function (params) { this.togglerow(params, false); }; bootstraptable.prototype.togglerow = function (params, visible) { var row, index; if (params.hasownproperty('index')) { row = this.getdata()[params.index]; } else if (params.hasownproperty('uniqueid')) { row = this.getrowbyuniqueid(params.uniqueid); } if (!row) { return; } index = $.inarray(row, this.hiddenrows); if (!visible && index === -1) { this.hiddenrows.push(row); } else if (visible && index > -1) { this.hiddenrows.splice(index, 1); } this.initbody(true); }; bootstraptable.prototype.gethiddenrows = function (show) { var that = this, data = this.getdata(), rows = []; $.each(data, function (i, row) { if ($.inarray(row, that.hiddenrows) > -1) { rows.push(row); } }); this.hiddenrows = rows; return rows; }; bootstraptable.prototype.mergecells = function (options) { var row = options.index, col = $.inarray(options.field, this.getvisiblefields()), rowspan = options.rowspan || 1, colspan = options.colspan || 1, i, j, $tr = this.$body.find('>tr'), $td; if (this.options.detailview && !this.options.cardview) { col += 1; } $td = $tr.eq(row).find('>td').eq(col); if (row < 0 || col < 0 || row >= this.data.length) { return; } for (i = row; i < row + rowspan; i++) { for (j = col; j < col + colspan; j++) { $tr.eq(i).find('>td').eq(j).hide(); } } $td.attr('rowspan', rowspan).attr('colspan', colspan).show(); }; bootstraptable.prototype.updatecell = function (params) { if (!params.hasownproperty('index') || !params.hasownproperty('field') || !params.hasownproperty('value')) { return; } this.data[params.index][params.field] = params.value; if (params.reinit === false) { return; } this.initsort(); this.initbody(true); }; bootstraptable.prototype.getoptions = function () { return this.options; }; bootstraptable.prototype.getselections = function () { var that = this; return $.grep(this.options.data, function (row) { // fix #2424: from html with checkbox return row[that.header.statefield] === true; }); }; bootstraptable.prototype.getallselections = function () { var that = this; return $.grep(this.options.data, function (row) { return row[that.header.statefield]; }); }; bootstraptable.prototype.checkall = function () { this.checkall_(true); }; bootstraptable.prototype.uncheckall = function () { this.checkall_(false); }; bootstraptable.prototype.checkinvert = function () { var that = this; var rows = that.$selectitem.filter(':enabled'); var checked = rows.filter(':checked'); rows.each(function () { $(this).prop('checked', !$(this).prop('checked')); }); that.updaterows(); that.updateselected(); that.trigger('uncheck-some', checked); checked = that.getselections(); that.trigger('check-some', checked); }; bootstraptable.prototype.checkall_ = function (checked) { var rows; if (!checked) { rows = this.getselections(); } this.$selectall.add(this.$selectall_).prop('checked', checked); this.$selectitem.filter(':enabled').prop('checked', checked); this.updaterows(); if (checked) { rows = this.getselections(); } this.trigger(checked ? 'check-all' : 'uncheck-all', rows); }; bootstraptable.prototype.check = function (index) { this.check_(true, index); }; bootstraptable.prototype.uncheck = function (index) { this.check_(false, index); }; bootstraptable.prototype.check_ = function (checked, index) { var $el = this.$selectitem.filter(sprintf('[data-index="%s"]', index)).prop('checked', checked); this.data[index][this.header.statefield] = checked; this.updateselected(); this.trigger(checked ? 'check' : 'uncheck', this.data[index], $el); }; bootstraptable.prototype.checkby = function (obj) { this.checkby_(true, obj); }; bootstraptable.prototype.uncheckby = function (obj) { this.checkby_(false, obj); }; bootstraptable.prototype.checkby_ = function (checked, obj) { if (!obj.hasownproperty('field') || !obj.hasownproperty('values')) { return; } var istrigger = true; /*是否触发选中事件,还是静默处理*/ if (obj.hasownproperty('istrigger') && !obj['istrigger']) { istrigger = false } var that = this, rows = []; $.each(this.options.data, function (index, row) { if (!row.hasownproperty(obj.field)) { return false; } if ($.inarray(row[obj.field], obj.values) !== -1) { var $el = that.$selectitem.filter(':enabled') .filter(sprintf('[data-index="%s"]', index)).prop('checked', checked); row[that.header.statefield] = checked; rows.push(row); if (istrigger) { that.trigger(checked ? 'check' : 'uncheck', row, $el); } } }); this.updateselected(); if (istrigger) { this.trigger(checked ? 'check-some' : 'uncheck-some', rows); } }; bootstraptable.prototype.destroy = function () { this.$el.insertbefore(this.$container); $(this.options.toolbar).insertbefore(this.$el); this.$container.next().remove(); this.$container.remove(); this.$el.html(this.$el_.html()) .css('margin-top', '0') .attr('class', this.$el_.attr('class') || ''); // reset the class }; bootstraptable.prototype.showloading = function () { this.$tableloading.show(); }; bootstraptable.prototype.hideloading = function () { this.$tableloading.hide(); }; bootstraptable.prototype.togglepagination = function () { this.options.pagination = !this.options.pagination; var button = this.$toolbar.find('button[name="paginationswitch"] i'); if (this.options.pagination) { button.attr("class", this.options.iconsprefix + " " + this.options.icons.paginationswitchdown); } else { button.attr("class", this.options.iconsprefix + " " + this.options.icons.paginationswitchup); } this.updatepagination(); }; bootstraptable.prototype.refresh = function (params) { if (params && params.url) { this.options.url = params.url; } if (params && params.pagenumber) { this.options.pagenumber = params.pagenumber; } if (params && params.pagesize) { this.options.pagesize = params.pagesize; } this.initserver(params && params.silent, params && params.query, params && params.url); this.trigger('refresh', params); }; bootstraptable.prototype.resetwidth = function () { if (this.options.showheader && this.options.height) { this.fitheader(); } if (this.options.showfooter) { this.fitfooter(); } }; bootstraptable.prototype.showcolumn = function (field) { this.togglecolumn(getfieldindex(this.columns, field), true, true); }; bootstraptable.prototype.hidecolumn = function (field) { this.togglecolumn(getfieldindex(this.columns, field), false, true); }; bootstraptable.prototype.gethiddencolumns = function () { return $.grep(this.columns, function (column) { return !column.visible; }); }; bootstraptable.prototype.getvisiblecolumns = function () { return $.grep(this.columns, function (column) { return column.visible; }); }; bootstraptable.prototype.toggleallcolumns = function (visible) { $.each(this.columns, function (i, column) { this.columns[i].visible = visible; }); this.initheader(); this.initsearch(); this.initpagination(); this.initbody(); if (this.options.showcolumns) { var $items = this.$toolbar.find('.keep-open input').prop('disabled', false); if ($items.filter(':checked').length <= this.options.minimumcountcolumns) { $items.filter(':checked').prop('disabled', true); } } }; bootstraptable.prototype.showallcolumns = function () { this.toggleallcolumns(true); }; bootstraptable.prototype.hideallcolumns = function () { this.toggleallcolumns(false); }; bootstraptable.prototype.filterby = function (columns) { this.filtercolumns = $.isemptyobject(columns) ? {} : columns; this.options.pagenumber = 1; this.initsearch(); this.updatepagination(); }; bootstraptable.prototype.scrollto = function (value) { if (typeof value === 'string') { value = value === 'bottom' ? this.$tablebody[0].scrollheight : 0; } if (typeof value === 'number') { this.$tablebody.scrolltop(value); } if (typeof value === 'undefined') { return this.$tablebody.scrolltop(); } }; bootstraptable.prototype.getscrollposition = function () { return this.scrollto(); }; bootstraptable.prototype.selectpage = function (page) { if (page > 0 && page <= this.options.totalpages) { this.options.pagenumber = page; this.updatepagination(); } }; bootstraptable.prototype.prevpage = function () { if (this.options.pagenumber > 1) { this.options.pagenumber--; this.updatepagination(); } }; bootstraptable.prototype.nextpage = function () { if (this.options.pagenumber < this.options.totalpages) { this.options.pagenumber++; this.updatepagination(); } }; bootstraptable.prototype.toggleview = function () { this.options.cardview = !this.options.cardview; this.initheader(); // fixed remove toolbar when click cardview button. //that.inittoolbar(); this.initbody(); this.trigger('toggle', this.options.cardview); }; bootstraptable.prototype.refreshoptions = function (options) { //if the objects are equivalent then avoid the call of destroy / init methods if (compareobjects(this.options, options, true)) { return; } this.options = $.extend(this.options, options); this.trigger('refresh-options', this.options); this.destroy(); this.init(); }; bootstraptable.prototype.resetsearch = function (text) { var $search = this.$toolbar.find('.search input'); $search.val(text || ''); this.onsearch({ currenttarget: $search }); }; bootstraptable.prototype.expandrow_ = function (expand, index) { var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', index)); if ($tr.next().is('tr.detail-view') === (expand ? false : true)) { $tr.find('> td > .detail-icon').click(); } }; bootstraptable.prototype.expandrow = function (index) { this.expandrow_(true, index); }; bootstraptable.prototype.collapserow = function (index) { this.expandrow_(false, index); }; bootstraptable.prototype.expandallrows = function (issubtable) { if (issubtable) { var $tr = this.$body.find(sprintf('> tr[data-index="%s"]', 0)), that = this, detailicon = null, executeinterval = false, idinterval = -1; if (!$tr.next().is('tr.detail-view')) { $tr.find('> td > .detail-icon').click(); executeinterval = true; } else if (!$tr.next().next().is('tr.detail-view')) { $tr.next().find(".detail-icon").click(); executeinterval = true; } if (executeinterval) { try { idinterval = setinterval(function () { detailicon = that.$body.find("tr.detail-view").last().find(".detail-icon"); if (detailicon.length > 0) { detailicon.click(); } else { clearinterval(idinterval); } }, 1); } catch (ex) { clearinterval(idinterval); } } } else { var trs = this.$body.children(); for (var i = 0; i < trs.length; i++) { this.expandrow_(true, $(trs[i]).data("index")); } } }; bootstraptable.prototype.collapseallrows = function (issubtable) { if (issubtable) { this.expandrow_(false, 0); } else { var trs = this.$body.children(); for (var i = 0; i < trs.length; i++) { this.expandrow_(false, $(trs[i]).data("index")); } } }; bootstraptable.prototype.updateformattext = function (name, text) { if (this.options[sprintf('format%s', name)]) { if (typeof text === 'string') { this.options[sprintf('format%s', name)] = function () { return text; }; } else if (typeof text === 'function') { this.options[sprintf('format%s', name)] = text; } } this.inittoolbar(); this.initpagination(); this.initbody(); }; // bootstrap table plugin definition // ======================= var allowedmethods = [ 'getoptions', 'getselections', 'getallselections', 'getdata', 'load', 'append', 'prepend', 'remove', 'removeall', 'insertrow', 'updaterow', 'updatecell', 'updatebyuniqueid', 'removebyuniqueid', 'getrowbyuniqueid', 'showrow', 'hiderow', 'gethiddenrows', 'mergecells', 'checkall', 'uncheckall', 'checkinvert', 'check', 'uncheck', 'checkby', 'uncheckby', 'refresh', 'resetview', 'resetwidth', 'destroy', 'showloading', 'hideloading', 'showcolumn', 'hidecolumn', 'gethiddencolumns', 'getvisiblecolumns', 'showallcolumns', 'hideallcolumns', 'filterby', 'scrollto', 'getscrollposition', 'selectpage', 'prevpage', 'nextpage', 'togglepagination', 'toggleview', 'refreshoptions', 'resetsearch', 'expandrow', 'collapserow', 'expandallrows', 'collapseallrows', 'updateformattext' ]; $.fn.bootstraptable = function (option) { var value, args = array.prototype.slice.call(arguments, 1); this.each(function () { var $this = $(this), data = $this.data('bootstrap.table'), options = $.extend({}, bootstraptable.defaults, $this.data(), typeof option === 'object' && option); if (typeof option === 'string') { if ($.inarray(option, allowedmethods) < 0) { throw new error("unknown method: " + option); } if (!data) { return; } value = data[option].apply(data, args); if (option === 'destroy') { $this.removedata('bootstrap.table'); } } if (!data) { $this.data('bootstrap.table', (data = new bootstraptable(this, options))); } }); return typeof value === 'undefined' ? this : value; }; $.fn.bootstraptable.constructor = bootstraptable; $.fn.bootstraptable.defaults = bootstraptable.defaults; $.fn.bootstraptable.columndefaults = bootstraptable.column_defaults; $.fn.bootstraptable.locales = bootstraptable.locales; $.fn.bootstraptable.methods = allowedmethods; $.fn.bootstraptable.utils = { sprintf: sprintf, getfieldindex: getfieldindex, compareobjects: compareobjects, calculateobjectvalue: calculateobjectvalue, getitemfield: getitemfield, objectkeys: objectkeys, isiebrowser: isiebrowser }; // bootstrap table init // ======================= $(function () { $('[data-toggle="table"]').bootstraptable(); }); })(jquery);
总结
以上所述是小编给大家介绍的bootstrap table支持高度百分比的实例代码,希望对大家有所帮助
推荐阅读
-
bootstrap table支持高度百分比的实例代码
-
修改 bootstrap table 默认detailRow样式的实例代码
-
基于Bootstrap table组件实现多层表头的实例代码
-
基于Metronic的Bootstrap开发框架经验总结(18)-- 在代码生成工具Database2Sharp中集成对Bootstrap-table插件的分页及排序支持
-
让bootstrap-table支持高度百分比
-
修改 bootstrap table 默认detailRow样式的实例代码
-
Angularjs+bootstrap+table多选(全选)支持单击行选中实现编辑、删除功能的代码案例
-
让bootstrap-table支持高度百分比
-
基于Bootstrap table组件实现多层表头的实例代码
-
bootstrap table支持高度百分比的实例代码