PHP User
PHP User

Reputation: 2422

render datatables columns fetched via ajax

I'm using datatables via ajax and display the table like this

var table = $('#data').DataTable( {
        "ajax": "initTable.php",
        "columns": [
            { "data": "orderid" },
            { "data": "first_name"},
            { "data": "last_name"},
            { "data": "unix" },
            { "data": "final_total" }
        ]
    } );

I've tried

{ "data": "first_name" + "data": "last_name"},

But I get an error and table is not displayed. So how can I change the render to display first name next to last_name in the same cell not in the next cell

[UPDATE]

Tried

"ajax": "initTable.php",
"columns": [
            { "data": "orderid" },
            { "data": "first_name"},
            {"data": "last_name"},
            { "data": "unix" },
            { "data": "final_total" }
        ],
        "columnDefs": [
            {
                "render": function ( data, type, row ) {
                    return data + row[2];
                },
                "targets": 1
            },
            { "visible": false,  "targets": [ 2 ] }
        ]

(Note: I have to define column rows because I get many columns (about 20) and want to display just 4 or 5) But I get the first name followed by 'undefined' something like "Andy undefined"

Upvotes: 3

Views: 5239

Answers (1)

Gyrocode.com
Gyrocode.com

Reputation: 58860

Use the code below:

{
    "render": function ( data, type, row ){
        return row["first_name"] + " " + row["last_name"];
    },
    "targets": 1
},

Also there is no need to include last_name column if you're hiding it.

Upvotes: 2

Related Questions