extjs3.2生成带复选框的树

HYLJun 发布于 2016/05/13 11:15
阅读 92
收藏 0

@豆仔 你好,想跟你请教个问题:

请问如何生成带复选框的树,下面是相关代码,我是用的是extjs3.2.

js代码

function tree(){
var sb_tree=new Ext.tree.TreePanel({
//useArrows: true,
autoScroll: true,
       animate: false,//开始是true
enableDD: false,
   rootVisible:false,
   containerScroll: true,
            border: false,
            
            checkModel:'cascade',   //对树的级联多选
            onlyLeafCheckable: true,//对树所有结点都可选
            
loader:new Ext.tree.TreeLoader({
dataUrl:"/HW/createTree2.do",
baseAttrs: { uiProvider: TreeCheckNodeUI } //添加 uiProvider 属性
}),

root:{
nodeType:'async',
ext:'�豸',
draggable:false,
id:'sb_root',
}
});

sb_tree.on("check",function(node,checked){alert(node.text+" = "+checked)}); //注册"check"事件

sb_tree.getRootNode().expand();
//�豸���¼�

sb_tree.on("dbclick",function(node){
if(node.isLeaf()){
sb_Field.setRawValue(node.text);
sbmcId=node.id;
sb_Window.close();
}
});

sb_tree.on("dbclick",function(node){
if(node.isLeaf()){
sb_Field.setRawValue(node.text);
    sbmcId=node.id; 
}    
});

createTree2Action.java代码

package com.nari.struts.action;


import java.io.IOException;
import java.sql.ResultSet;
import java.sql.SQLException;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;


import com.nari.util.DBConn;


/** 
 * MyEclipse Struts
 * Creation date: 07-20-2010
 * 
 * XDoclet definition:
 * @struts.action validate="true"
 */




public class CreateTree2Action extends Action {
/*
* Generated Methods
*/


/** 
* Method execute
* @param mapping
* @param form
* @param request
* @param response
* @return ActionForward
*/
//private DBConn  db = DBConn.getInstance();
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) throws JSONException {
// TODO Auto-generated method stub

        //db = new DBConn();
DBConn  db = DBConn.getInstance();

JSONObject obj=new JSONObject();
   JSONArray array=new JSONArray();
   array.put(obj);
   obj.put("id", "0");
   obj.put("text", "设备树");
String sqlString="select  DISTINCT root_id   from T_DEV_RELATION";
ResultSet rsroot=db.query(sqlString);
try {
if (rsroot.next())
{
long rootid=rsroot.getLong(1);
       OutDriversTree(rootid,obj);

}
} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();

}
   
    db.close();
    String s=array.toString();
 
    response.setContentType("application/json;charset=utf-8");//设置响应的内同类型
    try{
    response.getWriter().write(s);
    }catch (IOException e) {
  e.printStackTrace(); // TODO: handle exception
}

return null;
}

private  void OutDriversTree( long father_id,JSONObject parentNode)
{
DBConn  db = DBConn.getInstance();
String sqlString="select c.id,c.dev_name from t_dev_info c  where  c.id  in (select b.child_id from t_dev_relation  b where b.father_id= "+ father_id+")";
ResultSet rs1=db.query(sqlString);
boolean endflag=false;
try {
// endflag=rs1.first();
endflag=rs1.next();

} catch (SQLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try {
JSONArray childArray = new JSONArray();  
while (endflag)
{
//获取设备树信息
String name;
name =rs1.getString(2);
    long id =rs1.getLong(1);
//建立树节点对象
    JSONObject treeNode = new JSONObject();
    treeNode.put("id", ""+ id);
    treeNode.put("text", name);
   
    //形成节点对象集合
    childArray.put(treeNode);



OutDriversTree( id,treeNode);
    endflag=rs1.next();
}
//节点对象集合加入父节点
if (childArray.length()==0)
{
parentNode.put("leaf", true);
//parentNode.put("cls", "x-tree-node-collapsed");
//parentNode.put("iconCls", "x-tree-node-icon");
}
else
{
parentNode.put("leaf", false);
parentNode.put("children",childArray);
}


}
catch (Exception e) {

}
try {
rs1.close();
} catch (SQLException e) {
e.printStackTrace();
}
}


}



加载中
返回顶部
顶部