XMLListCollection子元素的Flex排序 [英] Flex sorting of XMLListCollection subelements
问题描述
在Flex web应用程序中,是否有一种简单的方法来根据子元素的属性对XML元素的子元素应用排序?示例如下:
In a Flex webapp, is there an easy way to go about applying a sort to the children of an XML element, based on the children's attributes? Example follows below:
XMLListCollection:
XMLListCollection:
; a anotherProp =ABCDE>
< e prop =AB> 1< / element> / code>
< e prop =BC> 2< / element>
< / a>
< a anotherProp =FGEH>
< e prop =HF> 3< / element>
< e prop =AD> 4< / element>
< e prop =AC> 5< / element>
< / a>
br />
<a anotherProp="ABCDE">
<e prop="AB">1</element>
<e prop="BC">2</element>
</a>
<a anotherProp="FGEH">
<e prop="HF">3</element>
<e prop="AD">4</element>
<e prop="AC">5</element>
</a>
我想对< e>
元素,在每个< a>
元素中,根据他们的prop属性。生成包含< a>
元素的数组的代码如下:
I would like to sort the <e>
elements, within each <a>
element separately, according to their "prop" attribute. My code for generating the array containing the <a>
elements is along the lines of:
for each(var node:XML in initialInput:XMLListCollection){
if(node.localName()=="a"){
//I was hoping to be able to sort the <e> children of the node variable here
xmlListCollectionVar.addItem(node);
}
}
结尾我想要< a>
以保持其定义的顺序,但是他们的< e>
属性。到目前为止,如果我尝试:
At the end I would like the <a>
's to remain in their defined order, but their <e>
children to be sorted based on the "prop" attribute. So far if I try:
node.children()。sort = someSortVar
node.children().sort=someSortVar
其中someSortVar的字段设置为:
where someSortVar has its fields set to:
SortFields(e。@ prop,。 ..)
SortFields("e.@prop",...)
我得到一个空值的异常。任何方式将儿童列表转换为XMLListCollection,排序它,并将其集成回节点变量?感谢任何回复。
I get an exception about a null value. Any way to convert the children list to XMLListCollection, sort it and integrate it back into the node variable? Thanks for any replies.
推荐答案
我想出了两个解决方案:第一个涉及将XMLList转换为数组, array:
I came up with two solutions: The first involved converting an XMLList to an array and sorting on the array:
for each (var a:XML in elt..a)
{
var children:Array = toArray(a.children());
children.sortOn("@prop");
a.setChildren(new XMLList());
for each (var c:XML in children)
{
a.appendChild(c);
}
}
第二个涉及使用< c $ c> XMLListCollection ,虽然我相信< a>子项是 XMLList
,而不是 XMLListCollection
:
The second involved using the sort attribute of an XMLListCollection
, although I believe that the <a> children are an XMLList
, not an XMLListCollection
:
var sort:Sort = new Sort()
sort.fields = [new SortField("@prop")];
for each (var a:XML in xml..a)
{
var xcoll:XMLListCollection = new XMLListCollection(a.children());
xcoll.sort = sort;
xcoll.refresh();
a.setChildren(xcoll.copy());
}
这篇关于XMLListCollection子元素的Flex排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!