json 如何将多个数据放入数据表列中?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15174560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to put multiple data in a datatable column?
提问by LittleLebowski
I'm using Datatablesto represent data from a JSON. My JSON is as follows:
我正在使用数据表来表示来自 JSON 的数据。我的 JSON 如下:
[{"name": "John Doe", "email": "[email protected]", "address" : "blah"}]
In Datatables I can easily show these 3 pieces of info in 3 diff columns using the following:
在数据表中,我可以使用以下内容轻松地在 3 个差异列中显示这 3 条信息:
columnDefs = [
{ "mData": "name", "aTargets":[0] },
{ "mData": "email", "aTargets":[1] },
{ "mData": "address", "aTargets":[2] }
];
But the problem is that I want to show "name" and "email" in 1st column and then "address" in the 2nd column. How can I do that? Please guide.
但问题是我想在第一列中显示“姓名”和“电子邮件”,然后在第二列中显示“地址”。我怎样才能做到这一点?请指导。
回答by Danny Lacks
Rather than putting either the name or e-mail in the first column in the column definition, you can use a function to get and print whatever data you want. See the mData section on this page for more details: https://datatables.net/usage/columns. In my example below I use aoColumns because I tested my answer that way; but in the link referenced they used aoColumnDefs and provide elaboration on the type and dataToSet fields. This approach should hopefully help anyone reading this answer whether you use aoColumns or aoColumnDefs.
您可以使用函数来获取和打印您想要的任何数据,而不是将姓名或电子邮件放在列定义的第一列中。有关更多详细信息,请参阅此页面上的 mData 部分:https://datatables.net/usage/columns 。在下面的示例中,我使用了 aoColumns,因为我以这种方式测试了我的答案;但在引用的链接中,他们使用了 aoColumnDefs 并详细说明了 type 和 dataToSet 字段。无论您使用 aoColumns 还是 aoColumnDefs,这种方法都有望帮助任何阅读此答案的人。
For example, using aoColumns rather than aoColumnDefs:
例如,使用 aoColumns 而不是 aoColumnDefs:
aoColumns = [
{ "mData": getNameAndEmail },
{ "mData": "address" }
];
Then define the getNameAndEmail function in JavaScript scope which takes three parameters: the data passed back, the type of action on the data, and if the type is "set" then the data to set.
然后在 JavaScript 范围内定义 getNameAndEmail 函数,它接受三个参数:传回的数据、对数据的操作类型以及如果类型是“设置”则要设置的数据。
function getNameAndEmail(data, type, dataToSet) {
return data.name + "<br>" + data.email;
}
回答by Sruit A.Suk
回答by Paul Richard
columnDefs = [
{
data: {name : "name", email : "email", address : "address"},
mRender : function(data, type, full) {
return data.name+' '+data.email+' '+data.address;
}
}
];
The above code can render multiple data in a column of a datatable.
上面的代码可以在一个数据表的一列中渲染多个数据。
回答by java graduates
[You can use syntax like this:
It worked for me.]"columnDefs":[{
"targets":0,//column index
"data":"first_name",
"render":function(data,type,row){
return data+' '+row['last_name'];
}
}]
[你可以使用这样的语法:
它对我有用。]"columnDefs":[{
"targets":0,//column index
"data":"first_name",
"render":function(data,type,row){
return data+' '+row['last_name'];
}
}]
[thanks]
[谢谢]


