Browse Source

Update version to 1.0.1

zhixin 11 years ago
parent
commit
8dbef5eeab
5 changed files with 4179 additions and 64 deletions
  1. 11 42
      README.md
  2. 4000 3
      demos/data.json
  3. 2 2
      demos/index.html
  4. 9 0
      src/bootstrap-table.css
  5. 157 17
      src/bootstrap-table.js

+ 11 - 42
README.md

@@ -1,46 +1,15 @@
-## bootstrap table
+# bootstrap table
 
-Simple table for bootstrap. [Examples and documentation](http://wenzhixin.net.cn/p/bootstrap-table/)
+The table displays data in a tabular format and offers rich support to select, sort. The table has been designed to reduce development time and to require no specific knowledge from developers. It is both featherweight and feature-rich.
 
-### How to use:
+[Examples and documentation](http://wenzhixin.net.cn/p/bootstrap-table/)
 
-	<table id="table"></table>
+## Changelog
 
-	$('#table').bootstrapTable({
-		sortName: 'code',
-		sortOrder: 'desc',
-		columns: [
-			{field: 'code', title: 'Code', align: 'left', width: 200, sortable: true, sorter: function(a, b) {
-				return a < b;
-			}},
-			{field: 'name', title: 'Name', align: 'center', width: 100},
-			{field: 'price', title: 'Price', align: 'right', valign: 'middle', width: 200, formatter: function(value, row) {
-				return 'the price is: ' + value;
-			}}
-		],
-		data: [
-			{code: '123', name: 'hello 123', price: '$123'},
-			{code: '222', name: 'hello 222', price: '$222'},
-			{code: '321', name: 'hello 321', price: '$321'}
-		]
-	});
-	
-	$('#table').bootstrapTable('load', [
-		{code: '123', name: 'hello 123', price: '$123'},
-		{code: '222', name: 'hello 222', price: '$222'},
-		{code: '321', name: 'hello 321', price: '$321'}
-	]);
-	
-	$('#table').bootstrapTable('append', [
-		{code: '123', name: 'hello 123', price: '$123'},
-		{code: '222', name: 'hello 222', price: '$222'},
-		{code: '321', name: 'hello 321', price: '$321'}
-	]);
-	
-	$('#table').bootstrapTable('mergeCells', {
-		index: 1,
-		field: 'code',
-		rowspan: 2,
-		colspan: 2
-	});
-	
+### 1.0.1
+
+Add ```pagination``` support.
+
+### 1.0.0
+
+Initial release

File diff suppressed because it is too large
+ 4000 - 3
demos/data.json


+ 2 - 2
demos/index.html

@@ -7,7 +7,7 @@
 </head>
 <body>
 <div class="container">
-    <table class="bootstrap-table" data-url="data.json">
+    <table class="bootstrap-table" data-url="data.json" data-height="500">
         <thead>
             <tr>
                 <th data-field="state" data-checkbox="true"></th>
@@ -28,7 +28,7 @@
 <script type="text/javascript">
     $(function() {
         $('button').click(function() {
-            alert($('.bootstrap-table').bootstrapTable('getSelections'));
+            console.log($('.bootstrap-table').bootstrapTable('getSelections'));
         });
     });
 </script>

+ 9 - 0
src/bootstrap-table.css

@@ -74,6 +74,15 @@
     margin-right: auto;
 }
 
+.fixed-table-pagination .pagination-info {
+    line-height: 34px;
+}
+
+.fixed-table-pagination li.disabled a {
+    pointer-events: none;
+    cursor: default;
+}
+
 /* support bootstrap 3 */
 .table thead>tr>th {
 	padding: 0;

+ 157 - 17
src/bootstrap-table.js

@@ -1,6 +1,6 @@
 /**
  * @author zhixin wen <wenzhixin2010@gmail.com>
- * version: 1.0.0
+ * version: 1.0.1
  */
 
 !function ($) {
@@ -42,13 +42,21 @@
     };
 
     BootstrapTable.DEFAULTS = {
-        classes: 'table table-hover table-striped',
+        classes: 'table table-hover',
+        striped: true,
         undefinedText: '-',
+        height: undefined,
         columns: [],
         data: [],
         method: 'get',
         url: '',
-        onClickRow: function(value, row) {return false;},
+        queryParams: {},
+        pagination: true,
+        sidePagination: 'client', // client or server
+        totalRows: 0, // server side need to set
+        pageNumber: 1,
+        pageSize: 20,
+        pageList: [10, 20, 30, 40, 50],
         onSort: function(name, order) {return false;}
     };
 
@@ -56,6 +64,7 @@
         this.initContainer();
         this.initHeader();
         this.initData();
+        this.initPagination();
         this.initBody();
         this.initServer();
     };
@@ -63,15 +72,21 @@
     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-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() {
@@ -198,6 +213,125 @@
         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)">&lt;&lt;</a></li>',
+                    '<li class="page-pre"><a href="javascript:void(0)">&lt;</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)">&gt;</a></li>',
+                    '<li class="page-last"><a href="javascript:void(0)">&gt;&gt;</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 = [];
@@ -207,7 +341,14 @@
             this.$body = $('<tbody></tbody>').appendTo(this.$el);
         }
 
-        $.each(this.data, function(i, item) {
+        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>');
 
             $.each(that.header.fields, function(j, field) {
@@ -220,7 +361,7 @@
                 }
 
                 text = ['<td' + sprintf(' style="%s"', that.header.styles[j]) + '>',
-                    value || that.options.undefinedText,
+                    typeof value === 'undefined' ? that.options.undefinedText : value,
                     '</td>'].join('');
 
                 if (that.options.columns[j].checkbox || that.options.columns[j].radio) {
@@ -228,7 +369,7 @@
                     type = that.options.columns[j].radio ? 'radio' : type;
 
                     text = ['<td>',
-                        '<input name="btSelectItem" class="checkbox"' +
+                        '<input name="btSelectItem" class="checkbox" data-index="' + i + '"' +
                             sprintf(' type="%s"', type) +
                             sprintf(' checked="%s"', value ? 'checked' : undefined) + ' />',
                         '</td>'].join('');
@@ -237,18 +378,15 @@
             });
 
             html.push('</tr>');
-        });
+        }
 
         this.$body.html(html.join(''));
 
-        this.$body.find('tr').off('click').on('click', function() {
-            that.options.onClickRow(that.data[$(this).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.updateRows();
+            that.data[$(this).data('index')][that.header.stateField] = $(this).prop('checked');
         });
         this.resetView();
     };
@@ -262,6 +400,7 @@
         $.ajax({
             method: this.options.method,
             url: this.options.url,
+            data: this.options.queryParams,
             contentType: 'application/json',
             dataType: 'json',
             success: function(data) {
@@ -285,11 +424,11 @@
         });
     };
 
-    BootstrapTable.prototype.updateRows = function() {
+    BootstrapTable.prototype.updateRows = function(checked) {
         var that = this;
 
         $.each(this.data, function(i, row) {
-            row[that.header.stateField] = that.$selectItem.eq(i).prop('checked');
+            row[that.header.stateField] = checked;
         });
     };
 
@@ -298,6 +437,7 @@
 
     BootstrapTable.prototype.load = function(data) {
         this.initData(data);
+        this.initPagination();
         this.initBody();
     };
 
@@ -339,13 +479,13 @@
     BootstrapTable.prototype.checkAll = function() {
         this.$selectAll.prop('checked', true);
         this.$selectItem.prop('checked', true);
-        this.updateRows();
+        this.updateRows(true);
     };
 
     BootstrapTable.prototype.uncheckAll = function() {
         this.$selectAll.prop('checked', false);
         this.$selectItem.prop('checked', false);
-        this.updateRows();
+        this.updateRows(false);
     };