Multiple Ajax Objects in One Cell

Multiple Ajax Objects in One Cell

dbaldersdbalders Posts: 3Questions: 1Answers: 0

Hello,

I am using ajax to populate my datatable. Our rest server has 2 objects for Name, one for first name, and one for last name. In my datatable, I want to combine those into one cell instead of needing to split it into two. Here is my current code:

$('#summary-contacts-table').dataTable({
    ajax: {
        url: restURL,
        dataSrc: ""
    },
    dom:            "Tfrtip",
    bInfo:          false,
    searching:      false, 
    paging:         false,
    columns: [
        { "data": "name_first" },
        { "data": "role" },
    ]
});

For the "data": "name_first", I want it to be something like "data": "name_last" + ", " + "name_first" but the few ways I have tried to do this have resulted in datatables errors. Has anyone done something like this before? Is there a way to squish that data into one cell?

Thanks,
David Balderston

This question has an accepted answers - jump to answer

Answers

  • ignignoktignignokt Posts: 146Questions: 4Answers: 39
    edited February 2015 Answer ✓
    $('#summary-contacts-table').dataTable({
        ajax: {
            url: restURL,
            dataSrc: ""
        },
        dom:            "Tfrtip",
        bInfo:          false,
        searching:      false,
        paging:         false,
        columns: [
            {"data":"name_last",
            fnCreatedCell: function (nTd, sData, oData, iRow, iCol) {
                $(nTd).append(', '+oData['name_first']);
            }
        },
            { "data": "role" }
        ]
    });
    
  • dbaldersdbalders Posts: 3Questions: 1Answers: 0

    Amazing! Thank you very much ignignokt!

This discussion has been closed.