您好,登錄后才能下訂單哦!
前臺代碼:
1、html部分
<div style="width: 99%; height: 80%; margin-top: 4px; border: solid 0px #44c5e4; float: left; text-align: left; OVERFLOW-Y: auto; OVERFLOW-X: auto;">
<ul id="knowledgeTree" class="ztree"></ul>
</div>
2、js部分代碼:
$knowledgeTree ="";
var setting = {
check: {
enable: false, //顯示復選框或者radio
chkStyle: "checkbox", //復選框或者radio
chkboxType: { "Y" : "ps", "N" : "ps" } //控制關系
},
view: {
selectedMulti: false, //true/false分別表示 支持/不支持 同時選中多個節點
showTitle: true, //true/false 分別表示 顯示 /隱藏 提示信息(即鼠標移動到節點上時,自動彈出的提示信息圖層)
showIcon:false
},
data: {
key: {
name: "name", //設置 zTree 顯示節點名稱的屬性名稱
title: "title" //設置 顯示節點提示信息的屬性名稱。[setting.view.showTitle = true 時生效]
},
simpleData: {
enable: true, //是否使用簡易JSON格式數據(非父子結構)
idKey: "id", //設置 zTree 顯示節點名稱的節點名稱
pIdKey: "pid", //設置 zTree 簡易數據的父節點名稱
rootPId: "-1" //最上層父節點
}
},
callback: {
onClick: onClick
}
};
jQuery(document).ready(function(){
$.ajax({
url : localBasePath + "/servlet/KnowledgeToolServlet",
type : "post",
dataType : "json",
success : function(dataObj) {
$knowledgeTree = $.fn.zTree.init($("#knowledgeTree"), setting, dataObj);
}
});
});
3、servlet部分代碼:
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
response.setCharacterEncoding("utf-8");
PrintWriter out = response.getWriter();
KnowledgeService ks=new KnowledgeService();
List<TreeNode> treeNodeList=ks.searchKonwldgeTree();
String treeNodeJson = JSONSerializer.toJSON(treeNodeList).toString();
out.print(treeNodeJson);
out.flush();
out.close();
}
其中KnowledgeService.searchKonwldgeTree()代碼如下:
//查詢知識庫樹
public List<TreeNode> searchKonwldgeTree() {
StringBuffer sql = new StringBuffer();
Connection conn = null;
Statement stmt = null;
List<Knowledge> knowlList = new ArrayList<Knowledge>();
List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
ResultSet rs = null;
try {
conn = newdom.getConnection();
stmt = conn.createStatement();
conn.setAutoCommit(false);
sql.append(" select skl.knowid, skl.usedoctype,sed.typename,skl.knowname,skl.knowtext from SBL_KNOWLEDGE_LIB skl,SZD_EMR_DOCTYPE sed ";
sql.append(" order by skl.usedoctype ");
System.out.println(sql.toString());
rs = stmt.executeQuery(sql.toString());
while (rs.next()) {
Knowledge knowledge=new Knowledge();
knowledge.setId(rs.getString("knowid"));
knowledge.setName(rs.getString("knowname"));
knowledge.setDocType(rs.getString("usedoctype"));
knowledge.setDocTypeName(rs.getString("typename"));
knowledge.setContent(rs.getString("knowtext"));
knowlList.add(knowledge);
}
} catch (Exception ex) {
log.error("查詢知識庫樹錯誤");
ex.printStackTrace();
} finally {
newdom.close(conn, stmt, rs);
}
String typeTemp="";
for(Knowledge knowledge:knowlList){
TreeNode treeNode = new TreeNode();
if(!knowledge.getDocType().equals(typeTemp)){
TreeNode treeNodeParent = new TreeNode();
treeNodeParent.setId(knowledge.getDocType());
treeNodeParent.setName(knowledge.getDocTypeName());
treeNodeParent.setIsParent("true");
treeNodeParent.setChecked("true");
treeNodeParent.setOpen("false");
treeNodeParent.setPid("-1");
treeNodeParent.setTitle(knowledge.getDocTypeName());
treeNodeList.add(treeNodeParent);
}
typeTemp=knowledge.getDocType();
treeNode.setId(knowledge.getId());
treeNode.setName(knowledge.getName());
treeNode.setTitle(knowledge.getName());
treeNode.setContent(knowledge.getContent());
treeNode.setChecked("true");
treeNode.setOpen("true");
treeNode.setIsParent("false");
treeNode.setPid(knowledge.getDocType());
treeNodeList.add(treeNode);
}
return treeNodeList;
}
大致代碼如上,實現的思路是將要做成樹的數據,按照zTree的指定格式,放到一個list里面,本文中樹的內容是表SBL_KNOWLEDGE_LIB ,SZD_EMR_DOCTYPE結合的數據,將這些數據從數據庫中查出來,然后按照實際需要組裝成所需要的List,然后轉換成json格式print出就可以了。這樣ajax里面接收到返回的json格式的對象后,直接賦值給zTree就完成了一個樹的簡單實現。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。