我已经能够从外部 XML 文件创建 ExtJS 网格。我通常定义商店如下:
var store = new Ext.data.Store({
model: 'PersonModel',
proxy: {
type: 'ajax',
url : 'MyXML.xml',
reader: {
type : 'xml',
record: 'Person'
}
}
});
我有一个 XML 片段存储在其中一个字段中,所以假设我将它存储在局部变量中。如何实现存储从变量/字符串读取而不是从文件读取?谢谢。
最佳答案
我让它工作了。诀窍是定义一个内存类型代理,然后将解析后的 xml 对象传递给它。
var xmlstr = "<personRecord>" +
"<Person>" +
"<firstName>Stan</firstName>" +
"<lastName>C</lastName>" +
"</Person>" +
"<Person>" +
"<firstName>Stanley</firstName>" +
"<lastName>D</lastName>" +
"</Person>" +
"</personRecord>";
var doc;
if (window.ActiveXObject) { //IE
var doc = new ActiveXObject("Microsoft.XMLDOM");
doc.async = "false";
doc.loadXML(xmlstr);
} else { //Mozilla
var doc = new DOMParser().parseFromString(xmlstr, "text/xml");
}
var memstore = new Ext.data.Store({
autoLoad: true,
model: 'Person',
data : doc,
proxy: {
type: 'memory',
reader: {
type: 'xml',
record: 'Person'
}
}
});
关于xml - 从 XML 字符串而不是文件加载 Extjs 网格,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6430075/