Flex中如何在Tree控件中指定一个自定义的label函数使其返回的标签名字为叶项目的名称的例子

<iframe src="http://blog.minidx.com/ext05/defining-a-custom-label-function-on-a-flex-tree-control/main.html" width="550" height="380"></iframe>

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mx:Applicationxmlns:mx="http://www.adobe.com/2006/mxml"
  3. layout="vertical"
  4. verticalAlign="middle"
  5. backgroundColor="white">
  6. <mx:Script>
  7. <![CDATA[
  8. private function tree_labelFunc(item:XML):String {
  9. var label:String;
  10. switch (item.localName()) {
  11. case "league":
  12. label = item.@title;
  13. break;
  14. case "team":
  15. label = item.@name;
  16. break;
  17. case "stadium":
  18. label = item.name;
  19. }
  20. return label;
  21. }
  22. ]]>
  23. </mx:Script>
  24. <mx:XMLid="mlb" source="mlb.xml" />
  25. <mx:Treeid="tree"
  26. dataProvider="{mlb.league}"
  27. labelFunction="tree_labelFunc"
  28. width="300"
  29. rowCount="8"/>
  30. </mx:Application>

下面是

Download: mlb.xml
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <mlb>
  3. <leagueid="al" title="American League">
  4. <teamname="Baltimore Orioles" />
  5. <teamname="Boston Red Sox" />
  6. <teamname="Chicago White Sox" />
  7. <teamname="Cleveland Indians" />
  8. <teamname="Detroit Tigers" />
  9. <teamname="Kansas City Royals" />
  10. <teamname="Los Angeles Angels of Anaheim" />
  11. <teamname="Minnesota Twins" />
  12. <teamname="New York Yankees" />
  13. <teamname="Oakland Athletics" />
  14. <teamname="Seattle Mariners" />
  15. <teamname="Tampa Bay Devil Rays" />
  16. <teamname="Texas Rangers" />
  17. <teamname="Toronto Blue Jays" />
  18. </league>
  19. <leagueid="nl" title="National League">
  20. <teamname="Arizona Diamondbacks" />
  21. <teamname="Atlanta Braves" />
  22. <teamname="Chicago Cubs" />
  23. <teamname="Cincinnati Reds" />
  24. <teamname="Colorado Rockies" />
  25. <teamname="Florida Marlins" />
  26. <teamname="Houston Astros" />
  27. <teamname="Los Angeles Dodgers" />
  28. <teamname="Milwaukee Brewers" />
  29. <teamname="New York Mets" />
  30. <teamname="Philadelphia Phillies" />
  31. <teamname="Pittsburgh Pirates" />
  32. <teamname="San Diego Padres" />
  33. <teamname="San Francisco Giants" />
  34. <teamname="St. Louis Cardinals" />
  35. <teamname="Washington Nationals" />
  36. </league>
  37. </mlb>

你可能感兴趣的:(xml,Flex,Blog,Adobe)