在 adobe flex 中可以使用什么代替 tree.dataProvider?
What can be used instead of tree.dataProvider in adobe flex?
我的 .mxml 如下:
<fx:Declarations>
<fx:XMLList id="data">
<node>
<node label="Inbox">
<node label="Marketing"/>
<node label="Product Management"/>
<node label="Personal"/>
</node>
<node label="Outbox">
<node label="Professional"/>
<node label="Private"/>
</node>
<node label="Spam">kushal</node>
<node label="Sent"/>
</node>
</fx:XMLList>
</fx:Declarations>
<mx:VBox>
<mx:Button label="Search by name" click="findByName()" />
<mx:Tree id="tree" width="500" height="500"
showRoot="false" dataProvider="{data}"
labelField="@label" />
</mx:VBox>
我正在尝试通过单击按钮来单击 findByName():
即:
private function findByName():void
{
var mixml:XMLList = new XMLList(data);
var searchStr:String = "Outbox";
//child.expandChildrenOf(myXML[0], false);
//mixml=data;
searchResult= mixml.node.(@label==searchStr);
var xn:XML = searchResult[searchResultIndex];
Alert.show("xn"+ xn);
searchResultIndex = 0;
if (searchResult[searchResultIndex] != undefined)
var xmlNode:XML = searchResult[searchResultIndex];
while (xmlNode.parent() != null) {
Alert.show("xmlNodeBefore"+ xmlNode);
xmlNode = xmlNode.parent();
Alert.show("xmlNodeAfter"+ xmlNode);
//checkpoint 1
tree.expandItem(xmlNode, true, false);
tree.selectedItem = searchResult[searchResultIndex];
Alert.show(" tree.selectedItem " + tree.selectedItem );
}
}
如果这里不是数据
我使用 tree.dataProvider 然后这段代码不起作用,有人能告诉我为什么吗?
如果您查看 Tree.dataProvider 的 setter,您可以看到它 modifies/translates 传入的值。
这行代码将转换为 null,因为 dataProvider 将基于 xml 的数据更改为 XmlListCollection。
var mixml:XMLList = new XMLList(data);
这里是setter供参考。
override public function set dataProvider(value:Object):void
{
// in all cases save off the original
if (_rootModel)
_rootModel.removeEventListener(
CollectionEvent.COLLECTION_CHANGE,
collectionChangeHandler);
// handle strings and xml
if (typeof(value)=="string")
value = new XML(value);
else if (value is XMLNode)
value = new XML(XMLNode(value).toString());
else if (value is XMLList)
value = new XMLListCollection(value as XMLList);
if (value is XML)
{
_hasRoot = true;
var xl:XMLList = new XMLList();
xl += value;
_rootModel = new XMLListCollection(xl);
}
//if already a collection dont make new one
else if (value is ICollectionView)
{
_rootModel = ICollectionView(value);
if (_rootModel.length == 1)
_hasRoot = true;
}
else if (value is Array)
{
_rootModel = new ArrayCollection(value as Array);
}
//all other types get wrapped in an ArrayCollection
else if (value is Object)
{
_hasRoot = true;
// convert to an array containing this one item
var tmp:Array = [];
tmp.push(value);
_rootModel = new ArrayCollection(tmp);
}
else
{
_rootModel = new ArrayCollection();
}
//flag for processing in commitProps
dataProviderChanged = true;
invalidateProperties();
}
当您使用 tree.dataProvider
时,您的代码不起作用,因为它属于 XMLListCollection
类型,它是 XMLList
对象顶部的包装器。 Flex 在内部将 XMLList
转换为 XMLListCollection
,因为 XMLListCollection
具有通过事件提供更改通知的功能,而 XMLList
没有。
所以在您的代码中,如果您在以下行使用 tree.dataProvider
:
var mixml:XMLList = new XMLList(tree.dataProvider);
您实际上是在尝试键入转换并将 XMLListCollection
转换为 XMLList,这就是它不起作用的原因。
有趣的是,XMLListCollection
具有 public 属性(与 Flex 中的大多数集合一样),可用于访问构成它的基础 XMLList
源。因此,要访问树中的数据,一种正确的方法是:
var mixml:XMLList = new XMLList(tree.dataProvider.source);
我已经在你的代码中试过了,它似乎和你上面的代码做了同样的工作。
您可以在此处阅读更多相关信息:
希望这个解释能奏效,并能从整体上回答您的问题。
干杯。
我的 .mxml 如下:
<fx:Declarations>
<fx:XMLList id="data">
<node>
<node label="Inbox">
<node label="Marketing"/>
<node label="Product Management"/>
<node label="Personal"/>
</node>
<node label="Outbox">
<node label="Professional"/>
<node label="Private"/>
</node>
<node label="Spam">kushal</node>
<node label="Sent"/>
</node>
</fx:XMLList>
</fx:Declarations>
<mx:VBox>
<mx:Button label="Search by name" click="findByName()" />
<mx:Tree id="tree" width="500" height="500"
showRoot="false" dataProvider="{data}"
labelField="@label" />
</mx:VBox>
我正在尝试通过单击按钮来单击 findByName(): 即:
private function findByName():void
{
var mixml:XMLList = new XMLList(data);
var searchStr:String = "Outbox";
//child.expandChildrenOf(myXML[0], false);
//mixml=data;
searchResult= mixml.node.(@label==searchStr);
var xn:XML = searchResult[searchResultIndex];
Alert.show("xn"+ xn);
searchResultIndex = 0;
if (searchResult[searchResultIndex] != undefined)
var xmlNode:XML = searchResult[searchResultIndex];
while (xmlNode.parent() != null) {
Alert.show("xmlNodeBefore"+ xmlNode);
xmlNode = xmlNode.parent();
Alert.show("xmlNodeAfter"+ xmlNode);
//checkpoint 1
tree.expandItem(xmlNode, true, false);
tree.selectedItem = searchResult[searchResultIndex];
Alert.show(" tree.selectedItem " + tree.selectedItem );
}
}
如果这里不是数据 我使用 tree.dataProvider 然后这段代码不起作用,有人能告诉我为什么吗?
如果您查看 Tree.dataProvider 的 setter,您可以看到它 modifies/translates 传入的值。
这行代码将转换为 null,因为 dataProvider 将基于 xml 的数据更改为 XmlListCollection。
var mixml:XMLList = new XMLList(data);
这里是setter供参考。
override public function set dataProvider(value:Object):void
{
// in all cases save off the original
if (_rootModel)
_rootModel.removeEventListener(
CollectionEvent.COLLECTION_CHANGE,
collectionChangeHandler);
// handle strings and xml
if (typeof(value)=="string")
value = new XML(value);
else if (value is XMLNode)
value = new XML(XMLNode(value).toString());
else if (value is XMLList)
value = new XMLListCollection(value as XMLList);
if (value is XML)
{
_hasRoot = true;
var xl:XMLList = new XMLList();
xl += value;
_rootModel = new XMLListCollection(xl);
}
//if already a collection dont make new one
else if (value is ICollectionView)
{
_rootModel = ICollectionView(value);
if (_rootModel.length == 1)
_hasRoot = true;
}
else if (value is Array)
{
_rootModel = new ArrayCollection(value as Array);
}
//all other types get wrapped in an ArrayCollection
else if (value is Object)
{
_hasRoot = true;
// convert to an array containing this one item
var tmp:Array = [];
tmp.push(value);
_rootModel = new ArrayCollection(tmp);
}
else
{
_rootModel = new ArrayCollection();
}
//flag for processing in commitProps
dataProviderChanged = true;
invalidateProperties();
}
当您使用 tree.dataProvider
时,您的代码不起作用,因为它属于 XMLListCollection
类型,它是 XMLList
对象顶部的包装器。 Flex 在内部将 XMLList
转换为 XMLListCollection
,因为 XMLListCollection
具有通过事件提供更改通知的功能,而 XMLList
没有。
所以在您的代码中,如果您在以下行使用 tree.dataProvider
:
var mixml:XMLList = new XMLList(tree.dataProvider);
您实际上是在尝试键入转换并将 XMLListCollection
转换为 XMLList,这就是它不起作用的原因。
有趣的是,XMLListCollection
具有 public 属性(与 Flex 中的大多数集合一样),可用于访问构成它的基础 XMLList
源。因此,要访问树中的数据,一种正确的方法是:
var mixml:XMLList = new XMLList(tree.dataProvider.source);
我已经在你的代码中试过了,它似乎和你上面的代码做了同样的工作。
您可以在此处阅读更多相关信息:
希望这个解释能奏效,并能从整体上回答您的问题。
干杯。