美文网首页
ExtJs动态树

ExtJs动态树

作者: 激扬文字 | 来源:发表于2019-05-09 23:30 被阅读0次

    二话不说上代码:

    html:
    
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=gbk">
            <title>tree</title>
        <link rel="stylesheet" type="text/css" href="extjs/resources/css/ext-all.css"/>
    <script type="text/javascript" src="extjs/adapter/ext/ext-base.js"></script>
    <script type="text/javascript" src="extjs/ext-all.js"></script>
            
            <script type="text/javascript">
    Ext.onReady(function(){
     Ext.QuickTips.init();
      Ext.BLANK_IMAGE_URL="extjs/resources/images/default/s.gif";
        var _tree = new Ext.tree.TreePanel({
            el: 'tree',
            width:150,
            loader: new Ext.tree.TreeLoader({dataUrl: 'test'})
        });
    
        var _root = new Ext.tree.AsyncTreeNode({
            id: '1',
            text:'中国',
            checked:false
        });
    
        _tree.setRootNode(_root);
        _tree.render();
    
        _root.expand(false, false);
        
      
        
        
        _tree.on('checkchange',function(_node,checked){
         //_node.expand();
       _node.attributes.checked=checked;
       _node.eachChild(function(child){
        child.ui.toggleCheck(checked);
        child.attributes.checked=checked;
        
        child.fireEvent('checkchange',child,checked);
        
        });
       },_tree);
       
        var _botton=new Ext.Button({
       text : '请选择',
       handler: function (){
        var _b=_tree.getChecked();
        var checkedid=new Array;
        if(_b.length==0){
         alert("您什么也没选!");
         return false;}
        for(var i=0;i<_b.length;i++){
         checkedid.push(_b[i].id);
         }
         alert(checkedid.toString());
        }
       }).render(document.body,"btn");
        
        
    });
            </script>
        </head>
        <body>
           
            <div id="tree" style="height:300px;"></div>
            <div id="btn"></div>
        </body>
    </html>
    s2:
    
    request.setCharacterEncoding("UTF-8");
         response.setCharacterEncoding("UTF-8");
    
         // 获得node参数,对应的是正在展开的节点id
         String node = request.getParameter("node");
         System.out.println(node);
         List<Node> list=ld.getLocation(Integer.parseInt(node));
      JSONArray ja=JSONArray.fromObject(list);
      String sf=ja.toString();
      System.out.println(sf);
    
         
         response.getWriter().print(sf);
    
    dao:
    
    Connection con=null;
      PreparedStatement stmt=null;
      ResultSet rs=null;
      List list=new ArrayList();
      try {
       con=DbUtil.getConnection();
       
       stmt=con.prepareStatement("select id,name,parentid,ischild from location where parentid=?");
       stmt.setInt(1, parentid);
       rs=stmt.executeQuery(); 
    
       while(rs.next()){
        Node n=new Node();
        n.setId(rs.getInt("id"));
        n.setText(rs.getString("name"));
    
        if(rs.getInt("ischild")==0){
         n.setLeaf(true);
        }else{
         n.setLeaf(false);
        }
        n.setChecked(false);
        list.add(n);
       }
       return list;
      } catch (Exception e) {
       e.printStackTrace();
      }finally{
       try {
        rs.close();
       } catch (SQLException e) {
    
        e.printStackTrace();
       }
       
       try {
        stmt.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
       
       try {
        con.close();
       } catch (SQLException e) {
        e.printStackTrace();
       }
      }
      return null;
    

    相关文章

      网友评论

          本文标题:ExtJs动态树

          本文链接:https://www.haomeiwen.com/subject/juunoqtx.html