Browse Source

added option to replace jquery ajax method

Yoni Jah 10 years ago
parent
commit
8d5cbc97ac
2 changed files with 25 additions and 9 deletions
  1. 8 1
      docs/_i18n/en/documentation/table-options.md
  2. 17 8
      src/bootstrap-table.js

+ 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>
     </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>data-method</td>
         <td>String</td>
@@ -186,7 +193,7 @@ The table options is defined in `jQuery.fn.bootstrapTable.defaults`.
         <td>data-side-pagination</td>
         <td>String</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>
         <td>pageNumber</td>

+ 17 - 8
src/bootstrap-table.js

@@ -137,6 +137,7 @@
         data: [],
         method: 'get',
         url: undefined,
+        ajax: undefined,
         cache: true,
         contentType: 'application/json',
         dataType: 'json',
@@ -348,7 +349,9 @@
         this.initToolbar();
         this.initPagination();
         this.initBody();
-        this.initServer();
+        if (this.options.sidePagination === 'server') {
+            this.initServer();
+        }
         this.initKeyEvents();
     };
 
@@ -1463,10 +1466,11 @@
                 searchText: this.searchText,
                 sortName: this.options.sortName,
                 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') {
@@ -1499,8 +1503,7 @@
         if (!silent) {
             this.$loading.show();
         }
-
-        $.ajax($.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
+        request = $.extend({}, calculateObjectValue(null, this.options.ajaxOptions), {
             type: this.options.method,
             url: this.options.url,
             data: this.options.contentType === 'application/json' && this.options.method === 'post' ?
@@ -1522,7 +1525,13 @@
                     that.$loading.hide();
                 }
             }
-        }));
+        });
+
+        if (this.options.ajax) {
+            calculateObjectValue(this, this.options.ajax, [request], null);
+        } else {
+            $.ajax(request);
+        }
     };
 
     BootstrapTable.prototype.initKeyEvents = function () {
@@ -2162,7 +2171,7 @@
 
             if (typeof option === 'string') {
                 if ($.inArray(option, allowedMethods) < 0) {
-                    throw "Unknown method: " + option;
+                    throw new Error("Unknown method: " + option);
                 }
 
                 if (!data) {