Java Code
-
- /**
-
- * Tree menu Vo
-
- */
-
- Public ClassSelecttreeImplementsSerializable {
-
- Private IntID;
-
- PrivateString name;
- PrivateList <selecttree> child =NewArraylist <selecttree> ();
-
- // Getter & setter... omitted
-
- }
/*** 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
-
- /**
- * Construct tree menu data
-
- */
-
- PublicList <selecttree> buildnode (IntPID, list <ycchannel> channels ){
-
- List <selecttree> result =NewArraylist <selecttree> ();
-
- For(Ycchannel chlorophyll: channels ){
- Selecttree node =NewSelecttree ();
-
- If(Null! = Chlorophyll. getparentid () & chlorophyll. getparentid (). Equals (PID )){
-
- Node. setid (chlorophyll. getchannelid ());
-
- Node. setname (chlorophyll. getname ());
-
- List <selecttree> Children = buildnode (chlorophyll. getchannelid (), channels );
- If(Null! = Children &&0<Children. Size ()){
-
- Node. setchild (children );
-
- }
-
- Result. Add (node );
-
- }
-
- }
- ReturnResult;
-
- }
-
-
- PublicString querychannellist (){
-
- Ycchannellist =This. Channelser. querychannellist ();
-
- List <selecttree> trees =NewArraylist <selecttree> ();
-
- For(Ycchannel YC: ycchannellist ){
- If(Null= YC. getparentid ()){
-
- Selecttree T =NewSelecttree ();
-
- T. setid (YC. getchannelid ());
-
- T. setname (YC. getname ());
-
- T. setchild (buildnode (T. GETID (), ycchannellist ));
- Trees. Add (t );
-
- }
-
- }
-
- This. Setajaxdata (trees );
-
- ReturnAjax_data;
-
- }
/*** 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
-
- // Recursive Tree node
-
- FunctionBuildnode (Len, data ){
-
- VaRPrefix ="|";
-
- For(VaRI = 0; I <Len; I ++ ){
- Prefix + ="-";
-
- }
-
- $. Each (data,Function(I, item ){
-
- If(0 <item. Child. Length ){
- $ ('# Typeid'). Append ("<Option value ="+ Item. ID +">"+ Prefix + item. Name +"</Option>");
-
- Buildnode (LEN + 1, item. Child );
-
- }Else{
- $ ('# Typeid'). Append ("<Option value ="+ Item. ID +">"+ Prefix + item. Name +"</Option>");
-
- }
-
- });
-
- }
-
-
- $. Ajax ({
- URL:'$ {Base}/channel/channelaction! Querychannellist. action',
-
- Type:'Get',
-
- Datatype:'Json',
-
- Contenttype:'Application/json',
-
- Success:Function(JSON ){
- If(JSON. Success ){
-
- $ ('# Typeid'). Empty ();
-
- $ ('# Typeid'). Append ("<Option value = '0'> select a topic... </option>");
-
- $. Each (JSON. Data,Function(I, item ){
- If(Null= Item. parentid ){
-
- $ ('# Typeid'). Append ("<Option value ="+ Item. ID +">"+ Item. Name +"</Option>");
-
- Buildnode (1, item. Child );
- }
-
- });
-
- }
-
- },
-
- Error:Function(){
-
- Alert ("An error occurred while loading the topic! ");
-
- }
- });
-
-
- });