ソースを参照

Merge pull request #664 from yonjah/master

Added option to replace jquery ajax method
文翼 10 年 前
コミット
6518a51e96

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

@@ -169,6 +169,11 @@ The calling method syntax: `$('#table').bootstrapTable('method', parameter);`.
         <td>Reset the bootstrap table view, for example reset the table height.</td>
         <td>Reset the bootstrap table view, for example reset the table height.</td>
     </tr>
     </tr>
     <tr>
     <tr>
+        <td>resetWidth</td>
+        <td>none</td>
+        <td>Resizes header and footer to fit current columns width</td>
+    </tr>
+    <tr>
         <td>destroy</td>
         <td>destroy</td>
         <td>none</td>
         <td>none</td>
         <td>Destroy the bootstrap table.</td>
         <td>Destroy the bootstrap table.</td>

+ 8 - 1
docs/_i18n/en/documentation/table-options.md

@@ -102,6 +102,13 @@ The table options is defined in `jQuery.fn.bootstrapTable.defaults`.
         <td>The data to be loaded.</td>
         <td>The data to be loaded.</td>
     </tr>
     </tr>
     <tr>
     <tr>
+        <td>ajax</td>
+        <td>data-ajax</td>
+        <td>Function</td>
+        <td>undefined</td>
+        <td>A method to replace ajax call. Should implement the same API as jQuery ajax method</td>
+    </tr>
+    <tr>
         <td>method</td>
         <td>method</td>
         <td>data-method</td>
         <td>data-method</td>
         <td>String</td>
         <td>String</td>
@@ -186,7 +193,7 @@ The table options is defined in `jQuery.fn.bootstrapTable.defaults`.
         <td>data-side-pagination</td>
         <td>data-side-pagination</td>
         <td>String</td>
         <td>String</td>
         <td>'client'</td>
         <td>'client'</td>
-        <td>Defines the side pagination of table, can only be 'client' or 'server'.</td>
+        <td>Defines the side pagination of table, can only be 'client' or 'server'. Using 'server' side requires either setting the 'url' or 'ajax' option</td>
     </tr>
     </tr>
     <tr>
     <tr>
         <td>pageNumber</td>
         <td>pageNumber</td>

+ 27 - 8
src/bootstrap-table.js

@@ -137,6 +137,7 @@
         data: [],
         data: [],
         method: 'get',
         method: 'get',
         url: undefined,
         url: undefined,
+        ajax: undefined,
         cache: true,
         cache: true,
         contentType: 'application/json',
         contentType: 'application/json',
         dataType: 'json',
         dataType: 'json',
@@ -347,7 +348,9 @@
         this.initToolbar();
         this.initToolbar();
         this.initPagination();
         this.initPagination();
         this.initBody();
         this.initBody();
-        this.initServer();
+        if (this.options.sidePagination === 'server') {
+            this.initServer();
+        }
         this.initKeyEvents();
         this.initKeyEvents();
     };
     };
 
 
@@ -1466,10 +1469,11 @@
                 searchText: this.searchText,
                 searchText: this.searchText,
                 sortName: this.options.sortName,
                 sortName: this.options.sortName,
                 sortOrder: this.options.sortOrder
                 sortOrder: this.options.sortOrder
-            };
+            },
+            request;
 
 
-        if (!this.options.url) {
-            return;
+        if (!this.options.url && !this.options.ajax) {
+            throw new Error("Using Server requires either a `url` or a custom `ajax` method");
         }
         }
 
 
         if (this.options.queryParamsType === 'limit') {
         if (this.options.queryParamsType === 'limit') {
@@ -1502,8 +1506,7 @@
         if (!silent) {
         if (!silent) {
             this.$loading.show();
             this.$loading.show();
         }
         }
-
-        $.ajax($.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
+        request = $.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
             type: this.options.method,
             type: this.options.method,
             url: this.options.url,
             url: this.options.url,
             data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
             data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
@@ -1525,7 +1528,13 @@
                     that.$loading.hide();
                     that.$loading.hide();
                 }
                 }
             }
             }
-        }));
+        });
+
+        if (this.options.ajax) {
+            calculateObjectValue(this, this.options.ajax, [request], null);
+        } else {
+            $.ajax(request);
+        }
     };
     };
 
 
     BootstrapTable.prototype.initKeyEvents = function () {
     BootstrapTable.prototype.initKeyEvents = function () {
@@ -2077,6 +2086,15 @@
         this.initServer(params && params.silent, params && params.query);
         this.initServer(params && params.silent, params && params.query);
     };
     };
 
 
+    BootstrapTable.prototype.resetWidth = function () {
+        if (this.options.showHeader && this.options.height) {
+            this.fitHeader();
+        }
+        if (this.options.showFooter) {
+            this.fitFooter();
+        }
+    };
+
     BootstrapTable.prototype.showColumn = function (field) {
     BootstrapTable.prototype.showColumn = function (field) {
         this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
         this.toggleColumn(getFieldIndex(this.options.columns, field), true, true);
     };
     };
@@ -2146,6 +2164,7 @@
         'checkBy', 'uncheckBy',
         'checkBy', 'uncheckBy',
         'refresh',
         'refresh',
         'resetView',
         'resetView',
+        'resetWidth',
         'destroy',
         'destroy',
         'showLoading', 'hideLoading',
         'showLoading', 'hideLoading',
         'showColumn', 'hideColumn',
         'showColumn', 'hideColumn',
@@ -2167,7 +2186,7 @@
 
 
             if (typeof option === 'string') {
             if (typeof option === 'string') {
                 if ($.inArray(option, allowedMethods) < 0) {
                 if ($.inArray(option, allowedMethods) < 0) {
-                    throw "Unknown method: " + option;
+                    throw new Error("Unknown method: " + option);
                 }
                 }
 
 
                 if (!data) {
                 if (!data) {