santhosh patil
santhosh patil

Reputation: 86

Display Column name from server side,Datatable jquery

Now i am displaying the Column Name statically from Html View .. For Eg these are all my column headers: PlatformName, length, width, height. These are included in table in my HTML view.

And in client side using Datatable initialization I am displaying

"aoColumns" : [
    "mDataProp" : "PlatformName",
    "mDataProp" : "length "
    "mDataProp" : "width "
    "mDataProp" : "height "
] 

Everything works fine for me. But my new task is to add these column Name from the server side(i need to process the column name from the server side and send it to the client through JSON format) , so that the columns can be display dynamically in table.

Can anyone give an insight to me on how can this be done,,link to a blog or sample code would be of great help...

Upvotes: 1

Views: 2526

Answers (1)

colestrode
colestrode

Reputation: 10658

You'll need to make an AJAX call to the server to get the column definitions. Below is a rough example of how you could do that. I'm assuming that the data you return from the server is in JSON format and that it has a property called columns that is in the correct format for the datatables aoColumns property.

$(function() {
    //use jQuery AJAX to get column definitions
    $.ajax({
        url: 'path/to/serverside/columns',
        dataType: 'json',
        success: function(data) {
            //once you have column definitions, use them to initialize your table 
            initializeTable(data.columns);
        }
    });
});

//initialize datatables as you were before
function initializeTable(columnsDef) {
    $('#myTable').datatables({
        ...
        aoColumns: columnsDef,
        ...
    });
}

Upvotes: 2

Related Questions