3

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"

2
  • Take a look to column rendering Commented Oct 16, 2016 at 21:29
  • updated please review. I get the last name 'defined' Commented Oct 16, 2016 at 21:41

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Great thanks a lot. And used: { "visible": false, "targets": [ 2 ] } to hide the last name field.
@PHPUser, as I said you don't have to include last_name field in the table and can remove {"data": "last_name"} and ` { "visible": false, "targets": [ 2 ] }` as long as last_name is available in the Ajax response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.