ASP.NET MVC: Displaying JSON in a grid where JSON data only known at runtime
I've spent a long time trying to work this out, but no success thus far. Basically, there will be a JSON dataset coming via a jQuery GET or similar, which will differ in the number of columns and column names. Datatypes will vary. One time it may be Name=Bob, Age=50 (etc) while next time it may be name, address, and occupation.
All I'd like to do is present that in a grid in my MVC view, which ideally provides some paging/sorting etc. Column names should be derived from the labels in the JSON. I'm happy to pay for such a component if needed.
That's it! If I fail I guess I'll just have to render HTML and do without the other features.
Thanks
PS. The actual data will come from anonymous collections, generated by LI开发者_JS百科NQ queries in the controller. Binding to such a type would be the perfect solution if anyone knows how (on demand paging would be great) but no luck yet. 2 Days of getting the Telerik MVC grid to bind to an anonymous type or any conversion of it (dynamics, tables, etc) has been unsuccessful so far. I've added telerik MVC and Jqgrid as tags in case there are gurus monitoring those sections who know the answer.
You might want to look at the DataTables jQuery plug-in. It has built in paging, sorting and a lot of other cool features. Most of the examples show rendering the entire table on the page and then it takes over with the paging, etc. It does however, also support the use of server side binding and here is a good example of that binding within a ASP.NET MVC site - jQuery DataTables and ASP.NET MVC Integration.
I have been using this plug-in for about a month now and have been very happy with it so far.
I use only jqGrid and no Telerik MVC, so I can help you only in he jqGrid implementation.
There are some different approaches how you can implement variable number of columns and ather jqGrid parameters.
The easiest way is to use GridUnload method (see the old demo from the answer) which recreate the basis <table>
element of the grid after which you can create new jqGrid. So you can just has an $.ajax
call which load from the server colModel
which you need and inside of success
handler of the $.ajax
you call GridUnload and then create jqGrid.
If your has variable number of columns with the knows format you can use another aproatch described here and here (see the demo and the demo). One need just have some hidden columns, and inside of loadComplete
to make it visible and overwrite the column names.
精彩评论