jqgrid how to dynamically create columns

2019-06-28 00:51发布

I am trying to implement a jquery in mvc, there are a couple of tutorials around on this, but I don't want the jquery to have the columns hard coded (or in fact anything to be hard coded - this is logic that should be in the controller.

So... looking at the code below you will see a colNames:, and colModel: how can I write these so that I can loop through a Model, adding a column to the grid for each column item? Essentually what I want in the end is a generic jqGrid statement, with the only thing that changes is the code in the controller/s!

Query("#list").jqGrid({
    url: '/Home/GetMovieData/',
    datatype: 'json',
    mtype: 'GET',
    colNames: ['id', 'Movie Name', 'Directed By', 'Release Date', 'IMDB Rating', 'Plot', 'ImageURL'],
    colModel: [
        **{ name: 'id', index: 'Id', width: 55, sortable: false, hidden: true },
        { name: 'Movie Name', index: 'Name', width: 250 },
        { name: 'Directed By', index: 'Director', width: 250, align: 'right' },
        { name: 'Release Date', index: 'ReleaseDate', width: 100, align: 'right' },
        { name: 'IMDB Rating', index: 'IMDBUserRating', width: 100, align: 'right' },
        { name: 'Plot', index: 'Plot', width: 55, hidden: true },
        { name: 'ImageURL', index: 'ImageURL', width: 55, hidden: true}],**
    pager: jQuery('#pager'),
    rowNum: 5,
    rowList: [5, 10, 20],
    sortname: 'id',
    sortorder: "desc",
    height: '100%', 
    width: '100%',
    viewrecords: true,
    imgpath: '/Content/jqGridCss/redmond/images',
    caption: 'Movies from 2008'
});