2010-11-01 121 views
1

我發現/看到的每個jsGrid使用示例都顯示了通過Ajax請求加載的數據。我想根據已有的數據加載網格;除非技術要求,否則單獨的請求是完全不必要的。如何在沒有Ajax請求的情況下加載jqGrid?

我真的希望我的控制器,拉網格中顯示所需要的數據,把它傳遞給我的看法,讓jqGrid的做它的事基於本地數據,而不是發起另一個請求。我無法想象這是不可能的,但我甚至沒有發現一個不使用url配置的例子來獲取JSON格式的數據。

當然,數據裝載不是這個窄,但也有人點我到不是AJAX爲中心的一個例子嗎?

謝謝。

回答

2

從版本3.7開始,jqgrid擁有全面的本地支持,包括數據排序分頁等。

所以喲可以使用datadatastr參數。需要使用localReader可參見documentation。這裏是一個簡單的例子:

var mydata = [ 
    { id: "1", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "2", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "3", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "4", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "5", invdate: "2007-10-05", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "6", invdate: "2007-09-06", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "7", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "8", invdate: "2007-10-03", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "9", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "10", invdate: "2007-10-01", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "11", invdate: "2007-10-02", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "12", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "13", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "14", invdate: "2007-10-05", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "15", invdate: "2007-09-06", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" }, 
    { id: "16", invdate: "2007-10-04", name: "test", note: "note", amount: "200.00", tax: "10.00", total: "210.00" }, 
    { id: "17", invdate: "2007-10-03", name: "test2", note: "note2", amount: "300.00", tax: "20.00", total: "320.00" }, 
    { id: "18", invdate: "2007-09-01", name: "test3", note: "note3", amount: "400.00", tax: "30.00", total: "430.00" } 
]; 
var grid = $("#list"); 
grid.jqGrid({ 
    data: mydata, 
    datatype: "local", 
    colNames: ['Inv No', 'Date', 'Client', 'Amount', 'Tax', 'Total', 'Notes'], 
    colModel: [ 
     { name: 'id', index: 'id', key: true, width: 70, sorttype: "int" }, 
     { name: 'invdate', index: 'invdate', width: 90, sorttype: "date" }, 
     { name: 'name', index: 'name', width: 100 }, 
     { name: 'amount', index: 'amount', width: 80, align: "right", sorttype: "float" }, 
     { name: 'tax', index: 'tax', width: 80, align: "right", sorttype: "float" }, 
     { name: 'total', index: 'total', width: 80, align: "right", sorttype: "float" }, 
     { name: 'note', index: 'note', width: 150, sortable: false } 
    ], 
    pager: '#pager', 
    rowNum: 10, 
    rowList: [5, 10, 20, 50], 
    sortname: 'id', 
    sortorder: 'asc', 
    viewrecords: true, 
    height: "100%", 
    caption: "Simple loading of local data" 
}); 
grid.jqGrid('navGrid', '#pager', { add: false, edit: false, del: false, search: true, refresh: true }); 
1

看那jqGrid的例子here。展開「加載數據」並點擊「陣列數據」。

+0

+1因爲我查看了數組加載器,因爲我非常注重格式化選項。 – 2010-11-01 18:53:46

相關問題