iteye_566
2010-06-22 13:52
浏览 225

求一个例子:extjs树形菜单动态从后台加载节点的实例

本人目前在自学extjs,读的相关书籍是《深入浅出extjs》但是对于本书中的树形菜单动态加载节点以及相关节点的加载和点击事件,总是不能写出满意的例子来,希望各位中能抽出时间的帮忙写一个完整的实例,(可以写成jsp+servlet的,容易一些)要求:
首先,加载所有节点,然后再点击节点的时候能够进行请求提交。当然了,所有的节点都是动态从servlet中取到的。谢谢了。。。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • upup1000 2010-06-22 15:35
    已采纳

    TreeSevice
    [code="java"]
    package name.sencha;

    public class TreeService {
    public String getLevel1Nodes(){
    return "[{text:\"北京\", id:\"1\"},{text:\"吉林\", id:\"2\"}]";
    }

    public String getLevel2Nodes(){
        return "[{text:\"丰台\", id:\"1-1\"},{text:\"海淀\", id:\"1-2\", leaf:true},{text:\"东城\", id:\"1-3\", leaf:true},{text:\"西城\", id:\"1-4\", leaf:true}]";
    }
    
    public String getLevel3Nodes(){
        return "[{text:\"长春\", id:\"2-1\", leaf:true},{text:\"吉林\", id:\"2-2\", leaf:true},{text:\"白山\", id:\"2-3\", leaf:true},{text:\"白城\", id:\"2-4\", leaf:true}]";
    }
    
    public String getLevel4Nodes(){
        return "[{text:\"丰台1区\", id:\"1-1-1\", leaf:true},{text:\"丰台2区\", id:\"1-1-2\", leaf:true},{text:\"丰台3区\", id:\"1-1-3\", leaf:true},{text:\"丰台小学\", id:\"1-1-4\", leaf:true}]";
    }
    
    public String getTreeNodeS(String id){
        if(id==null||id.length()==0){
            return "[]";
        }else if(id.equals("0")){
            return getLevel1Nodes();
        }else if(id.equals("1")){
            return getLevel2Nodes();
        }else if(id.equals("2")){
            return getLevel3Nodes();
        }else{
            return getLevel4Nodes();
        }
    }
    

    }

    [/code]

    TreeServlet :
    [code="java"]
    package name.sencha;

    import java.io.IOException;

    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    public class TreeServlet extends HttpServlet{
    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
    String id = request.getParameter("id");
    if(id!=null&&id.length()>0){
    TreeService service = new TreeService();
    try {
    response.setCharacterEncoding("UTF-8");
    response.getWriter().write(service.getTreeNodeS(id));
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        doPost(request, response);
    }
    

    }

    [/code]

    web.xml:
    [code="java"]

    treeservlet
    name.sencha.TreeServlet


    treeservlet
    /tree

    [/code]

    html :
    [code="html"]
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



    Ext.onReady(function(){ Ext.BLANK_IMAGE_URL = "../../ext-3.0.0/resources/images/default/s.gif"; var rootNode = new Ext.tree.AsyncTreeNode({ text : "根", id : "0", expanded : true }); var tree = new Ext.tree.TreePanel({ root : rootNode, loader : new Ext.tree.TreeLoader({ dataUrl : "../../tree" }), width : 200, height : 400 }); tree.on("beforeload" , function(node){ tree.loader.baseParams.id = node.id; }); tree.on("click", function(node){ Ext.Ajax.request({ url : "../../tree", params : {id:1}, success : function(res){ var txt = res.responseText; var obj = Ext.decode(txt); alert(obj.length); } }); }); tree.render(Ext.get("tree_div")); });

    Insert title here



    [/code]

    点赞 评论
  • upup1000 2010-06-22 16:37

    那我只能说是你处理的有问题了,在我这儿是跑的好好的,我才把代码帖上来了。

    点赞 评论
  • Adeason 2013-10-09 22:50

    这几个码分别写成什么文件,放在什么目录下

    点赞 评论

相关推荐 更多相似问题