I m new about JqGrid. I use following code to grid my data:
<script type="text/javascript">
jQuery(document).ready(function () {
jQuery("#grid_table").jqGrid({
url: '@Url.Action("GetAll", "Widget")',
datatype: "json",
mtype: 'POST',
colNames: ['ReadDate', 'Name'],
colModel: [
{ name: 'ReadDate', index: 'ReadDate', width: 200, sortable: true, editable: false, editoptions: { readonly: true, size: 10} },
{ name: 'Name', index: 'Name', width: 500, editable: false }
],
jsonReader: {
root: "rows", //array containing actual data
page: "page", //current page
total: "total", //total pages for the query
records: "records", //total number of records
repeatitems: false,
id: "id" //index of the column with the PK in it
},
rowNum: 20,
rowList: [20, 30],
pager: jQuery('#gridpager'),
sortname: 'Name',
viewrecords: true,
sortorder: "asc",
width:968,
height:349
}).navGrid('#gridpager');
});
</script>
Code works.I cant get data from server with using json. But I want to get colNames Dynamicly, too. I cant find any documentation about this topic. How can I get colNames and colModels dynamicly?
something like this:
colNames: data.colNames,
colModel: data.colModels,
Thanks in advance.
You can do this but you it may not be worth it. The data you put into colModels will also have to hold all the presentation logic of these columns, such as size, editable, etc.
If you think you can consistently figure all these options from your properties then it might work but it is hard to get a good looking grid that fits all kinds of data without customising the look a little. You would return your poco serialised as json that fits jqgrid schema.
My personal opinion is to leave as is or look at buying another grid, say from telerik or rad controls. Indeed consider whether you want a grid at all.
Two articles that might help you create the grids dynamically using asp.net-mvc
http://www.codeproject.com/Articles/421189/jqGrid-MVC-Html-Helper
http://www.codeproject.com/Articles/424640/ASP-NET-MVC-HTML-Helper-for-the-jqGrid