| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544 |
- /**
- * @author zhixin wen <wenzhixin2010@gmail.com>
- * version: 1.0.1
- */
- !function ($) {
- 'use strict';
- // TOOLS DEFINITION
- // ======================
- // 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;
- });
- if (flag) {
- return str;
- }
- return '';
- };
- // BOOTSTRAP TABLE CLASS DEFINITION
- // ======================
- var BootstrapTable = function(el, options) {
- this.options = options;
- this.$el = $(el);
- this.init();
- };
- BootstrapTable.DEFAULTS = {
- classes: 'table table-hover',
- height: undefined,
- undefinedText: '-',
- sortName: undefined,
- sortOrder: 'asc',
- striped: false,
- columns: [],
- data: [],
- method: 'get',
- url: undefined,
- queryParams: {},
- pagination: false,
- sidePagination: 'client', // client or server
- totalRows: 0, // server side need to set
- pageNumber: 1,
- pageSize: 10,
- pageList: [10, 20, 30, 40, 50],
- onClickRow: function(item) {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;}
- };
- BootstrapTable.prototype.init = function() {
- this.initContainer();
- this.initHeader();
- this.initData();
- this.initPagination();
- this.initBody();
- this.initServer();
- };
- BootstrapTable.prototype.initContainer = function() {
- this.$container = $([
- '<div class="fixed-table-container">',
- '<div class="fixed-table-header"></div>',
- '<div class="fixed-table-body"></div>',
- '<div class="fixed-table-pagination"></div>',
- '</div>'].join(''));
- this.$container.insertAfter(this.$el);
- this.$container.find('.fixed-table-body').append(this.$el);
- this.$container.after('<div class="clearfix"></div>');
- if (this.options.height) {
- this.$container.css('height', this.options.height + 'px');
- }
- this.$el.addClass(this.options.classes);
- if (this.options.striped) {
- this.$el.addClass('table-striped');
- }
- };
- BootstrapTable.prototype.initHeader = function() {
- var that = this,
- columns = [],
- html = [];
- this.$header = this.$el.find('thead');
- if (!this.$header.length) {
- this.$header = $('<thead></thead>').appendTo(this.$el);
- }
- if (!this.$header.find('tr').length) {
- this.$header.append('<tr></tr>');
- }
- this.$header.find('th').each(function() {
- var column = $.extend({}, {
- title: $(this).text()
- }, $(this).data());
- columns.push(column);
- });
- this.options.columns = $.extend(columns, this.options.columns);
- this.header = {
- fields: [],
- styles: [],
- formatters: [],
- sorters: []
- };
- $.each(this.options.columns, function(i, column) {
- var text = '',
- style = sprintf('text-align: %s; ', column.align) + sprintf('vertical-align: %s; ', column.valign),
- order = that.options.sortOrder || column.order || 'asc';
- that.header.fields.push(column.field);
- that.header.styles.push(style);
- that.header.formatters.push(column.formatter);
- that.header.sorters.push(column.sorter);
- style = sprintf('width: %spx; ', column.checkbox || column.radio ? 36 : column.width);
- style += column.sortable ? 'cursor: pointer; ' : '';
- html.push('<th' + sprintf(' style="%s"', style) + '>');
- html.push('<div class="th-inner">');
- text = column.title;
- if (that.options.sortName === column.field && column.sortable) {
- text += that.getCaretHtml();
- }
- if (column.checkbox) {
- text = '<input name="btSelectAll" type="checkbox" class="checkbox" />';
- that.header.stateField = column.field;
- }
- if (column.radio) {
- text = '';
- that.header.stateField = column.field;
- }
- html.push(text);
- html.push('</div>');
- html.push('</th>');
- });
- this.$header.find('tr').html(html.join(''));
- this.$header.find('th').each(function(i) {
- $(this).data(columns[i]);
- if (columns[i].sortable) {
- $(this).click($.proxy(that.onSort, that));
- }
- });
- this.$selectAll = this.$header.find('[name="btSelectAll"]');
- this.$selectAll.off('click').on('click', function() {
- var checked = $(this).prop('checked');
- that[checked ? 'checkAll' : 'uncheckAll']();
- });
- };
- BootstrapTable.prototype.initData = function(data, append) {
- if (append) {
- this.data = this.data.concat(data);
- } else {
- this.data = data || this.options.data;
- }
- this.initSort();
- };
- BootstrapTable.prototype.initSort = function() {
- var name = this.options.sortName,
- order = this.options.sortOrder === 'desc' ? -1 : 1,
- index = $.inArray(this.options.sortName, this.header.fields);
- if (index !== -1) {
- var sorter = this.header.sorters[index];
- this.data.sort(function(a, b) {
- if (typeof sorter === 'function') {
- return order * sorter(a[name], b[name]);
- }
- if (a[name] === b[name]) {
- return 0;
- }
- if (a[name] < b[name]) {
- return order * -1;
- }
- return order;
- });
- }
- };
- BootstrapTable.prototype.onSort = function(event) {
- var $this = $(event.currentTarget);
- this.$header.find('span.order').remove();
- this.options.sortName = $this.data('field');
- this.options.sortOrder = $this.data('order') === 'asc' ? 'desc' : 'asc';
- this.options.onSort(this.options.sortName, this.options.sortOrder);
- $this.data('order', this.options.sortOrder);
- $this.find('.th-inner').append(this.getCaretHtml());
- this.initSort();
- this.initBody();
- };
- BootstrapTable.prototype.initPagination = function() {
- if (!this.options.pagination) {
- return;
- }
- this.$pagination = this.$container.find('.fixed-table-pagination');
- if (this.options.sidePagination === 'client') {
- this.options.totalRows = this.data.length;
- }
- this.totalPages = 0;
- if (this.options.totalRows) {
- this.totalPages = ~~((this.options.totalRows - 1) / this.options.pageSize) + 1;
- }
- this.updatePagination();
- };
- BootstrapTable.prototype.updatePagination = function() {
- var html = [],
- i, from, to,
- $first, $pre,
- $next, $last,
- $number;
- 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-left pagination">',
- '<div class="pagination-info">',
- sprintf('Showing %s to %s of %s rows', this.pageFrom, this.pageTo, this.options.totalRows),
- '</div>',
- '</div>',
- '<div class="pull-right">',
- '<ul class="pagination">',
- '<li class="page-first"><a href="javascript:void(0)"><<</a></li>',
- '<li class="page-pre"><a href="javascript:void(0)"><</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;
- }
- }
- for (i = from; i <= to; i++) {
- html.push('<li class="page-number' + (i === this.options.pageNumber ? ' active' : '') + '">',
- '<a href="javascript:void(0)">', i ,'</a>',
- '</li>');
- }
- html.push(
- '<li class="page-next"><a href="javascript:void(0)">></a></li>',
- '<li class="page-last"><a href="javascript:void(0)">>></a></li>',
- '</ul>',
- '</div>')
- this.$pagination.html(html.join(''));
- $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.pageNumber <= 1) {
- $first.addClass('disabled');
- $pre.addClass('disabled');
- }
- if (this.options.pageNumber >= this.totalPages) {
- $next.addClass('disabled');
- $last.addClass('disabled');
- }
- $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.onPageFirst = function() {
- this.options.pageNumber = 1;
- this.updatePagination();
- this.initBody();
- };
- BootstrapTable.prototype.onPagePre = function() {
- this.options.pageNumber--;
- this.updatePagination();
- this.initBody();
- };
- BootstrapTable.prototype.onPageNext = function() {
- this.options.pageNumber++;
- this.updatePagination();
- this.initBody();
- };
- BootstrapTable.prototype.onPageLast = function() {
- this.options.pageNumber = this.totalPages;
- this.updatePagination();
- this.initBody();
- };
- BootstrapTable.prototype.onPageNumber = function(event) {
- this.options.pageNumber = +$(event.currentTarget).text();
- this.updatePagination();
- this.initBody();
- };
- BootstrapTable.prototype.initBody = function() {
- var that = this,
- html = [];
- this.$body = this.$el.find('tbody');
- if (!this.$body.length) {
- this.$body = $('<tbody></tbody>').appendTo(this.$el);
- }
- if (!this.options.pagination) {
- this.pageFrom = 1;
- this.pageTo = this.data.length;
- }
- for (var i = this.pageFrom - 1; i < this.pageTo; i++) {
- var item = this.data[i];
- html.push('<tr' + ' data-index="' + i + '">');
- $.each(that.header.fields, function(j, field) {
- var text = '',
- value = item[field],
- type = '';
- if (typeof that.header.formatters[j] === 'function') {
- value = that.header.formatters[j](value, item);
- }
- text = ['<td' + sprintf(' style="%s"', that.header.styles[j]) + '>',
- typeof value === 'undefined' ? that.options.undefinedText : value,
- '</td>'].join('');
- if (that.options.columns[j].checkbox || that.options.columns[j].radio) {
- type = that.options.columns[j].checkbox ? 'checkbox' : type;
- type = that.options.columns[j].radio ? 'radio' : type;
- text = ['<td>',
- '<input name="btSelectItem" class="checkbox" data-index="' + i + '"' +
- sprintf(' type="%s"', type) +
- sprintf(' checked="%s"', value ? 'checked' : undefined) + ' />',
- '</td>'].join('');
- }
- html.push(text);
- });
- html.push('</tr>');
- }
- this.$body.html(html.join(''));
- this.$body.find('tr').off('click').on('click', function() {
- that.options.onClickRow(that.data[$(this).data('index')]);
- });
- this.$selectItem = this.$body.find('[name="btSelectItem"]');
- this.$selectItem.off('click').on('click', function() {
- var checkAll = that.data.length === that.$selectItem.filter(':checked').length;
- that.$selectAll.prop('checked', checkAll);
- that.data[$(this).data('index')][that.header.stateField] = $(this).prop('checked');
- });
- this.resetView();
- };
- BootstrapTable.prototype.initServer = function() {
- var that = this;
- if (!this.options.url) {
- return;
- }
- $.ajax({
- type: this.options.method,
- url: this.options.url,
- data: this.options.queryParams,
- contentType: 'application/json',
- dataType: 'json',
- success: function(data) {
- that.load(data);
- }
- });
- };
- BootstrapTable.prototype.getCaretHtml = function() {
- return ['<span class="order' + (this.options.sortOrder === 'desc' ? '' : ' dropup') + '">',
- '<span class="caret" style="margin: 10px 5px;"></span>',
- '</span>'].join('');
- };
- BootstrapTable.prototype.resetView = function() {
- var header = this.header;
- this.$header.find('.th-inner').each(function(i) {
- $(this).attr('style', header.styles[i])
- .css('width', ($(this).parent().width()) + 'px'); // padding: 8px
- });
- };
- BootstrapTable.prototype.updateRows = function(checked) {
- var that = this;
- $.each(this.data, function(i, row) {
- row[that.header.stateField] = checked;
- });
- };
- // PUBLIC FUNCTION DEFINITION
- // =======================
- BootstrapTable.prototype.load = function(data) {
- this.initData(data);
- this.initPagination();
- this.initBody();
- };
- BootstrapTable.prototype.append = function(data) {
- this.initData(data, true);
- this.initBody();
- };
- BootstrapTable.prototype.mergeCells = function(options) {
- var row = options.index,
- col = $.inArray(options.field, this.header.fields),
- rowspan = options.rowspan || 1,
- colspan = options.colspan || 1,
- i, j,
- $tr = this.$body.find('tr'),
- $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.getSelections = function() {
- var that = this;
- return $.grep(this.data, function(row) {
- return row[that.header.stateField];
- });
- };
- BootstrapTable.prototype.checkAll = function() {
- this.$selectAll.prop('checked', true);
- this.$selectItem.prop('checked', true);
- this.updateRows(true);
- };
- BootstrapTable.prototype.uncheckAll = function() {
- this.$selectAll.prop('checked', false);
- this.$selectItem.prop('checked', false);
- this.updateRows(false);
- };
- // BOOTSTRAP TABLE PLUGIN DEFINITION
- // =======================
- $.fn.bootstrapTable = function(option, _relatedTarget) {
- var allowedMethods = [
- 'getSelections',
- 'load', 'append', 'mergeCells',
- 'checkAll', 'uncheckAll'
- ],
- value;
- this.each(function() {
- var $this = $(this),
- data = $this.data('bootstrap.table'),
- options = $.extend({}, BootstrapTable.DEFAULTS, $this.data(), typeof option === 'object' && option);
- if (!data) {
- $this.data('bootstrap.table', (data = new BootstrapTable(this, options)));
- }
- if (typeof option === 'string') {
- if ($.inArray(option, allowedMethods) < 0) {
- throw "Unknown method: " + option;
- }
- value = data[option](_relatedTarget);
- }
- });
- return value ? value : this;
- };
- $.fn.bootstrapTable.Constructor = BootstrapTable;
- // BOOTSTRAP TABLE INIT
- // =======================
- $(function() {
- $('[data-toggle="table"]').bootstrapTable();
- });
- }(jQuery);
|