jqGrid的使用笔记:4. 数据操作

[b]数据类型[/b]
xml, json, jsonp, array, xmlstring, jsonstring, script, function (…)

xml类型默认解析如下:

jQuery("#gridid").jqGrid({
...
xmlReader : {
root: "rows",
row: "row",
page: "rows>page",
total: "rows>total",
records : "rows>records",
repeatitems: true,
cell: "cell",
id: "[id]",
userdata: "userdata",
subgrid: {
root:"rows",
row: "row",
repeatitems: true,
cell:"cell"
}
},
...
});


json数据默认解析如下:

jQuery("#gridid").jqGrid({
...
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
cell: "cell",
id: "id",
userdata: "userdata",
subgrid: {
root:"rows",
repeatitems: true,
cell:"cell"
}
},
...
});


[b]XML数据[/b]
XML数据解析时有几个重要的元素:root, row, page, total, records, repeattimes, cell,id
举个例子:

......
<invoices>
<request>true</request>
...
<currentpage>1</currentpage> // page
<totalpages>10</totalpages> //total
<totalrecords>20</totalrecords> //records

<result> // root
<invoice asin='12345'> // id

// 都是用 <invcell></invcell>, repeateitems: true

<invcell>data1</invcell> // cell
<invcell>data2</invcell>
<invcell>data3</invcell>
<invcell>data4</invcell>
<invcell>data5</invcell>
<invcell>data6</invcell>

</invoice>
...
</result>
</invoices>


使用js解析时:

jQuery("#gridid").jqGrid({
...
xmlReader: {
root:"result",
row:"invoice",
page:"invoices>currentpage",
total:"invoices>totalpages",
records:"invoices>totalrecords",
repeatitems:true,
cell:"invcell",
id : "[asin]"
},
...
});


如果XML结构如下:

...
<invoices>
<request>true</request>
...
<currentpage>1</currentpage>
<totalpages>10</totalpages>
<totalrecords>20</totalrecords>
<result>
<invoice>
// 以下TAG不同,这repeatitems: false
<asin>12345</asin>
<invoiceno>data1</invoiceno>
<invoicedate>data2</invoicedate>
<invoiceamount>data3</invoiceamount>
<invoicetax>data4</invoicetax>
<invoicetotal>data5</invoicetotal>
<notes>data6</notes>
</invoice>
...
</result>
</invoices>


js源码:

jQuery("#gridid").jqGrid({
...
xmlReader: {
root:"result",
row:"invoice",
page:"invoices>currentpage",
total:"invoices>totalpages",
records:"invoices>totalrecords",
repeatitems:false,
id : "asin"
},
...
});

and our colModel from the example should look like this:

<code javascript>
jQuery("#gridid").jqGrid({
...
// 作个XML TAG和 colModel的映射关系
// 如果不作映射关系,name的值必须是xml里的tag名称。
colModel :[
{name:'invid', index:'invid', width:55, xmlmap:"invoiceno"},
{name:'invdate', index:'invdate', width:90, xmlmap:"invoicedate"},
{name:'amount', index:'amount', width:80, align:'right', xmlmap:"invoiceamount"},
{name:'tax', index:'tax', width:80, align:'right', xmlmap:"invoicetax"},
{name:'total', index:'total', width:80, align:'right', xmlmap:"invoicetotal"},
{name:'note', index:'note', width:150, sortable:false, xmlmap:"notes"}
],
xmlReader: {
root:"result",
row:"invoice",
page:"invoices>currentpage",
total:"invoices>totalpages",
records:"invoices>totalrecords",
repeatitems:false,
id : "asin"
},
...
});


[b]xml string[/b]

<script>
var mystr =
"<?xml version='1.0' encoding='utf-8'?>
<invoices>
<rows>
<row>
<cell>data1</cell>
<cell>data2</cell>
<cell>data3</cell>
<cell>data4</cell>
<cell>data5</cell>
<cell>data6</cell>
</row>
</rows>
</invoices>";

jQuery(document).ready(function(){
jQuery("#list").jqGrid({
datatype: 'xmlstring',
datastr : mystr,
colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
colModel :[
{name:'invid', index:'invid', width:55, sorttype:'int'},
{name:'invdate', index:'invdate', width:90, sorttype:'date', datefmt:'Y-m-d'},
{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,
viewrecords: true,
caption: 'My first grid'
});
});
</script>


[b]数据类型:json, jsonp, (jsonstring)[/b]
主要数据元素:root, page, total,records, row, cell, id, repeatitems,

默认的JS结构:

jQuery("#gridid").jqGrid({
...
jsonReader : {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: true,
cell: "cell",
id: "id",
userdata: "userdata",
subgrid: {root:"rows",
repeatitems: true,
cell:"cell"
}
},
...
});


json的数据结构:

{
"total": "xxx",
"page": "yyy",
"records": "zzz",
"rows" : [
{"id" :"1", "cell" :["cell11", "cell12", "cell13"]},
{"id" :"2", "cell":["cell21", "cell22", "cell23"]},
...
]
}


repeatitems的使用:

jQuery("#gridid").jqGrid({
...
jsonReader : {
root:"invdata",
page: "currpage",
total: "totalpages",
records: "totalrecords",
repeatitems: false,
id: "0"
},
...
});


json:

{
"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"},
...
]
}


[b]json string[/b]
colModel:

jQuery("#gridid").jqGrid({
...
colModel:[
{name:'name',label:'Name', width:150,editable: true},
{name:'id',width:50, sorttype:"int", editable: true,formatter:strongFmatter},
{name:'email',label:'Email', width:150,editable: true,formatter:'email'},
{name:'stock',label:'Stock', width:60, align:"center", editable: true,formatter:'checkbox',edittype:"checkbox"},
{name:'item.price',label:'Price', width:100, align:"right", editable: true,formatter:'currency'},
{name:'item.weight',label:'Weight',width:60, align:"right", editable: true,formatter:'number'},
{name:'ship',label:'Ship Via',width:90, editable: true,formatter:'select', edittype:"select",editoptions: value:"2:FedEx;1:InTime;3:TNT;4:ARK;5:ARAMEX"}},
{name:'note',label:'Notes', width:100, sortable:false,editable: true,edittype:"textarea", editoptions:{rows:"2",cols:"20"}}
],
...
});


json string:

{
"page":"1",
"total":2,
"records":"13",
"rows":[
{"id":"12345","name":"Desktop Computers","email":"josh@josh.com","item":{"price":"1000.72", "weight": "1.22" }, "note": "note", "stock": "0","ship": "1"},
{"id":"23456","name":"<var>laptop</var>","note":"Long text ","stock":"yes","item":{"price":"56.72", "weight":"1.22"},"ship": "2"},
{"id":"34567","name":"LCD Monitor","note":"note3","stock":"true","item":{"price":"99999.72", "weight":"1.22"},"ship":"3"},
{"id":"45678","name":"Speakers","note":"note","stock":"false","ship":"4"}
]
}


[b]jsonReader as function[/b]

jsonReader: {
repeatitems: false,
id: "Id",
root: function (obj) { return obj; },
page: function (obj) { return 1; },
total: function (obj) { return 1; },
records: function (obj) { return obj.length; }
}

此处的 obj 是从服务端服务器的response。

[b] Array Data[/b]
相关的配置:datatype
colModel的相关选项: sorttype, datefmt
相关的方法:getRowData, delRowData, setRowData, addRowData, updateGridRows

localReader = {
root: "rows",
page: "page",
total: "total",
records: "records",
repeatitems: false,
cell: "cell",
id: "id",
userdata: "userdata",
subgrid: {root:"rows", repeatitems: true, cell:"cell"}
}


js

...
<script>
jQuery(document).ready(function(){
jQuery("#list").jqGrid({
datatype: 'clientSide',
colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
colModel :[
{name:'invid',index:'invid', width:55, sorttype:'int'},
{name:'invdate',index:'invdate', width:90, sorttype:'date', datefmt:'Y-m-d'},
{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,
viewrecords: true,
caption: 'My first grid'
});
...
});
</script>


语法:
1. jQuery("#grid_id").addRowData( rowid, data, position, srcrowid );
position: 增加数据的位置:first, last, before or after. 1) first:增加在最前面,2)last:增加在最后面,3) before”,“after” srcrowid参数的前面或后面。
srcrowid: 和position中的“before”,“after”配合使用。

<script>
...
var myfirstrow = {invid:"1", invdate:"2007-10-01", note:"note", amount:"200.00", tax:"10.00", total:"210.00"};
jQuery("#list").addRowData("1", myfirstrow);
...
</script>


2. jQuery("#grid_id").getRowData( rowid );

jQuery("#list").getRowData( "1" );

返回的数据:
{invid:"1", invdate:"2007-10-01", note:"note", amount:"200.00", tax:"10.00", total:"210.00"};


3. jQuery("#grid_id").delRowData( rowid );

jQuery("#list").delRowData( "2" );


4. jQuery("#grid_id").setRowData( rowid, data );

jQuery("#list").setRowData( "1", { tax:"5", total:"205" });


[b]Function[/b]

<script type="text/javascript">
...
jQuery(document).ready(function(){
jQuery("#list").jqGrid({
datatype: function(postdata) {
jQuery.ajax({
url: 'example.php',
data:postdata,
dataType:"xml",
complete: function(xmldata,stat){
if(stat=="success") {
var thegrid = jQuery("#list")[0];
thegrid.addXmlData(xmldata.responseXML);
}
}
});
},
colNames:['Inv No','Date', 'Amount','Tax','Total','Notes'],
colModel :[
{name:'invid', index:'invid', width:55},
{name:'invdate', index:'invdate', width:90},
{name:'amount', index:'amount', width:80, align:'right'},
{name:'tax', index:'tax', width:80, align:'right'},
{name:'total', index:'total', width:80, align:'right'},
{name:'note', index:'note', width:150, sortable:false}
],
pager: '#pager',
rowNum:10,
rowList:[10,20,30],
sortname: 'invid',
sortorder: 'desc',
viewrecords: true,
caption: 'My first grid'
});
});
...
</script>


[b]User Data[/b]

jsonReader: {
...
userdata: "userdata",
...
}


xml

<invoices>
<request>true</request>
<userdata name="totalinvoice"> 240.00 </userdata> // userdata
<userdata name="tax"> 40.00</userdata> // userdata
...
<result>
<row>
<cell>data1</cell>
<cell>data2</cell>
<cell>data3</cell>
<cell>data4</cell>
<cell>data5</cell>
<cell>data6</cell>
</row>
...
</result>
</invoices>


json

{
total: "xxx",
page: "yyy",
records: "zzz",
userdata: {totalinvoice:240.00, tax:40.00}, // userdata
rows : [
{id:"1", cell:["cell11", "cell12", "cell13"]},
{id:"2", cell:["cell21", "cell22", "cell23"]},
...
]
}


获取userdata的方法:
jQuery("grid_id").jqGrid('getGridParam', 'userData')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值