因数据量较大,一下子全部提取上来比较耗时间,所以项目中决定用延时加载,在看的时候 。注意一下initialize、itemOpen事件,还有isBranch、和isLoad属性。搞清楚了这几个重要的东西,基本上就能明白了,下面贴代码
flex代码:
  1. <mx:Tree id="agencyTree"
  2. horizontalScrollPolicy="on"
  3. width="190"
  4. labelField="@name"
  5. height="100%"
  6. scroll="agencyTree_scrollHandler(event)"
  7. dataProvider="{agencyList}"
  8. change="treeChanged(event)"
  9. initialize="agencyTree_initializeHandler(event)"
  10. itemOpen="agencyTree_itemOpenHandler(event)"
  11. click="agencyTree_clickHandler(event)">
  12. </mx:Tree>
复制代码
  1. //获取结构树
  2. public function findAgency_resultHandler(result:ResultEvent):void
  3. {
  4. agencyList=new XML(result.message.body);
  5. if (agencyTree != null)
  6. {
  7. agencyTree.callLater(expandTree);
  8. }
  9. }
复制代码

initialize取得初始化要显示的数据

  1. //调用后台java中的方法(findDistrictsxmlByUser)
  2. districtProxy.findDistrictsxml().addResultListener(findDistricts_resultHandler);
复制代码
findDistrictsxmlByUser代码
  1. public String findDistrictsxmlByUser(User user) throws ServiceException,
  2. SQLException {
  3. String endNode = new String("</node>");
  4. StringBuffer strXML = new StringBuffer();
  5. District district1 = districtDAO.find(District.class, user.getAgency()
  6. .getDistrictId());
  7. strXML.append("<node id='" + district1.getId() + "' name='"
  8. + district1.getName() + "' level='" + district1.getLevel()
  9. + "' code='" + district1.getCode() + "' isLoad='" + "true"
  10. + "' isBranch='" + true + "'>");
  11. List<District> subDistricts = districtDAO.findDistrctByParentId(user
  12. .getAgency().getDistrictId());
  13. District district;
  14. for (int i = 0; i < subDistricts.size(); i++) {
  15. district = subDistricts.get(i);
  16. List<District> districts = districtDAO
  17. .findDistrctByParentId(district.getId());
  18. // 如果有叶子节点isBranch=true ,否则不管
  19. if (districts != null && districts.size() > 0) {
  20. strXML
  21. .append("<node id='" + district.getId() + "' name='"
  22. + district.getName() + "' level='"
  23. + district.getLevel() + "' code='"
  24. + district.getCode() + "' isLoad='" + "false"
  25. + "' isBranch='" + true + "'>");
  26. } else {
  27. strXML.append(strXMLAppendAddIsLoad(district));
  28. }
  29. strXML.append(endNode);
  30. }
  31. strXML.append(endNode);
  32. return strXML.toString();
  33. }
复制代码

特别注意上边拼字符串中的 isBranch='" + true + "',这个表示当前是非叶子节点,当初为了找这个可是花了些时间,然后里面还有个isLoad属性,先不用管到了后面你就知道了

下面给tree添加一个itemOpen事件agencyTree_itemOpenHandler,即每次打开时候去后台取数据
agencyTree_itemOpenHandler:


  1. private var currentItem:XML;
  2. [Bindable]
  3. //点击机构树节点的时候
  4. protected function agencyTree_itemOpenHandler(event:TreeEvent):void
  5. {
  6. if(event.type == TreeEvent.ITEM_OPEN){
  7. var e:TreeEvent = TreeEvent(event);
  8. currentItem = XML(e.item);
  9. //此时这句话的作用来了,当isLoad为false的时候才去后台去数据,取完之后把isLoad改为true,这样下次点击的时候
  10. //就不会在去取数据了。如果不加这个判断的话 ,每次点开节点的时候都会重复的添加数据
  11. if(currentItem.@isLoad == "false"){
  12. agencyProxy.findAgencyByParentId(currentItem.@id).addResultListener(agencyTree_itemOpenHandler_result);
  13. }
  14. }
  15. }
  16. public function agencyTree_itemOpenHandler_result(event:ResultEvent):void
  17. {
  18. var subAgencyData:XMLList = new XMLList(event.message.body.toString());
  19. // currentItem.appendChild(subAgencyData);//这两种方法都可以,但是下面那种速度快很多
  20. currentItem.node += subAgencyData;
  21. //自动获取树数据超过显示范围时没有自动生成滚动条.好犀利
  22. (agencyTree.dataProvider as XMLListCollection).itemUpdated(currentItem);
  23. (agencyTree.dataProvider as XMLListCollection).dispatchEvent(new CollectionEvent(CollectionEvent.COLLECTION_CHANGE, false, false, CollectionEventKind.ADD, -1, -1, [currentItem]));
  24. currentItem.@isLoad = "true";
  25. }
复制代码

findAgencyByParentId跟上面java中的方法类似就不列举出来了,至此flex tree的延时加载就实现了。唉,想想当时做的时候可费了我不少时间,在此记录一下,也为其他的朋友做做参考吧 ,,嘿嘿

效果图
QQ截图未命名.jpg
2011-4-26 12:52 上传
下载附件 (30.63 KB)
posted on 2011-07-13 11:20  立伟  阅读(2019)  评论(0编辑  收藏  举报