2012-01-26 126 views
0

我遇到了用觸發器('reloadGrid')重新加載網格的問題。 我發送一個AJAX調用到服務器,服務器返回xmlstring就好了。但網格不會加載新數據。 繼承人我的代碼:這裏jqGrid重新加載不起作用

$('#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!'; 
} 

UPDATE:

你使用 $.ajax不知道 dataType: 'xmlstring'
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

第一。你的意思是dataType: 'xml'

二你誤會了datatype: 'xmlstring'。相反,單獨$.ajax電話的使用,你可以使用的jqGrid的

datatype: 'xml', 
url: 'register.php', 
postData: {uID: uID}, 

選項。您可以考慮實施postDatauID屬性作爲方法(詳情請參見here)。 setInterval的主體可以縮減爲$("#tasks").trigger('reloadGrid', [{current: true}]);(請參閱herehere)。

如果您需要在第一次不加載柵格體,做只加載的setInterval裏面可以設置datatype: 'local'和更改datatype'xml'方面的setGridParam像你一樣了。

而且我不使用PHP自己,但無論如何我會建議你使用DOMDocumentPHP DOM(見例如herehere瞭解詳細信息),而不是手動編寫XML。直接構建XML文本會產生很多錯誤。例如,您不能直接將<等字符作爲XML元素的內容,而無需編碼。至少應該使用CDATA部分。

+0

很好,非常感謝所有的信息,但在遵循大部分的內容之後(除了PHP到XML)......我仍然堅持原始問題,網格不會每隔x秒刷新一次新記錄。如果您需要,我會發布代碼。 – stackErr

+0

@ user1160022:不客氣!爲了能夠幫助您本地化問題,我當然需要您使用的當前代碼。此外,如果知道你在哪裏定義(或分配)「uID」可能很重要。如果發佈將由服務器生成的XML響應,那麼這會很好。 – Oleg

+0

對不起,遲到的迴應,我幾天沒有互聯網連接。我已經更新了我原來的問題,看看並讓我知道我哪裏出錯了。謝謝! 我也在網格中增加了一些功能。 – stackErr