這與我之前關於jqgrid的問題有關。即時做一個搜索按鈕,將搜索我的服務器輸入的文本,並顯示這些數據(如果有)在jqgrid。現在,我所做的是創建一個存儲過濾器的全局變量。下面是我的javascript代碼爲我的搜索和顯示:如何在jgrid上顯示搜索到的數據
filter = ''; //this is my global variable for storing filters
$('#btnsearchCode').click(function(){
var row_data = '';
var par = {
"SessionID": $.cookie("ID"),
"dataType": "data",
"filters":[{
"name":"code",
"comparison":"starts_with",
"value":$('#searchCode').val(),
}],
"recordLimit":50,
"recordOffset":0,
"rowDataAsObjects":false,
"queryRowCount":true,
"sort_descending_fields":"main_account_group_desc"
}
filter="[{'name':'main_account_group_code','comparison':'starts_with','value':$('#searchCode').val()}]";
$('#list1').setGridParam({
url:'json.php?path=' + encodeURI('data/view') + '&json=' + encodeURI(JSON.stringify(par)),
datatype: Settings.ajaxDataType,
});
$('#list1').trigger('reloadGrid');
$.ajax({
type: 'GET',
url: 'json.php?' + $.param({path:'data/view',json:JSON.stringify(par)}),
dataType: Settings.ajaxDataType,
success: function(data) {
if ('error' in data){
showMessage('ERROR: ' + data["error"]["msg"]);
}
else{
if ((JSON.stringify(data.result.main.row)) <= 0){
alert('code not found');
}
else{
var root=[];
$.each(data['result']['main']['rowdata'], function(rowIndex, rowDataValue) {
var row = {};
$.each(rowDataValue, function(columnIndex, rowArrayValue) {
var fldName = data['result']['main']['metadata']['fields'][columnIndex].name;
row[fldName] = rowArrayValue;
});
root[rowIndex] = row;
row_data += JSON.stringify(root[rowIndex]) + '\r\n';
});
}
alert(row_data); //this alerts all the data that starts with the inputed text...
}
}
});
}
我觀察到的代碼總是進入這個(我計劃的代碼與我的其他表使用),所以我在這裏把過濾器:
$.extend(jQuery.jgrid.defaults, {
datatype: 'json',
serializeGridData: function(postData) {
var jsonParams = {
'SessionID': $.cookie("ID"),
'dataType': 'data',
'filters': filter,
'recordLimit': postData.rows,
'recordOffset': postData.rows * (postData.page - 1),
'rowDataAsObjects': false,
'queryRowCount': true,
'sort_fields': postData.sidx
};
return 'json=' + JSON.stringify(jsonParams);
},
loadError: function(xhr, msg, e) {
showMessage('HTTP error: ' + JSON.stringify(msg) + '.');
},
});
現在,我的問題是,爲什麼它顯示錯誤消息「服務器錯誤:參數'dataType'沒有指定」?我已經在上面的代碼中聲明瞭dataType,但似乎沒有讀取它。有沒有人可以在這裏幫助我如何在網格上顯示搜索到的數據?(功能是一個很好的幫助)
重要的是要知道你是否使用HTTP POST方式或GET服務器請求?在[一箇舊的答案](http://stackoverflow.com/questions/5374977/is-it-possible-to-include-an-event-in-a-javascript-function/5375479#5375479)我建議你使用jqGrid的'postData'參數。你用它嗎?你可以包含你使用的jqGrid的當前代碼嗎?你目前的'serializeGridData'實現**忽略**許多jqGrid的標準參數和** ovewrite **它與yourth。你能描述一下你想在服務器上使用哪種形式的參數嗎? – Oleg 2011-04-25 09:24:52
在我看來,存在一個誤解,jqGrid和'jQuery.ajax'如何構建用於服務器請求的URL(postData將如何用附加參數追加URL)。如果需要的話,我可以解釋一切。你可以另外爲什麼需要一些奇怪的** static **參數''dataType「:」data「''''sort_fields」:「main_account_group_desc」'等等。 'Settings.ajaxDataType'有什麼值,爲什麼值不是靜態值'json'或'xml'? – Oleg 2011-04-25 09:44:17
奧列格,請參閱我的編輯...我不使用postdata像你以前的答案。那是我使用的所有代碼。如果我提醒row_data,它會顯示很好的答案。但網格顯示所有數據。我真的無法解釋'Settings.ajaxDataType'的值有什麼,因爲我自己不知道。我的老大隻告訴我使用它。 – jayAnn 2011-04-26 02:30:31