简体   繁体   English

将XML加载到extjs4树存储中

[英]Load xml into extjs4 treestore

I need to load data into my treestore. 我需要将数据加载到我的树存储中。 My ajax request give me XML data without leaf and text attribute. 我的ajax请求为我提供了没有叶子和文本属性的XML数据。 How I can map them ? 我如何绘制它们?

<items>
   <item>mytext</item>
   <item>mytext2</item>
   ...
</items>

I know for leaf property but for text I tried mapping in my model: 我知道叶子属性,但是对于文本,我尝试在模型中映射:

fields: [
    { name: 'leaf' ,  type: 'boolean' , defaultValue: true } ,
    { name: 'text', mapping: 'item'}, 
]

and the reader in my proxy store: 和我的代理商店中的读者:

reader: { type: 'xml',root: 'items' , record: 'item' }

I have all the nodes but without text =/ 我有所有节点,但没有文字= /

Please help me ! 请帮我 !

-kyrillos -kyrillos

Looking at the source code, the problem is that Extjs 4 expects a model to have sub-elements, and your xml has the data at the top most level for a model. 查看源代码,问题在于Extjs 4期望模型具有子元素,而您的xml具有模型中最顶层的数据。 You will either have to restructure the xml returned to something like this: 您将不得不将返回的xml重组为如下所示:

<items>
   <item>
       <text>mytext</text>
   </item>
   <item>
       <text>mytext</text>
   </item>
   ...
</items>

And use mapping to the text node that's in there. 并使用映射到其中的文本节点。

The other option is to create a custom mapping function that will work with your structure (not probably the way Extjs would like you to do it, but if you can't change the xml, it's the only way): 另一个选择是创建一个将与您的结构一起使用的自定义映射函数(这可能不是Extjs希望您执行的方式,但是如果您不能更改xml,这是唯一的方法):

fields: [
    { name: 'leaf' ,  type: 'boolean' , defaultValue: true } ,
    { name: 'text', mapping: function(node){
        if(node.firstChild && node.firstChild.nodeValue)
            return node.firstChild.nodeValue;
        return "";
    }}, 
]

I found an other solution : 我找到了另一个解决方案:

fields: [
    { name: 'leaf' ,  type: 'boolean' , defaultValue: true } ,
    { name: 'text', mapping: '/' }
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM