ソースを参照

Fix #263, #394: add prepend and insertRow methods.

zhixin 11 年 前
コミット
66fc69e896
2 ファイル変更40 行追加5 行削除
  1. 14 0
      docs/_i18n/en/documentation/methods.md
  2. 26 5
      src/bootstrap-table.js

+ 14 - 0
docs/_i18n/en/documentation/methods.md

@@ -38,6 +38,11 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter);`.
         <td>Append the data to table.</td>
         <td>Append the data to table.</td>
     </tr>
     </tr>
     <tr>
     <tr>
+        <td>prepend</td>
+        <td>data</td>
+        <td>Prepend the data to table.</td>
+    </tr>
+    <tr>
         <td>remove</td>
         <td>remove</td>
         <td>params</td>
         <td>params</td>
         <td>
         <td>
@@ -48,6 +53,15 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter);`.
         </td>
         </td>
     </tr>
     </tr>
     <tr>
     <tr>
+        <td>insertRow</td>
+        <td>params</td>
+        <td>
+        Insert a new row, the param contains following properties:<br>
+        index: the row index to insert into.<br>
+        row: the row data.
+        </td>
+    </tr>
+    <tr>
         <td>updateRow</td>
         <td>updateRow</td>
         <td>params</td>
         <td>params</td>
         <td>
         <td>

+ 26 - 5
src/bootstrap-table.js

@@ -466,9 +466,15 @@
             });
             });
     };
     };
 
 
-    BootstrapTable.prototype.initData = function (data, append) {
-        if (append) {
+    /**
+     * @param data
+     * @param type: append / prepend
+     */
+    BootstrapTable.prototype.initData = function (data, type) {
+        if (type === 'append') {
             this.data = this.data.concat(data);
             this.data = this.data.concat(data);
+        } else if (type === 'prepend') {
+            this.data = data.concat(this.data);
         } else {
         } else {
             this.data = data || this.options.data;
             this.data = data || this.options.data;
         }
         }
@@ -1408,7 +1414,14 @@
     };
     };
 
 
     BootstrapTable.prototype.append = function (data) {
     BootstrapTable.prototype.append = function (data) {
-        this.initData(data, true);
+        this.initData(data, 'append');
+        this.initSearch();
+        this.initPagination();
+        this.initBody(true);
+    };
+
+    BootstrapTable.prototype.prepend = function (data) {
+        this.initData(data, 'prepend');
         this.initSearch();
         this.initSearch();
         this.initPagination();
         this.initPagination();
         this.initBody(true);
         this.initBody(true);
@@ -1442,6 +1455,14 @@
         this.initBody(true);
         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.initBody(true);
+    };
+
     BootstrapTable.prototype.updateRow = function (params) {
     BootstrapTable.prototype.updateRow = function (params) {
         if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
         if (!params.hasOwnProperty('index') || !params.hasOwnProperty('row')) {
             return;
             return;
@@ -1585,8 +1606,8 @@
 
 
     var allowedMethods = [
     var allowedMethods = [
         'getSelections', 'getData',
         'getSelections', 'getData',
-        'load', 'append', 'remove',
-        'updateRow',
+        'load', 'append', 'prepend', 'remove',
+        'insertRow', 'updateRow',
         'mergeCells',
         'mergeCells',
         'checkAll', 'uncheckAll',
         'checkAll', 'uncheckAll',
         'check', 'uncheck',
         'check', 'uncheck',