1

我是 JqGrid 的新手。我使用以下代码来网格化我的数据:

<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>

代码有效。我无法使用 json 从服务器获取数据。但我也想动态获取 colNames。我找不到有关此主题的任何文档。如何动态获取 colNames 和 colModels?

像这样的东西:

colNames: data.colNames,
colModel: data.colModels,

提前致谢。

4

1 回答 1

1

你可以这样做,但你可能不值得。您放入 colModels 的数据还必须包含这些列的所有表示逻辑,例如大小、可编辑等。

如果您认为您可以始终如一地从您的属性中计算出所有这些选项,那么它可能会起作用,但是如果不稍微自定义外观,就很难获得适合各种数据的美观网格。您将返回您的 poco 序列化为适合 jqgrid 模式的 json。

我个人的意见是原样离开,或者考虑购买另一个网格,比如从 Telerik 或 rad 控制。确实考虑一下你是否想要一个网格。

两篇文章可能会帮助您使用 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

于 2012-11-16T14:44:08.457 回答