How to put some data in a data column?

I use Datatables to represent data from JSON. My JSON looks like this:

[{"name": "John Doe", "email": "john@gmail.com", "address" : "blah"}]

In Datatables, I can easily show these 3 pieces of information in 3 diff columns using the following:

columnDefs = [ 
                { "mData": "name", "aTargets":[0] },
                { "mData": "email", "aTargets":[1] },
                { "mData": "address", "aTargets":[2] }
             ]; 

But the problem is that I want to show the “name” and “email address” in the 1st column, and then the “address” in the 2nd column. How can i do this? Please guide.

+8
source share
4 answers

, , , . . mData : https://datatables.net/usage/columns. aoColumns, ; , , aoColumnDefs dataToSet. , , , , aoColumns aoColumnDefs.

, aoColumns, aoColumnDefs:

aoColumns = [ 
    { "mData": getNameAndEmail },
    { "mData": "address" }
]; 

getNameAndEmail JavaScript, : , "", .

function getNameAndEmail(data, type, dataToSet) {
    return data.name + "<br>" + data.email;
}
+12

, :

"aoColumns": [
    { "mData": "i_id" },
    { "mData": "i_name" },
    { "mData": function (data, type, dataToSet) {
        return data.i_id + "<br/>" + data.i_name;
    }}
],

+9
columnDefs = [ 
            { 
             data: {name : "name", email : "email", address : "address"},
             mRender : function(data, type, full) {
                return data.name+' '+data.email+' '+data.address; 
              } 
            }
         ]; 

The code above can display multiple data in a data column.

+5
source

[You can use the syntax like this:
This worked for me.]
"columnDefs":[{ "targets":0,//column index "data":"first_name", "render":function(data,type,row){ return data+' '+row['last_name']; } }]

[Thank]

0
source

All Articles