diff --git a/docs/table/detail/options.ajax.md b/docs/table/detail/options.ajax.md index 27a0989b..16ce579b 100644 --- a/docs/table/detail/options.ajax.md +++ b/docs/table/detail/options.ajax.md @@ -140,6 +140,50 @@ table.render({ 该函数非常实用 + + + + + +[**ajax**](#options.ajax) 2.12+ + + + + +
+ 自定义 ajax 请求,用于发送异步请求。 +
+ +```js +table.render({ + // 自定义 ajax 请求 + // origOptions - 包含了原始的请求参数 + // type - 执行 ajax 请求的来源,'table' 或 'treeNodes' + ajax: function (origOptions, type) { + $.ajax({ + url: origOptions.url, + data: origOptions.data, + dataType: origOptions.dataType, + }) + .done(function (data) { + // 调用原始的 success 回调 + origOptions.success(data); + }) + .fail(function (xhr, status, error) { + // 调用原始的 error 回调 + origOptions.error(xhr, status, error); + }) + .always(function () { + // 调用原始的 complete 回调 + if (typeof origOptions.complete === "function") { + origOptions.complete(); + } + }); + } +}); + +``` + diff --git a/src/modules/table.js b/src/modules/table.js index 7cb3e2aa..1439911f 100644 --- a/src/modules/table.js +++ b/src/modules/table.js @@ -1196,12 +1196,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports) { that.loading(true); - // 4:代表响应已完成 - if (that._xhr && that._xhr.readyState !== 4) { - that._xhrAbort = true; - that._xhr.abort(); - } - that._xhr = $.ajax({ + var ajaxOptions = { type: options.method || 'get', url: options.url, contentType: options.contentType, @@ -1249,7 +1244,18 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports) { that.errorView('请求异常,错误提示:'+ msg); typeof options.error === 'function' && options.error(e, msg); } - }); + }; + + if(options.ajax){ + options.ajax(ajaxOptions, 'table'); + }else{ + // 4:代表响应已完成 + if (that._xhr && that._xhr.readyState !== 4) { + that._xhrAbort = true; + that._xhr.abort(); + } + that._xhr = $.ajax(ajaxOptions); + } } else if(layui.type(options.data) === 'array'){ //已知数据 res = {}; var startLimit = curr*options.limit - options.limit; diff --git a/src/modules/treeTable.js b/src/modules/treeTable.js index df9d6d5d..2f12ce73 100644 --- a/src/modules/treeTable.js +++ b/src/modules/treeTable.js @@ -711,7 +711,7 @@ layui.define(['table'], function (exports) { var asyncParseData = asyncSetting.parseData || options.parseData; var asyncResponse = asyncSetting.response || options.response; - $.ajax({ + var ajaxOptions = { type: asyncType || 'get', url: asyncUrl, contentType: asyncContentType, @@ -742,7 +742,14 @@ layui.define(['table'], function (exports) { // 异常处理 todo typeof options.error === 'function' && options.error(e, msg); } - }); + } + + if(options.ajax){ + options.ajax(ajaxOptions, 'treeNodes') + }else{ + $.ajax(ajaxOptions); + } + return retValue; } trExpanded = trData[LAY_HAS_EXPANDED] = true;