我想使用filtertoolbar,但使用接受不同過濾器參數的服務器。jqGrid postData.filter返回undefined
我試圖找到過濾器參數爲了操縱postData。
上beforeSearch,我試圖提取使用過濾器:
var postData = $("#jqGrid").jqGrid('getGridParam','postData');
alert(postData.filters);
未定義所有的時間postData.filter回報。
再次要注意的是,服務器沒有它的json請求方案中的任何「過濾器」概念,所以json請求由我填充。
這裏的功能:{
var pageOffset = (sessionPage - 1) * pageLength;
var totalList = 0;
var pageNumber = 0;
var totalPages = 0;
$("#jqGrid").jqGrid({
url: 'https://vlnx277:7000/vmgr/vapi/rest/sessions/list',
mtype: "POST",
postData: sessionListData(pageOffset),
datatype: "json",
onPaging: function(pgButton) {
var requestedPage = $(".ui-pg-input").val();
if (pgButton == "user") {
// find out the requested and last page
var lastPage = $("#jqGrid").getGridParam("lastpage");
// if the requested page is higher than the last page value
if (requestedPage > lastPage) {
// set the requested page to the last page value - then reload
$("#jqGrid").setGridParam({page:lastPage});
sessionPage = lastPage;
} else {
sessionPage = $(".ui-pg-input").val();
}
} else if (pgButton == "next_jqGridPager") {
sessionPage = +$(".ui-pg-input").val() + +1;
} else if (pgButton == "prev_jqGridPager") {
sessionPage = $(".ui-pg-input").val() - 1;
} else if (pgButton == "last_jqGridPager") {
sessionPage = $("#jqGrid").getGridParam("lastpage");
} else if (pgButton == "first_jqGridPager") {
sessionPage = 1;
} else if (pgButton == "records"){
sessionPage = 1;
pageLength = $(".ui-pg-selbox", this.p.pager).val();
}
pageOffset = (sessionPage - 1) * pageLength;
sessionList(pageOffset);
},
onSortCol: function (index, iCol, sortorder){
//alert (index + " " + sortorder);
var field_name = "id";
var sort_direction = "ASCENDING";
var sortDirectionFields = [];
//In case no more sorting:
if (!index){
sortingAddion = ',"sortSpec":[{"attName":"id","direction":"ASCENDING"}]';
} else {
var indexPlusSort = index.trim() + " " + sortorder.trim();
var multiSortVaribles = indexPlusSort.split(",") // Delimiter is a string
for (var i = 0; i < multiSortVaribles.length; i++)
{
var fieldDirectionPair = multiSortVaribles[i].trim().split(" ");
if (fieldDirectionPair[0]){
var attrDirection = {};
field_name = fieldDirectionPair[0].trim();
if (fieldDirectionPair[1] == 'desc') {
sort_direction = "DESCENDING";
} else {
sort_direction = "ASCENDING";
}
attrDirection["attName"] = field_name;
attrDirection["direction"] = sort_direction;
}
sortDirectionFields.push(attrDirection);
}
sortingAddion = ',"sortSpec":' + JSON.stringify(sortDirectionFields) ;
}
sessionList(pageOffset);
},
beforeProcessing: function(res, status, xhr) {
pageOffset = xhr.getResponseHeader("X-list-paging-offset");
//pageLength = xhr.getResponseHeader("X-list-paging-length");
totalList = xhr.getResponseHeader("X-list-size");
pageNumber = pageOffset/pageLength + 1;
//alert(pageOffset + " " + pageLength + " " + pageNumber);
if (totalList % pageLength == 0){
totalPages = Math.floor(totalList/pageLength);
} else {
totalPages = Math.floor(totalList/pageLength) + 1;
}
},
/*
gridComplete: function(data) {
var postData = $("#jqGrid").jqGrid('getGridParam','postData');
var filters = '';
postData.filters = {};
//alert ("In grid complete " + $("#jqGrid").jqGrid('getGridParam','postData'));
//alert(JSON.stringify(postData));
//$("#jqGrid").jqGrid('setGridParam',{postData: '{"filters":{}}' });
alert ("In grid complete " + $("#jqGrid").jqGrid('getGridParam','postData'));
},
*/
loadBeforeSend: function(xhr)
{
xhr.setRequestHeader("Authorization","Basic " + btoa("xxxxx" + ":" + "xxxxx"));
return xhr;
},
crossDomain: true,
ajaxGridOptions: { contentType: "application/json; charset=utf-8", dataType: "json", type: "POST"},
headertitles: true,
colModel: [
{ label: 'ID', name: 'id', key: true, width: 75 },
<!--{ label: 'Session Name', name: 'session_name', width: 150, editable: true },-->
{ label: 'Start Time', name: 'start_time', width: 150, editable: true },
{ label: 'End Time', name: 'end_time', width: 150, editable: true },
{ label: 'Owner', name: 'owner', width: 150, editable: true },
{ label:'Session Source Type', name: 'session_source_type', width: 150, editable: true },
{ label:'Session Status', name: 'session_status', width: 150, editable: true }
],
viewrecords: true,
jsonReader: {
page: function (obj) { return pageNumber; },
total: function (obj) { return totalPages; },
records: function (obj) { return totalList; }
},
loadError : function(xhr,st,err) {
alert("Type: "+st+"; Response: " + xhr.status + " " +xhr.statusText);
},
autowidth: true,
height: 500,
shrinkToFit: true,
rowNum: 50,
rowList:[10,20,30,50,100,200],
shrinkToFit: true,
altRows: true,
rownumbers: false, // show row numbers
rownumWidth: 25, // the width of the row numbers columns
//multiselect: true,
multiSort: true,
hoverrows: true, // true by default, can be switched to false if highlight on hover is not needed
caption: "vManager Sessions View Grid", // set caption to any string you wish and it will appear on top of the grid
pager: "#jqGridPager"
});
$("#jqGrid").jqGrid('bindKeys');
//$('#jqGrid').jqGrid('filterToolbar',{beforeSearch:beforeSearch});
$("#jqGrid").jqGrid('filterToolbar', {stringResult: true, searchOnEnter: true, defaultSearch : "cn", beforeSearch: function() {
var postData = $("#jqGrid").jqGrid('getGridParam','postData');
var newPostData = '1=1';
//alert(postData);
alert(postData.filters);
//var searchData = jQuery.parseJSON(postData.filters);
//alert(searchData);
/*
for (var iRule=0; iRule<searchData.rules.length; iRule++) {
newPostData = newPostData + " AND " + searchData.rules[iRule].field + " LIKE '%" + searchData.rules[iRule].data + "%' ";
}
$("#jqGrid").jqGrid('setGridParam',{postData: { filter: newPostData, filters: ''} });
*/
return false;
}});
$('#jqGrid').navGrid("#jqGridPager", {edit: false, add: false, del: false, refresh: false, view: false});
$('#jqGrid').inlineNav('#jqGridPager',
// the buttons to appear on the toolbar of the grid
{
edit: true,
add: false,
del: true,
cancel: true,
editParams: {
keys: true,
},
addParams: {
keys: true
}
});
});
function sessionList(pageOffset){
//alert(sessionListData(pageOffset));
$("#jqGrid").setGridParam({ postData:sessionListData(pageOffset) });
}
function sessionListData(pageOffset){
//alert('{"pageOffset":' + pageOffset + ',"pageLength":' + pageLength + ',"projection": {"type": "SELECTION_ONLY","selection": ["id","start_time","end_time","owner","session_source_type","session_status"] }' + sortingAddion + '}');
return '{"pageOffset":' + pageOffset + ',"pageLength":' + pageLength + ',"projection": {"type": "SELECTION_ONLY","selection": ["id","start_time","end_time","owner","session_source_type","session_status"] }' + sortingAddion + '}';
}
function beforeSearch(){
alert("In search");
alert($("#jqGrid").getGridParam("postData").filters);
}
感謝,對MTYPE: 「POST」, \t \t \t \t數據類型: 「JSON」,並stringResult設置爲true。 。 – Tal 2015-02-23 12:18:44
$( 「#的jqGrid」)的jqGrid( 'filterToolbar',{stringResult:真,searchOnEnter:真,defaultSearch: 「CN」,beforeSearch:函數(){ \t \t \t \t VAR POSTDATA = $( 「#jqGrid的」 ).jqGrid( 'getGridParam', 'POSTDATA'); \t \t \t \t變種newPostData = '1 = 1'; \t \t \t \t //警報(POSTDATA); \t \t \t \t警報(postData.filters) ; – Tal 2015-02-23 12:21:29