本節介紹jqGrid其他的使用方法,主要是一些基本操作,特殊的數據顯示等。
1 刷新jqGrid數據。
$("#search_btn").click(function(){
//此處可以添加對查詢數據的合法驗證
var orderId = $("#orderId").val();
$("#list4").jqGrid('setGridParam',{
datatype:'json',
postData:{'orderId':orderId}, //發送數據
page:1
}).trigger("reloadGrid"); //重新載入
});
① setGridParam用於設置jqGrid的options選項。返回jqGrid對象
② datatype為指定發送數據的格式;
③ postData為發送請求的數據,以key:value的形式發送,多個參數可以以逗號”,”間隔;
④ page為指定查詢結果跳轉到第一頁;
⑤ trigger(“reloadGrid”);為重新載入jqGrid表格。
2 無數據的提示信息。
loadComplete: function() {//如果數據不存在,提示信息 var rowNum = $("#list4").jqGrid('getGridParam','records'); if (rowNum if($("#norecords").html() == null){ $("#list4").parent().append("</pre> <div id="norecords">沒有查詢記錄!</div> <pre>"); } $("#norecords").show(); }else{//如果存在記錄,則隱藏提示信息。 $("#norecords").hide(); } }
① loadComplete 為jqGrid加載完成,執行的方法;
② getGridParam這個方法用來獲得jqGrid的選項值。它具有一個可選參數name,name即代表着jqGrid的選項名,如果不傳入name參數,則會返回jqGrid整個選項options。例:
$("#list4").jqGrid('getGridParam','records');//獲取當前jqGrid的總記錄數;
3 顯示jqGrid統計信息。
代碼片段:
$("#list4").jqGrid({
......
colModel:[
{name:'productName',index:'productName',align:'center',sortable:false},
{name:'productAmt',index:'productAmt', align:'center'}
],
footerrow: true,//分頁上添加一行,用於顯示統計信息
......
pager:$('#gridPager'),
gridComplete: function() {//當表格所有數據都加載完成,處理統計行數據
var rowNum = $(this).jqGrid('getGridParam','records');
if(rowNum > 0){
var options = {
url: "test.action",// 默認是form的action,如果寫的話,會覆蓋from的action.
dataType: "json",// 'xml', 'script', or 'json' (接受服務端返回的類型.)
type: "POST",
success: function(data){//成功后調用方法
$("#list4").footerData("set",{productName:"合計",productAmt:data.productAmtSum});
}
};
$("#searchForm").ajaxSubmit(options);
}
}
});
詳細介紹:
3.1jqGrid的options配置; 需要在jqGrid的options中添加如下屬性:
footerrow: true,//分頁上添加一行,用於顯示統計信息
3.2 調用gridComplete方法,當數據加載完成后,處理統計行數據; 3.3調用jqGrid的footerData方法,為統計行賦值:
$("#list4").footerData("set",{productName:"合計",productAmt:data.productAmtSum});
4 jqGrid的數據格式化。
jQuery("#jqGrid_id").jqGrid({
...
colModel: [
...
{name:'price', index:'price', formatter:'integer', formatoptions:{thousandsSeparator: ','}},
...
]
...
});
colModel:[ {name:'id', index:'id', formatter: customFmatter}, {name:'name', index:'name', formatter: "showlink", formatoptions:{baseLinkUrl:"save.action",idName: "id", addParam:"&name=123"}}, {name:'price', index:'price', formatter: "currency", formatoptions: {thousandsSeparator:",",decimalSeparator:".", prefix:"$"}}, {name:'email', index:'email', formatter: "email"}, {name:'amount', index:'amount', formatter: "number", formatoptions: {thousandsSeparator:",", defaulValue:"",decimalPlaces:3}}, {name:'gender', index:'gender', formatter: "checkbox",formatoptions:{disabled:false}}, {name:'type', index:'type', formatter: "select", editoptions:{value:"0:無效;1:正常;2:未知"}} ]
其中customFmatter聲明如下:
function customFmatter(cellvalue, options, rowObject){ console.log(cellvalue); console.log(options); console.log(rowObject); return "["+cellvalue+"]"; };
function customFmatter(cellvalue, options, rowObject){ } //cellvalue - 當前cell的值 //options - 該cell的options設置,包括{rowId, colModel,pos,gid} //rowObject - 當前cell所在row的值,如{ id=1, name="name1", price=123.1, ...}
當然對於自定義formatter,在修改時需要獲取原來的值,這里就提供了unformat函數,這里見官網的例子:
jQuery("#grid_id").jqGrid({
...
colModel: [
...
{name:'price', index:'price', width:60, align:"center", editable: true, formatter:imageFormat, unformat:imageUnFormat},
...
]
...
});
function imageFormat( cellvalue, options, rowObject ){
return '</pre>
<img src="'+cellvalue+'" alt="" />
<pre>';
}
function imageUnFormat( cellvalue, options, cell){
return $('img', cell).attr('src');
}
5 常見錯誤問題:
chrome報錯:
Uncaught TypeError: Cannot read property ‘integer’ of undefined
IE報錯:
SCRIPT5007: 無法獲取屬性“integer”的值: 對象為 null 或未定義
出現這樣的問題,是由於頁面沒有添加語言文件的引用導致的
解決辦法為:添加語言文件js
<script type="text/javascript" src="js/i18n/grid.locale-cn.js"></script>



