2013-05-01 34 views
2

我正在使用jQuery的頁面上工作,並且計劃將jqGrid併入到頁面中。該頁面將有一個提交按鈕,將一行寫入表中,並且當發生onReadyStateChange時,它將啓動一個回調函數。在該函數中,jqGrid將被重新加載/替換。當前的代碼如下:jQuery-jqGrid - 使用responseXML和or responseText

var myReq = new XMLHttpRequest(); 
var myURL = myServer + myOtherInfo; 

..... (other parameters are added to the myURL variable) 

myReq.open("GET",myURL, true); // true=asynchronous, false=synchronous 
myReq.onreadystatechange = mycallback; 
myReq.send(null); 


function mycallback() { 
var data = myReq.responseText; 
var xdata = myReq.responseXML; 

由於數據集將是非常小的,我們已選擇簡單地重新創建/替換頁面上的網格。我知道數據傳回到上面的兩個變量(數據& xdata)。截至目前,我只在ResponseText & ResponseXML(可變數量的行)中傳回一個字段。最終,這將是3-5場。

如何讓jqGrid使用data/myReq.responseTextxdata/myReq.responseXML變量/對象中已有的內容?

我以爲你會使用datastr & datatypexmlstring但這不像我想象的那樣工作。部分jqGrid如下所示。這也包含在mycallback function中。

$("#myGrid").jqGrid({ 
xmlReader: { 
datastr: xdata, 
datatype: "xmlstring", 
root: "Row", 
row: "ContactName", 
colNames: ["Contact Name"], 
colModel: [{name:"ContactName",index:"ContactName",width:200,align:"right"}], 
viewrecords: true, 
caption: "My Grid" 
} 
}); 

我很新的這兩個jQuery的& jqGrid的,並希望任何幫助或方向。

編輯

下面是我目前使用(從Northwind數據庫)中的數據的樣本。

<?xml version="1.0" encoding="UTF-8" ?> 
<Rowsets DateCreated="2013-05-02T09:18:07" EndDate="2013-05-02T09:18:07" StartDate="2013-05-02T08:18:07" Version="12.0.6 Build(13)"> 
<Rowset> 
<Columns> 
<Column Description="ContactName" MaxRange="1" MinRange="0" Name="ContactName" SQLDataType="12" SourceColumn="ContactName" /> 
<Column Description="City" MaxRange="1" MinRange="0" Name="City" SQLDataType="12" SourceColumn="City" /> 
<Column Description="Country" MaxRange="1" MinRange="0" Name="Country" SQLDataType="12" SourceColumn="Country" /> 
</Columns> 
<Row> 
<ContactName>Maria Anders</ContactName> 
<City>Berlin</City> 
<Country>Germany</Country> 
</Row> 
<Row> 
<ContactName>Ana Trujillo</ContactName> 
<City>México D.F.</City> 
<Country>Mexico</Country> 
</Row> 
<Row> 
<ContactName>Antonio Moreno</ContactName> 
<City>México D.F.</City> 
<Country>Mexico</Country> 
</Row> 
<Row> 
<ContactName>Thomas Hardy</ContactName> 
<City>London</City> 
<Country>UK</Country> 
</Row> 
<Row> 
<ContactName>Christina Berglund</ContactName> 
<City>Luleå</City> 
<Country>Sweden</Country> 
</Row> 
<Row> 
<ContactName>Hanna Moos</ContactName> 
<City>Mannheim</City> 
<Country>Germany</Country> 
</Row> 
</Rowset> 
</Rowsets> 

由於我原來的文章,我已經得到的數據出現在網格上,現在正在嘗試格式化它。

最終,我想在網格的每一行添加一個提交按鈕,這將允許用戶選擇一行,然後單擊提交按鈕重新添加該行到表(當完成後,我將日期時間戳用作列之一)。

最初,我一直在使用XMLHttpRequest來運行查詢&接收XML回來,並使用onreadystatechange來啓動一個回調函數來加載和顯示網格。

+0

使用*分開的* Ajax請求到服務器和使用帶'datatype:「xmlstring」'的jqGrid是壞方法。使用'datatype:「xml」'好得多,並允許jqGrid進行調用。您可以使用'xmlReader'來通知jqGrid *它可以從服務器返回的xml響應中讀取所需的信息。如果你要包含返回服務器的XML數據,我可以用'xmlReader'來幫助你。我不明白你的問題的第一部分(about和'responseText'&'responseXML')。 「創建」jqGrid的代碼是錯誤的,因爲您在'xmlReader'選項中插入了所有選項 – Oleg 2013-05-01 20:16:14

+0

由於我已經對數據進行了文本和XML響應,因此responseText&responseXML部分想要使用其中的一個。我想使用jqGrid從他們簡單地構造數據網格。我已經想出了一些「創建」的問題 - 我在看一個省略了xmlReader的示例。 – 2013-05-01 20:47:43

+0

您必須包含*返回服務器的XML數據*的示例。只有在這之後你才能說哪個'xmlReader'應該被使用。關於'responseXML'的用法我只能重複一遍,我發現不需要單獨使用'XMLHttpRequest'的代碼。在這個例子中,'myURL'的完整響應包含jqGrid的數據。您可以使用'url:myURL,datatype:「xml」,loadonce:true'來代替代碼。 jqGrid的其他選項(尤其是'xmlReader')取決於從服務器返回的XML響應的結構。 **你能否只用XML數據附加你的問題?** – Oleg 2013-05-01 20:57:12

回答

4

如果你有myURL這在你在你的問題包括,那麼你可以使用下面的代碼的形式,每Ajax的XML數據提供:

$("#myGrid").jqGrid({ 
    url: "steve_o.xml", 
    xmlReader: { 
     repeatitems: false, 
     root: "Rowsets>Rowset", 
     row: "Row" 
    }, 
    colNames: ["Contact Name", "City", "Country"], 
    colModel: [ 
     { name: "ContactName" }, 
     { name: "City" }, 
     { name: "Country" } 
    ], 
    rowNum: 10000, // no local paring of data 
    gridview: true, 
    viewrecords: true, 
    height: "auto", 
    loadonce: true 
}); 

相應demo顯示

enter image description here

您可以非常方便地使用本地分頁的數據,只需修改代碼即可

$("#myGrid").jqGrid({ 
    url: "steve_o.xml", 
    xmlReader: { 
     repeatitems: false, 
     root: "Rowsets>Rowset", 
     row: "Row" 
    }, 
    colNames: ["Contact Name", "City", "Country"], 
    colModel: [ 
     { name: "ContactName" }, 
     { name: "City" }, 
     { name: "Country" } 
    ], 
    rowNum: 5, 
    rowList: [5, 10, 20, 100, 10000], 
    pager: "#pager", 
    gridview: true, 
    rownumbers: true, 
    viewrecords: true, 
    height: "auto", 
    loadonce: true 
}); 

The corresponding demo有與一些按鈕尋呼機和一個可以使用它的分頁:

enter image description here

人們可以在the demo很容易本地過濾和搜索功能添加到網格等。

你最後的要求是關於每行中的一些按鈕看起來非常接近formatter: "actions"。對於一些代碼示例,您可以看看the answer