ExtJs动态树

2019-05-09  本文已影响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;
上一篇下一篇

猜你喜欢

热点阅读