pull/2752/merge
morning-star 2025-07-09 08:43:43 +00:00 committed by GitHub
commit 2eda47faab
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 66 additions and 9 deletions

View File

@ -140,6 +140,50 @@ table.render({
该函数非常实用 该函数非常实用
</td>
</tr>
<tr>
<td>
[**ajax**](#options.ajax) <sup>2.12+</sup>
</td>
<td>
<div class="ws-anchor" id="options.ajax">
自定义 ajax 请求,用于发送异步请求。
</div>
```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();
}
});
}
});
```
</td> </td>
</tr> </tr>
</tbody> </tbody>

View File

@ -1196,12 +1196,7 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports) {
that.loading(true); that.loading(true);
// 4代表响应已完成 var ajaxOptions = {
if (that._xhr && that._xhr.readyState !== 4) {
that._xhrAbort = true;
that._xhr.abort();
}
that._xhr = $.ajax({
type: options.method || 'get', type: options.method || 'get',
url: options.url, url: options.url,
contentType: options.contentType, contentType: options.contentType,
@ -1249,7 +1244,18 @@ layui.define(['lay', 'laytpl', 'laypage', 'form', 'util'], function(exports) {
that.errorView('请求异常,错误提示:'+ msg); that.errorView('请求异常,错误提示:'+ msg);
typeof options.error === 'function' && options.error(e, 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'){ //已知数据 } else if(layui.type(options.data) === 'array'){ //已知数据
res = {}; res = {};
var startLimit = curr*options.limit - options.limit; var startLimit = curr*options.limit - options.limit;

View File

@ -711,7 +711,7 @@ layui.define(['table'], function (exports) {
var asyncParseData = asyncSetting.parseData || options.parseData; var asyncParseData = asyncSetting.parseData || options.parseData;
var asyncResponse = asyncSetting.response || options.response; var asyncResponse = asyncSetting.response || options.response;
$.ajax({ var ajaxOptions = {
type: asyncType || 'get', type: asyncType || 'get',
url: asyncUrl, url: asyncUrl,
contentType: asyncContentType, contentType: asyncContentType,
@ -742,7 +742,14 @@ layui.define(['table'], function (exports) {
// 异常处理 todo // 异常处理 todo
typeof options.error === 'function' && options.error(e, msg); typeof options.error === 'function' && options.error(e, msg);
} }
}); }
if(options.ajax){
options.ajax(ajaxOptions, 'treeNodes')
}else{
$.ajax(ajaxOptions);
}
return retValue; return retValue;
} }
trExpanded = trData[LAY_HAS_EXPANDED] = true; trExpanded = trData[LAY_HAS_EXPANDED] = true;