1
...
// result is a JSON data passed to this function from outside
var result = getMyJSON();
var input = "{aaData: ["+result+"],"+'aoColumns':[
    { 'sTitle': 'Title', 'mDataProp': 'title' }]}";

$(document).ready(function() {
    $('#example').dataTable(input);
});
</script>

<table id='example' class='example' border='1'>

I know that .dataTable() function expects initialization object as specified here: http://datatables.net/ref#aaData If inside the dataTable function I manually paste the JSON data - then it displays everything fine. How can I "prepare" and pass in as an initialization object to dataTables() function? Right now this is not working as it should.

Thanks

1 Answer 1

2

I am not sure why you are building your input var as a string - it is an object. Try this instead

var input = {
    "aaData" : [result],
    "aoColumns" : [{
        "sTitle" : "Title"
    }, {
        "mDataProp" : "title"
    }]
};
Sign up to request clarification or add additional context in comments.

1 Comment

What if I don't want to specify aoColumns attribute. I want to prepare my table's header with few conditions. So I cannot go with it. Will that allow to give a proper table?

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.