我遇到了使用触发器('reloadGrid')重新加载网格的问题。 我向服务器发送一个AJAX调用,服务器返回xmlstring就好了。但网格不会加载新数据。 继承我的代码:
$('#tasks').jqGrid({
datatype: "xmlstring",
datastr: <?php echo json_encode($_xml); ?>,
colNames: ["Date","TaskID","Subject","Notes","Due Date"],
colModel: [
{name: "Date", index:"AssignDate",align: "center", xmlmap:"AssignDate"},
{name: "TaskID", index:"TaskID",xmlmap:"TaskID", align:"center"},
{name: "Subject", index:"TaskSubject", align: "center", xmlmap:"TaskSubject"},
{name:"Notes", index:"Notes", align: "center",height: 20,xmlmap:"Notes"},
{name:"Due Date", index:"DueDate", align: "center",height: 20,xmlmap:"DueDate"}
],
height: 250,
viewRecords: true,
autowidth: true,
xmlReader: {
root: "tasks",
row: "task",
repeatitems: false
},
pager: $('#navTasks'),
caption: "Your Tasks"});
setInterval(
function(){
$.ajax({
url: 'register.php',
data: {uID:uID},
dataType: 'xmlstring',
success: function(xml)
{
$("#tasks").setGridParam({datastr: xml, datatype: "xmlstring"}).trigger('reloadGrid')
}
});},10000);
这是我的register.php文件:
$uID = $_GET['uID'];
$host="127.0.0.1:3306";
$db_name='wf_db';
$tbl_name="tasks";
$connect = new mysqli("$host","root"," ","$db_name")or die('Can\'t connect to database!');
$sql = "SELECT * FROM $tbl_name WHERE UserID='$uID' and Task_Completed='0'";
$query_data = mysqli_query($connect,$sql)or die('Couldnt get data');
$row = mysqli_num_rows($query_data);
if($row != 0){
$_xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\r\n";
$_xml .="<tasks>\r\n";
while ($result = mysqli_fetch_array($query_data)) {
if ($result["TaskID"]) {
$_xml .="\t<task>\r\n";
$_xml .="\t\t<TaskID>" . $result["TaskID"] . "</TaskID>\r\n";
$_xml .="\t\t<UserID>" . $result["UserID"] . "</UserID>\r\n";
$_xml .="\t\t<AssignID>" . $result["AssignID"] . "</AssignID>\r\n";
$_xml .="\t\t<AssignDate>" . $result["Assign_Date"] . "</AssignDate>\r\n";
$_xml .="\t\t<DueDate>" . $result["Due_Date"] . "</DueDate>\r\n";
$_xml .="\t\t<AssignUser>" . $result["Assign_User"] . "</AssignUser>\r\n";
$_xml .="\t\t<TaskSubject>" . $result["Task_Subject"] . "</TaskSubject>\r\n";
$_xml .="\t\t<Notes>" . $result["Notes"] . "</Notes>\r\n";
$_xml .="\t\t<TrackID>" . $result["TrackID"] . "</TrackID>\r\n";
$_xml .="\t\t<Details>" . $result["Completion_Details"] . "</Details>\r\n";
$_xml .="\t</task>\r\n";
} else {
$_xml .="\t<task>\r\n";
$_xml .="\t\t<TaskID></TaskID>\r\n";
$_xml .="\t\t<UserID></UserID>\r\n";
$_xml .="\t\t<AssignID></AssignID>\r\n";
$_xml .="\t\t<AssignDate></AssignDate>\r\n";
$_xml .="\t\t<DueDate></DueDate>\r\n";
$_xml .="\t\t<AssignUser></AssignUser>\r\n";
$_xml .="\t\t<TaskSubject></TaskSubject>\r\n";
$_xml .="\t\t<Notes></Notes>\r\n";
$_xml .="\t\t<TrackID></TrackID>\r\n";
$_xml .="\t\t<Details></Details>\r\n";
$_xml .="\t</task>\r\n";
}
}
$_xml .="</tasks>";
echo json_encode($_xml);
}
else {
echo 'Oops something went wrong!';
}
更新:
var data = <?php echo json_encode($_xml); ?>;
var uID = <?php echo $uID; ?>;$grid.jqGrid({
datatype: "xmlstring",
datastr: data, url: 'register.php',
sortname: 'Job ID',
sortOrder: 'asc',
postData: {uID:uID},
colNames: ["track","Job ID","Subject","Notes","Details","Due Date"],
colModel: [
{name: "track", index:"TrackID",align: "center", xmlmap:"TrackID", hidden: true, sortable: true},
{name: "Job ID", index:"TaskID",xmlmap:"TaskID", align:"center",sortable: true, sorttype: 'text', sortable: true, editable: false, editoptions: {readonly: true}},
{name: "Subject", index:"TaskSubject", align: "center", xmlmap:"TaskSubject",sorttype: 'text', sortable: true,editable: false, editoptions: {readonly: true}},
{name:"Notes", index:"Notes", align: "center",height: 20,xmlmap:"Notes",sorttype: 'text', sortable: true,editable: false, editoptions: {readonly: true}},
{name:"Details", index:"Details", align: "center", xmlmap:"Details", editable: true, edittype: 'select', editoptions: {value: 'Not Started:Not Started;In Progress:In Progress;Completed:Completed'}, sortable: true},
{name:"Due Date", index:"DueDate", align: "center", xmlmap:"DueDate",sorttype: 'text', sortable: true, editable: true, editoptions: {dataInit: initDateEdit}, formatter: 'date', formatoptions: {newformat: 'd-M-Y'}, datefmt: 'd-M-Y'}
],
editurl: 'editRow.php',
rowNum: 10,
rowList: [10,20,50],
viewRecords: true,
xmlReader: {
root: "tasks",
row: "task",
repeatitems: false
},
pager: $('#navTasks'),
caption: "Your Tasks"
}).navGrid('#navTasks',{<?php
if ($user==NULL){
echo 'edit:false,add:false,del:false';
}
else{
echo 'edit:false,add:false,del:false';
}
?>},{},{},{},{multipleSearch: false, multipleGroup: false});
setInterval(
function(){
$grid.trigger('reloadGrid',[{current:true}]);},6000);
答案 0 :(得分:0)
首先,您使用的$.ajax不知道dataType: 'xmlstring'
。您的意思是dataType: 'xml'
。
第二,你误解了datatype: 'xmlstring'
。您可以使用
$.ajax
电话
datatype: 'xml',
url: 'register.php',
postData: {uID: uID},
jqGrid的选项。您可以考虑将uID
的{{1}}属性视为方法(有关详细信息,请参阅here)。 postData
的正文可以简化为setInterval
的调用(请参阅here和here)。
如果您不需要首先加载网格主体并仅在$("#tasks").trigger('reloadGrid', [{current: true}]);
内部加载,则可以设置setInterval
并将datatype: 'local'
更改为datatype
像你一样'xml'
。
此外我自己不使用PHP,但我建议您使用PHP DOM中的setGridParam
(有关详细信息,请参阅here或here)手动编写XML。直接构造XML文本会产生许多错误。例如,您不能将DOMDocument
之类的字符直接作为XML元素的内容而不进行编码。至少应使用CDATA个部分。