解決layui中table異步數(shù)據(jù)請(qǐng)求不支持自定義返回?cái)?shù)據(jù)格式的問(wèn)題
使用版本 layui-v2.3.0
修改:
打開(kāi)layui中table.js源碼
在 Class.prototype.pullData 這個(gè)方法定義內(nèi)部
//獲得數(shù)據(jù)
Class.prototype.pullData = function(curr, loadIndex){
var that = this
,options = that.config
,request = options.request
,response = options.response
,sort = function(){
if(typeof options.initSort === 'object'){
that.sort(options.initSort.field, options.initSort.type);
}
};
that.startTime = new Date().getTime(); //渲染開(kāi)始時(shí)間
if(options.url){ //Ajax請(qǐng)求
var params = {};
params[request.pageName] = curr;
params[request.limitName] = options.limit;
//參數(shù)
var data = $.extend(params, options.where);
if(options.contentType && options.contentType.indexOf("application/json") == 0){ //提交 json 格式
data = JSON.stringify(data);
}
$.ajax({
type: options.method || 'get'
,url: options.url
,contentType: options.contentType
,data: data
,dataType: 'json'
,headers: options.headers || {}
,success: function(res){
// 加入這部分?。?!
// 臨時(shí)解決layui的table組件中response選項(xiàng)不支持多層級(jí)獲取接口數(shù)據(jù)的方法
// ----------------開(kāi)始---------------------
if (typeof options.responseHandler == "function") {
res = options.responseHandler(res);
}
// ----------------結(jié)束---------------------
if(res[response.statusName] != response.statusCode){
that.renderForm();
that.layMain.html('<div class="'+ NONE +'">'+ (res[response.msgName] || '返回的數(shù)據(jù)狀態(tài)異常') +'</div>');
} else {
that.renderData(res, curr, res[response.countName]), sort();
options.time = (new Date().getTime() - that.startTime) + ' ms'; //耗時(shí)(接口請(qǐng)求+視圖渲染)
}
loadIndex && layer.close(loadIndex);
typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
}
,error: function(e, m){
that.layMain.html('<div class="'+ NONE +'">數(shù)據(jù)接口請(qǐng)求異常</div>');
that.renderForm();
loadIndex && layer.close(loadIndex);
}
});
} else if(options.data && options.data.constructor === Array){ //已知數(shù)據(jù)
var res = {}
,startLimit = curr*options.limit - options.limit
res[response.dataName] = options.data.concat().splice(startLimit, options.limit);
res[response.countName] = options.data.length;
that.renderData(res, curr, options.data.length), sort();
typeof options.done === 'function' && options.done(res, curr, res[response.countName]);
}
};
使用:
在建立table的時(shí)候
加入
responseHandler: function (res) {
// 可進(jìn)行數(shù)據(jù)操作
return {
"count": res.data.count,
"data": res.data.companyList,
"code": res.code == 200 ? 0 : -1 //code值為200表示成功
};
},
以上這篇解決layui中table異步數(shù)據(jù)請(qǐng)求不支持自定義返回?cái)?shù)據(jù)格式的問(wèn)題就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
基于JavaScript實(shí)現(xiàn)彈出框效果
彈出框在網(wǎng)站頁(yè)面中是必不可少的一部分,今天借助腳本之家平臺(tái)給大家分享使用js實(shí)現(xiàn)簡(jiǎn)單的彈出框效果,感興趣的朋友一起學(xué)習(xí)吧2016-02-02
cropper.js和exif.js實(shí)現(xiàn)頭像上傳縮放裁剪旋轉(zhuǎn)
這篇文章主要為大家詳細(xì)介紹了cropper.js和exif.js實(shí)現(xiàn)頭像上傳縮放裁剪旋轉(zhuǎn),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
JS中的6種打斷點(diǎn)的方式實(shí)例總結(jié)
斷點(diǎn)調(diào)試是啥?難不難? 斷點(diǎn)調(diào)試其實(shí)并不是多么復(fù)雜的一件事,簡(jiǎn)單的理解無(wú)外呼就是打開(kāi)瀏覽器,打開(kāi)sources找到j(luò)s文件,在行號(hào)上點(diǎn)一下罷了,下面這篇文章主要給大家介紹了關(guān)于JS中6種打斷點(diǎn)方式的相關(guān)資料,需要的朋友可以參考下2022-04-04
JavaScript 在網(wǎng)頁(yè)上單擊鼠標(biāo)的地方顯示層及關(guān)閉層
在網(wǎng)頁(yè)上單擊鼠標(biāo)的地方顯示層,供用戶選擇地點(diǎn),同時(shí)把用戶選擇的地點(diǎn)顯示在文本框中。主要是控制層的顯示、隱藏,感興趣的朋友可以參考下2012-12-12

