標籤:strong 資料 re c ar 伺服器
Json資料
需要定義jsonReader來跟伺服器端返回的資料做對應,其預設值:
· jsonReader : {
· root: "rows",
· page: "page",
· total: "total",
· records: "records",
· repeatitems: true,
· cell: "cell",
· id: "id",
· userdata: "userdata",
· subgrid: {root:"rows",
· repeatitems: true,
· cell:"cell"
· }
這樣伺服器端返回的資料格式:
· {
· total: "xxx",
· page: "yyy",
· records: "zzz",
· rows : [
· {id:"1", cell:["cell11", "cell12", "cell13"]},
· {id:"2", cell:["cell21", "cell22", "cell23"]},
· ...
· ]
· }
jsonReader的屬性
total |
總頁數 |
page |
當前頁 |
records |
查詢出的記錄數 |
rows |
包含實際資料的數組 |
id |
行id |
cell |
當前行的所有儲存格 |
自訂:
· jQuery("#gridid").jqGrid({
· ...
· jsonReader : {
· root:"invdata",
· page: "currpage",
· total: "totalpages",
· records: "totalrecords",
· cell: "invrow"
· },
· ...
· });
· totalpages: "xxx",
· currpage: "yyy",
· totalrecords: "zzz",
· invdata : [
· {id:"1", invrow:["cell11", "cell12", "cell13"]},
· {id:"2", invrow:["cell21", "cell22", "cell23"]},
· ...
· ]
repeatitems
指明每行的資料是可以重複的,如果設為false,則會從返回的資料中按名字來搜尋元素,這個名字就是colModel中的名字
· jsonReader : {
· root:"invdata",
· page: "currpage",
· total: "totalpages",
· records: "totalrecords",
· repeatitems: false,
· id: "0"
· }
· totalpages: "xxx",
· currpage: "yyy",
· totalrecords: "zzz",
· invdata : [
· {invid:"1",invdate:"cell11", amount:"cell12", tax:"cell13", total:"1234", note:"somenote"},
· {invid:"2",invdate:"cell21", amount:"cell22", tax:"cell23", total:"2345", note:"some note"},
· ...
· ]
此例中,id屬性值為“invid”。
一旦當此屬性設為false時,我們就不必把所有在colModel定義的name值都賦值。因為是按name來進行搜尋元素的,所以他的排序也不是按colModel中指定的排序結果。
使用者資料(user data)
在某些情況下,我們需要從伺服器端返回一些參數但並不想直接把他們顯示到表格中,而是想在別的地方顯示,那麼我們就需要用到userdata標籤
· jsonReader: {
· ...
· userdata: "userdata",
· ...
· }
· {
· total: "xxx",
· page: "yyy",
· records: "zzz",
· userdata: {totalinvoice:240.00, tax:40.00},
· rows : [
· {id:"1", cell:["cell11", "cell12", "cell13"]},
· {id:"2", cell:["cell21", "cell22", "cell23"]},
· ...
· ]
· }
在用戶端我們可以有下面兩種方法得到這些額外資訊:
1. jQuery("grid_id").getGridParam(‘userData‘)
2. jQuery("grid_id").getUserData()
3. jQuery("grid_id").getUserDataItem( key )