jqGrid datatype配置为function
http://www.w3dev.cn/article/20130701/jqGrid-datatype-setto-function.aspx
设置为function实际并不是定义数据类型,而是如何处理从服务器返回的数据(可能未xml或者json)。 配置的方法需要(或者可以)调用addXMLData, addJSONData或者addRowData方法当接收到数据时。如果需要分页,可以在此调用 jqGrid实例名称setGridParam({lastpage: your_number}) 定义页数。
示例
-收缩
JavaScript
代码datatype : function(postdata) {
// do something here
}
// do something here
}
配置的方法有一个唯一参数,包含get/post到服务器的数据(JSON对象,为jqGrid配置中的postData配置内容),postData对象和jQuery ajax方法中的data配置一样
下面为一个datatype设置为function的详细示例
-收缩
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'
});
});
//...
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'
});
});
//...