Merge 数据表-将列合并在一起
我有这些数据库列,但我希望它们位于一列中。我该怎么做?我想是和伦德先生在一起Merge 数据表-将列合并在一起,merge,datatables,multiple-columns,Merge,Datatables,Multiple Columns,我有这些数据库列,但我希望它们位于一列中。我该怎么做?我想是和伦德先生在一起 /* Address */ {"sTitle": "Address", "bVisible": true, "bSearchable": true}, /* City */ {"sTitle": "City",
/* Address */
{"sTitle": "Address",
"bVisible": true,
"bSearchable": true},
/* City */
{"sTitle": "City",
"bVisible": true,
"bSearchable": true},
/* State */
{"sTitle": "State",
"bVisible": true,
"bSearchable": true},
/* Zip */
{"sTitle": "Zip",
"bVisible": true,
"bSearchable": true},
假设datatables返回的列是address、city、state、zip 1-4 如果返回的数据是常规数组
{ "mData": 0 , //or address field
"mRender" : function ( data, type, full ) {
//data = mData
//full is the full array address= [0] city = [1] state=[2] zip=[3]
return data+', '+full[1]+', '+full[2]+', '+full[3];}
},
如果数据是关联数组
{ "mData": 'address' ,
"mRender" : function ( data, type, full ) {
return data+', '+full['city']+', '+full['state']+', '+full['zip'];}
},
或者,您可以独立于mData调用mRender(尽管在这种情况下似乎不需要它)
编辑:对于datatables 1.10,只需稍微更改名称,删除“m”
*注意:我没有考虑是否应该将此数据存储在一列中,只是显示了它是如何完成的数据库表字段:id、first\u name、last\u name、email 数据表:全名,电子邮件(无id)
为什么要将它们放在一列中?您可以序列化数据,但现在使用的方式将更易于搜索。我认为如果所有地址信息都在一列中,则更易于理解,不是吗?感谢您的介绍。真的很有帮助。请回答这个问题以改进格式。
{ "mData": null ,
"mRender" : function ( data, type, full ) {
return full['address']+', '+full['city']+', '+full['state']+', '+full['zip'];}
},
{ "data": null ,
"render" : function ( data, type, full ) {
return full['address']+', '+full['city']+', '+full['state']+', '+full['zip'];}
},
"columns": [
{
"mData": null ,
"mRender" : function ( data, type, full ) {
return full['first_name']+' '+full['last_name'];
}
},
$(document).ready(function() {
$('#example').DataTable( {
"columnDefs": [
{
// The `data` parameter refers to the data for the cell (defined by the
// `data` option, which defaults to the column being worked with, in
// this case `data: 0`.
"render": function ( data, type, row ) {
return data +' ('+ row[3]+')';
},
"targets": 0
},
{ "visible": false, "targets": [ 3 ] }
]
} );
} );