plugin doesn't understand jsonresult type from mvc controller?
plugin doesn't understand jsonresult type from mvc controller?
I have a ASP.NET MVC application whereby the MVC controller method returns a jsonresult or json object.
How do I need to bridge the gap between the json this plug-in expects and what MVC is returning?
Here is an example of the jsonresult from MVC:
[{"STORE":"0112"},{"STORE":"0113"}]
This compared to what the DataTable plug-in seems to want:
{ "aaData": [["0112"],["0113"]] }
What is the best way to bridge this gap between a MVC jsonresult and this plug-in? I guess json comes in various forms. I dunno...I'm a noob. I was kind of hoping the plug-in would understand the MVC jsonresult as is...but that was wishful thinking on my part I guess. Or, is there something I'm missing? Anyone working with ASP.NET MVC and/or this plug-in have any input?
How do I need to bridge the gap between the json this plug-in expects and what MVC is returning?
Here is an example of the jsonresult from MVC:
[{"STORE":"0112"},{"STORE":"0113"}]
This compared to what the DataTable plug-in seems to want:
{ "aaData": [["0112"],["0113"]] }
What is the best way to bridge this gap between a MVC jsonresult and this plug-in? I guess json comes in various forms. I dunno...I'm a noob. I was kind of hoping the plug-in would understand the MVC jsonresult as is...but that was wishful thinking on my part I guess. Or, is there something I'm missing? Anyone working with ASP.NET MVC and/or this plug-in have any input?
This discussion has been closed.
Replies
Json is much like XML in that it's a data transfer method, structured according to particular rules. Like in XML an application using Json needs to know the formatting (schema) of what the data being passed to it is. This is why DataTables is tripping up over your data - as you rightly point out, DataTables expects the second formatting rather than all these "STORE" parameters (... ;-) ).
So what you need to do is convert from one type to the other. On the client-side this could be done by making the Ajax call to get the data yourself (assuming you are not using server-side processing, if you are then you'll need to use a custom 'get' function for the data) and then format it into the array that DataTables wants. From there you can use the aaData parameter to pass your formatted array into DataTables at initialisation time.
Hope this helps!
Allan
{ "aaData": [["0112"],["0113"]] }
To initialize the table, I was thinking all I had to do was this but it isn't working:
$('#example').dataTable(aaData);
I'm probably missing something dumb or that is incorrect.
$('#example').dataTable( { "aaData": aaData } );
should probably do it.
Allan
$('#example').dataTable( { "aaData": eval(aaData) } );
My table was already defined on the page with a header/footer. But, if I don't specify the "aoColumns", then it is rendering null for the header columns. Any way you know of to avoid having to specify "aoColumns" when they are already defined in the thead? it seems like i have a hybrid solution as in the example you are creating the html table in script whereas mine is defined on the page but with no row data until i make the ajax call, format the data, and associate the data to the plugin.
Allan