又是拖曳树,大家来修改Bug

又是拖曳树,大家来修改Bug

看看这是很流行的一个拖曳树,不知是哪个高手写的,不过怎么运行还报错呢?那个items是空的,还请哪个高手检查以下,到底错在哪里了?也让我看明白啊


<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
              <mx:Tree x="379" y="122" width="279" height="278" id="tree"
      dataProvider="{treeDataList}"
      labelField="@label"
      dragEnabled="true"
      dragMoveEnabled="true"
      dropEnabled="true"
      dragEnter="onDragEnter(event)"
      dragOver="onDragOver(event)"
      dragDrop="onDragDrop(event)"
        dragComplete="onDragComplete(event)"
      />
     
       
      <mx:XMLList id="treeDataList">
                      <node label="Piano">
                        <node label="Piano1" value="http://www.gcoresoft.com/"/>
                        <node label="Piano2"/>
                    </node>
                    <node label="Bass">
                        <node label="Bass1"/>
                        <node label="Bass2"/>
                    </node>
                    <node label="Sax">
                        <node label="Sax1"/>
                        <node label="Sax2"/>
                    </node>
                    <node label="Guitar">
                        <node label="Guitar1"/>
                        <node label="Guitar2"/>
                    </node>
      </mx:XMLList>
     
        <mx:Script>
                <![CDATA[
                        import mx.managers.DragManager;
                        import mx.core.DragSource;
                        import mx.events.DragEvent;
                        import mx.core.UIComponent;
                        import mx.controls.Alert;
                       
                        private function onDragEnter( event:DragEvent ) : void
        {
              DragManager.acceptDragDrop(UIComponent(event.currentTarget));
        }
       
        private function onDragOver( event:DragEvent ) : void
      {
          // r is the visible index in the tree
          var dropTarget:Tree = Tree(event.currentTarget);
          var r:int = dropTarget.calculateDropIndex(event);
          tree.selectedIndex = r;

          // retrieving the newly selected node, you can examine it and decide to tell
          // the user the drop is invalid by changing the feedback.
          var node:XML = tree.selectedItem as XML;
          if( node.@type == "minivan" ) {
              DragManager.showFeedback(DragManager.NONE);
              return;
          }

          // the type of drop - copy, link, or move can be reflected in the feedback as well.
          // Here the control and shift keys determine that action.
          if (event.ctrlKey){
                          //        Alert.show("ctrl");
              DragManager.showFeedback(DragManager.COPY);
          }else if (event.shiftKey){
                          //Alert.show("shift");
              DragManager.showFeedback(DragManager.LINK);
          } else {
                          //Alert.show("none");
              DragManager.showFeedback(DragManager.MOVE);
          }
      }

        private function onDragDrop( event:DragEvent ) : void
{
     
      var ds:DragSource = event.dragSource;
     
      var dropTarget:Tree = Tree(event.currentTarget);
               
       
      // retrieve the data associated with the "items" format. This will be the data that
      // the dragInitiator has copied into the DragSource.
      var items:Array = ds.dataForFormat("items") as Array;
               
               
      // determine where in the tree the drop occurs and select that node by the index; followed by
      // retrieving the node itself.
      var r:int = tree.calculateDropIndex(event);
      tree.selectedIndex = r;
        var node:XML = tree.selectedItem as XML;
      var p:*;
       
      // if the selected node has children (it is type==city),
      // then add the items at the beginning
      if( tree.dataDescriptor.hasChildren(node) ) {
            p = node;
            r = 0;
      } else {
            p = node.parent();
      }
                  //Alert.show((items==null).toString());
      // taking all of the items in the DragSouce, insert them into the
      // tree using parent p.
      for(var i:Number=0; i < items.length; i++) {
                Alert.show("4");
            var insert:XML = <node />;
            insert.@label = items;
            insert.@type = "restaurant";
            tree.dataDescriptor.addChildAt(p, insert, r+i);
      }
   
}


        private function onDragComplete( event:DragEvent ) : void
        {
            tree.selectedIndex = -1;
        }

                ]]>
        </mx:Script>
       


</mx:Application>
 

回复: 又是拖曳树,大家来修改Bug

论坛回帖的人越来越少了,昨天贴的一个顶的人都没有?版主啊,整顿一下啊
 

回复:又是拖曳树,大家来修改Bug

有这么复杂吗?

<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute">
        <mx:Script>
                <![CDATA[
                        private var treedata:Array=[
                                {id:1, children:[{id:2, children:[{id:2}]},{id:3},{id:4}]},
                                {id:5, children:[{id:6, children:[{id:7}]},{id:8},{id:9}]},
                        ];
                ]]>
        </mx:Script>
        <mx:Tree x="10" y="10" width="100" dataProvider="{treedata}" dragEnabled="true" dropEnabled="true" labelField="id"></mx:Tree>
       
</mx:Application>


简单即可拖拽了
最后编辑cimmicola 最后编辑于 2008-05-09 08:34:10
Protoss拥有高度的文明、先进的科技和强大的精神力量。但是由于长达千年的寿命,种群数量稀少,消耗不过繁殖能力出众的Zerg,险些被Zerg给灭族。
 

回复: 又是拖曳树,大家来修改Bug

老大,这么简单的大家都会啊,还要实现复制呢?还要监听拖动事件呢
 

回复:又是拖曳树,大家来修改Bug

按住ctrl就是复制,默认的
而且你的需求只说了拖拽,不过这个拖拽的实现跟监听没冲突,你加上drag的相关事件就可以监听了,只是说这个拖拽的实现不需要这么复杂了
最后编辑cimmicola 最后编辑于 2008-05-09 08:44:50
Protoss拥有高度的文明、先进的科技和强大的精神力量。但是由于长达千年的寿命,种群数量稀少,消耗不过繁殖能力出众的Zerg,险些被Zerg给灭族。
 

回复 5F cimmicola 的帖子

关键是他默认的复制在同一棵树中实现不了啊,莫非你刚才的demo能实现,我试过不行
 

回复 6F wxl20031515 的帖子

你能把我发的demo研究一下么?打扰一下了,那个错困扰我好久了
 

回复 7F wxl20031515 的帖子

刚才说错了,行是行,不过反应很迟钝,感觉拖动时很吃力,这就是默认的拖曳。现就是想实现当源拖到目标对象时,让目标对象自动判断能否在该位置存放,若能,自动打开其下所有节点
 

回复:又是拖曳树,大家来修改Bug

第87行
var items:Array = ds.dataForFormat("items") as Array;
改为
var items:Array = ds.dataForFormat("treeItems") as Array;

这文章是flex2时写的
Protoss拥有高度的文明、先进的科技和强大的精神力量。但是由于长达千年的寿命,种群数量稀少,消耗不过繁殖能力出众的Zerg,险些被Zerg给灭族。
 

回复:又是拖曳树,大家来修改Bug

一语千金啊,谢谢老大
 

回复:又是拖曳树,大家来修改Bug

代码这么乱 也没说道重点 :~ :~ 怎么看啊
 

回复: 又是拖曳树,大家来修改Bug

重点的地方都有注释啊,逻辑很简单,就是要实现在一棵树中的拖曳,还有复制,不过树默认的按ctrl键就是复制,我怎么试也不行,根本不是那回事啊,而且拖得很吃力,大家觉得呢?给个高明的解决方法啊!
 
1  /  1  页   1 跳转

版权所有 riachina.com   Sitemap

Powered by Discuz!NT 2.1.202    Copyright © 2001-2008 Comsenz Inc.
Processed in 0 second(s) (Cached).
返顶部