1

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...

1 Answer 1

2

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,
        ...
    });
}
Sign up to request clarification or add additional context in comments.

3 Comments

@santhoshpatil How can we define columns in the mvc View if I need to render different number of columns there?
Hey @Zane, glad this post is helpful to you. If you need help with something it's best to post a new question, that way more people will see it and you'll get some help faster :) Plus people with the same question will be able to find it easier. Good luck!
I finally solved the issue... your answer was helpful. Thanks !!

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.