XMLListCollection 子元素的 Flex 排序 [英] Flex sorting of XMLListCollection subelements
问题描述
在 Flex webapp 中,是否有一种简单的方法可以根据子元素的属性对 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:
<e prop="AB">1</element>
<e prop="BC">2</element>
<e prop="HF">3</element>
<e prop="AD">4</element>
<e prop="AC">5</element>
<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>
我想对
元素进行排序,在每个 元素中,根据它们的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);
}
}
最后,我希望 保持其定义的顺序,但他们的
子项根据道具"属性.到目前为止,如果我尝试:
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 转换为数组并在数组上排序:
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);
}
}
第二个涉及使用 XMLListCollection
的 sort 属性,尽管我相信 <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屋!