如何将多列数据呈现为一列? [英] How can I render data from multiple columns into one column?

查看:61
本文介绍了如何将多列数据呈现为一列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将两个不同列中的值呈现到我的jquery数据表中

I want to render the values from two different columns into my jquery datatable

https://cdn.datatables.net/1.10.16/js/jquery.dataTables.min.js
https://cdn.datatables.net/1.10.16/css/jquery.dataTables.min.css

$(document).ready(function(){

    var table = $('#table').DataTable({

         "ajax": {
                "url": "data.json",
                "dataSrc": "",
            },

         "columnDefs": [
            {
               "render": function (data, type, row) {
                   var result = 'The id is ' + data[0] + ' and the name is ' + data[1];
                        return result;
                    },
                    "targets": 0,
                },      

         ],

        "columns": [
                {
                    "data": "id"
                },
                {
                    "data": "name"
                }
            ]
    });

});

data.json:

data.json:

[{
    "id": "12",
    "name": "Laura"
}]

但是我的结果是:

The id is 12 and the name is undefined

推荐答案

请更改以下内容:

"columnDefs": [
            {
               "render": function (data, type, row) {
                   var result = 'The id is ' + row["id"] + ' and the name is ' + row["name"];
                   console.log(result);
                        return result;
                    },
                    "targets": 0,
                },      

         ]

使用row["id"]代替data[0];

这篇关于如何将多列数据呈现为一列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆