FLEX - 打开树枝..?

时间:2012-01-12 13:40:09

标签: actionscript-3 flex air adobe

我一直在使用此代码来帮助我的树组件中的导航,但我确实有一个问题。

如果我点击“文件夹”或分支(在列出的名称),它打开正常,并关闭具有相同父级的其他打开的文件夹/分支。

但是,如果我点击前方的小箭头,我会收到错误。

<mx:Tree id="treeView" x="0" y="0" width="100%" height="100%"
                             itemRenderer="components.CustomTreeItemRenderer"
                             paddingBottom="2" showRoot="true"
                             itemClick="treeItemClick(event)"
                             itemOpen="treeItemOpen(event)" />

        public function treeItemOpen(event:Event):void {
            var a:Array=openParent(event.currentTarget.selectedItem, [event.currentTarget.selectedItem]);
            if(a) treeView.openItems = a;
        }

        public function openParent(o:Object,a:Array):Array{
            if( o.parent()){
                a.push(o.parent());
                a=openParent(o.parent(),a);
            }
            return a;
        }

这是我收到的错误消息:

TypeError: Error #1009: Cannot access a property or method of a null object reference.
at Test_tree_001/openParent()[D:\Flash Builder - Workspace\vCog Workspace\vCog Flash 14-12-2011 - SWF\src\Test_tree_001.mxml:212]
at Test_tree_001/treeItemOpen()[D:\Flash Builder - Workspace\vCog Workspace\vCog Flash 14-12-2011 - SWF\src\Test_tree_001.mxml:207]
at Test_tree_001/__treeView_itemOpen()[D:\Flash Builder - Workspace\vCog Workspace\vCog Flash 14-12-2011 - SWF\src\Test_tree_001.mxml:705]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at mx.controls::Tree/http://www.adobe.com/2006/flex/mx/internal::dispatchTreeEvent()[E:\dev\hero_private\frameworks\projects\mx\src\mx\controls\Tree.as:3551]
at mx.controls::Tree/http://www.adobe.com/2006/flex/mx/internal::onTweenEnd()[E:\dev\hero_private\frameworks\projects\mx\src\mx\controls\Tree.as:2381]
at mx.effects::Tween/endTween()[E:\dev\hero_private\frameworks\projects\framework\src\mx\effects\Tween.as:566]
at mx.effects::Tween/http://www.adobe.com/2006/flex/mx/internal::doInterval()[E:\dev\hero_private\frameworks\projects\framework\src\mx\effects\Tween.as:607]
at mx.effects::Tween$/timerHandler()[E:\dev\hero_private\frameworks\projects\framework\src\mx\effects\Tween.as:191]
at flash.utils::Timer/_timerDispatch()
at flash.utils::Timer/tick()

修改

我也有这个代码,如果我单击文本就会打开分支。 不知怎的,我仍然遇到新代码的错误。

if (treeView.dataDescriptor.isBranch(selectedItem)){  
                trace("isBranch: Triggered in treeItemClick!");
                treeView.expandItem(selectedItem,!treeView.isItemOpen(selectedItem),false,true);
                }

这是我在新代码中遇到的错误:

TypeError: Error #1010: A term is undefined and has no properties.
at Test_tree_001/treeItemClick()[D:\Flash Builder - Workspace\vCog Workspace\vCog Flash 14-12-2011 - SWF\src\Test_tree_001.mxml:411]
at Test_tree_001/__treeView_itemClick()[D:\Flash Builder - Workspace\vCog Workspace\vCog Flash 14-12-2011 - SWF\src\Test_tree_001.mxml:734]
at flash.events::EventDispatcher/dispatchEventFunction()
at flash.events::EventDispatcher/dispatchEvent()
at mx.core::UIComponent/dispatchEvent()[E:\dev\hero_private\frameworks\projects\framework\src\mx\core\UIComponent.as:13128]
at mx.controls.listClasses::ListBase/mouseClickHandler()[E:\dev\hero_private\frameworks\projects\mx\src\mx\controls\listClasses\ListBase.as:10328]
at mx.controls::Tree/mouseClickHandler()[E:\dev\hero_private\frameworks\projects\mx\src\mx\controls\Tree.as:3129]

1 个答案:

答案 0 :(得分:0)

您收到该错误消息,因为传递给o方法的对象openParent为空。因为没有有效的对象被传递,Flex无法评估parent属性。

我最好的猜测是因为点击箭头时没有selectedItem所以event.currentTarget.selectedItem始终为null。您必须找到另一种识别选择节点的方法。

我做了一些快速谷歌搜索并找到了this article。该文中的最后一条评论提到:

  

此代码对我不起作用,因为我不想打开/关闭项目   如果我点击它们,我希望它们打开/关闭,如果我点击加号/减号。   对我有用的代码:

protected function tree_itemOpenHandler(event:TreeEvent):void { 
  tree.openItems =[event.item,event.item.parent(),event.item.parent().parent(), event.item.parent().parent().parent()]; 
} 
  

这可以处理3级深度的树,例如,如果你有一个   有4个级别的树,你必须添加另一个:   event.item.parent().parent().parent().parent()

希望这有助于您开始朝着正确的方向前进。