2012-11-16 17 views
1

我對JqGrid有所瞭解。我用下面的代碼到網格我的數據:Asp.Net Mvc JqGrid動態col名稱?

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

代碼works.I不能從服務器獲取數據,使用JSON。但我也想動態獲取colNames。我無法找到關於這個主題的任何文檔。我如何動態獲取colNames和colModels?

是這樣的:

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

在此先感謝。

回答

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

+0

感謝答覆電網。事實上,我不需要添加,刪除和編輯。我應該在網格上顯示我的數據。分頁和排序對我來說已經足夠了。我擁有超過一百萬的數據,所以我不應該獲取所有數據並分頁。你對這個問題有什麼建議嗎? (可以使用哪個網格?) –

+0

用兩個鏈接更新答案。這些幫助程序可能會讓您更輕鬆地將任何模型序列化到網格中。 – dove

+0

謝謝。我之前看到過這個鏈接。最後一週我閱讀了關於這個話題的所有文件。使用javascript對我來說是合乎邏輯的。因爲它比mvc幫手更靈活。我會做你的建議。非常感謝...這是預期的答案。 –