Recursive unlimited tree drop-down menu

Source: Internet
Author: User

Java Code
  1. /** 
  2. * Tree menu Vo 
  3. */
  4.  Public ClassSelecttreeImplementsSerializable {
  5. Private IntID;
  6. PrivateString name;
  7. PrivateList <selecttree> child =NewArraylist <selecttree> ();
  8. // Getter & setter... omitted
  9. }
 
/*** Tree menu VO */public class selecttree implements serializable {private int ID; private string name; private list <selecttree> child = new arraylist <selecttree> (); // getter & setter .... omitted}

Code snippet:

Java code
  1. /** 
  2. * Construct tree menu data 
  3. */
  4. PublicList <selecttree> buildnode (IntPID, list <ycchannel> channels ){
  5. List <selecttree> result =NewArraylist <selecttree> ();
  6. For(Ycchannel chlorophyll: channels ){
  7. Selecttree node =NewSelecttree ();
  8. If(Null! = Chlorophyll. getparentid () & chlorophyll. getparentid (). Equals (PID )){
  9. Node. setid (chlorophyll. getchannelid ());
  10. Node. setname (chlorophyll. getname ());
  11. List <selecttree> Children = buildnode (chlorophyll. getchannelid (), channels );
  12. If(Null! = Children &&0<Children. Size ()){
  13. Node. setchild (children );
  14. }
  15. Result. Add (node );
  16. }
  17. }
  18. ReturnResult;
  19. }
  20. PublicString querychannellist (){
  21. Ycchannellist =This. Channelser. querychannellist ();
  22. List <selecttree> trees =NewArraylist <selecttree> ();
  23. For(Ycchannel YC: ycchannellist ){
  24. If(Null= YC. getparentid ()){
  25. Selecttree T =NewSelecttree ();
  26. T. setid (YC. getchannelid ());
  27. T. setname (YC. getname ());
  28. T. setchild (buildnode (T. GETID (), ycchannellist ));
  29. Trees. Add (t );
  30. }
  31. }
  32. This. Setajaxdata (trees );
  33. ReturnAjax_data;
  34. }
/*** construct tree menu data */public list 
  
    buildnode (int pid, list 
   
     channels) {list 
    
      result = new arraylist 
     
       (); For (ycchannel chlorophyll: channels) {selecttree node = new selecttree (); If (null! = Chlorophyll. getparentid () & chlorophyll. getparentid (). equals (PID) {node. setid (chlorophyll. getchannelid (); node. setname (chlorophyll. getname (); List 
      
        Children = buildnode (chlorophyll. getchannelid (), channels); If (null! = Children & 0 
       
         trees = new arraylist 
        
          (); For (ycchannel YC: ycchannellist) {If (null = YC. getparentid () {selecttree T = new selecttree (); T. setid (YC. getchannelid (); T. setname (YC. getname (); T. setchild (buildnode (T. GETID (), ycchannellist); trees. add (t) ;}} this. setajaxdata (trees); Return ajax_data ;}
        
       
      
     
    
   
  

Front-end JS Code:

JS Code
  1. // Recursive Tree node
  2. FunctionBuildnode (Len, data ){
  3. VaRPrefix ="|";
  4. For(VaRI = 0; I <Len; I ++ ){
  5. Prefix + ="-";
  6. }
  7. $. Each (data,Function(I, item ){
  8. If(0 <item. Child. Length ){
  9. $ ('# Typeid'). Append ("<Option value ="+ Item. ID +">"+ Prefix + item. Name +"</Option>");
  10. Buildnode (LEN + 1, item. Child );
  11. }Else{
  12. $ ('# Typeid'). Append ("<Option value ="+ Item. ID +">"+ Prefix + item. Name +"</Option>");
  13. }
  14. });
  15. }
  16. $. Ajax ({
  17. URL:'$ {Base}/channel/channelaction! Querychannellist. action',
  18. Type:'Get',
  19. Datatype:'Json',
  20. Contenttype:'Application/json',
  21. Success:Function(JSON ){
  22. If(JSON. Success ){
  23. $ ('# Typeid'). Empty ();
  24. $ ('# Typeid'). Append ("<Option value = '0'> select a topic... </option>");
  25. $. Each (JSON. Data,Function(I, item ){
  26. If(Null= Item. parentid ){
  27. $ ('# Typeid'). Append ("<Option value ="+ Item. ID +">"+ Item. Name +"</Option>");
  28. Buildnode (1, item. Child );
  29. }
  30. });
  31. }
  32. },
  33. Error:Function(){
  34. Alert ("An error occurred while loading the topic! ");
  35. }
  36. });
  37. });

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.