基于layuitable返回的值的多级嵌套的解决⽅法
我在学习layui的过程中遇到了table返回值的问题,如果服务器端返回给你的数据是多级嵌套的话,那你在前台是解析不了的,在table.js源码中 它渲染数据是⽤了
data = res[options.response.dataName] || []
这个意味着它源码不⽀持嵌套数据
举个例⼦把 ⽐如服务器端返回的数据中data>dataList>list
把这个数据返回给前段解析出来的是 res[data.dataList.list]类似这种的结构,当然解析不了,所以我写了⼀个⽅法处理返回的数据
function searchData(response,res,name){var data = new Object();
var arr = response[name].split(\"/\"),pre = arr[0];
data[pre] = res[pre];
for(var i = 1;ivar next = arr[i];data[pre] = data[pre][next];}}
然后再table.js中找到ajax下的success中 第⼀⾏写
try{
countNameInfo = response.countName;dataNameInfo = response.dataName;//console.log(options.response)
res.newcountName = searchData(response,res,“countName”);res.newdataName = searchData(response,res,“dataName”);}catch(err){
console.log(err.message);}
newcountName,newdataName在pullData中⾃⼰定义 之后让
response.countName = countNameInfo;response.dataName = dataNameInfo;
最后在你table.render中的response中写
countName: ‘你多级嵌套的节点值' //数据总数的字段名称,默认:count,dataName: ‘你多级嵌套的节点值' //数据列表的字段名称,默认:data
以上⾯我写的例⼦为例:‘data/dataList/list'
OK ⾄此你就可以处理多级嵌套的返回值了,如有不对的地⽅,还望多多包含
这篇基于layui table返回的值的多级嵌套的解决⽅法就是⼩编分享给⼤家的全部内容了,希望能给⼤家⼀个参考,也希望⼤家多多⽀持。