layui表格数据不符合规范的解决方法

最近用json处理layui的表格数据。
发现显示表格数据不规范
于是上网查资料

table.render({
    
    
            elem: '#demo'
            ,height: 312
            ,url: '/t' //数据接口

            ,page: true //开启分页
            ,cols: [[ //表头
                {
    
    field: 'password', title: '用户名', width:80}
                ,{
    
    field: 'username', title: '密码', width:80}

            ]]
            ,parseData: function(res) {
    
     //res 即为原始返回的数据
                console.log(res);
                return {
    
    
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": 10000, //解析数据长度
                    "data": res//解析数据列表
                };
            }
        });

结果发现还是不对,后来我查看我返回的数据格式

{
    
    "password":"123","username":"123"}

而layui给出的官方文档显示的data标准为

data:[{
    
    },{
    
    }]

这时候我才发现返回的格式少了[]
改成

table.render({
    
    
            elem: '#demo'
            ,height: 312
            ,url: '/t' //数据接口

            ,page: true //开启分页
            ,cols: [[ //表头
                {
    
    field: 'password', title: '用户名', width:80}
                ,{
    
    field: 'username', title: '密码', width:80}

            ]]
            ,parseData: function(res) {
    
     //res 即为原始返回的数据
                console.log(res);
                return {
    
    
                    "code": 0, //解析接口状态
                    "msg": "", //解析提示文本
                    "count": 10000, //解析数据长度
                    "data": [res]//解析数据列表
                };
            }
        });

这样后就正确了。

猜你喜欢

转载自blog.csdn.net/naiue/article/details/110246696