jQuery樹控件zTree使用方法詳解(一)
一、節(jié)點(diǎn)模糊搜索功能:搜索成功后,自動(dòng)高亮顯示并定位、展開搜索到的節(jié)點(diǎn)。
二、節(jié)點(diǎn)異步加載:1、點(diǎn)擊展開時(shí)加載數(shù)據(jù);2、選中節(jié)點(diǎn)時(shí)加載數(shù)據(jù)。
前臺代碼如下:
js:
<script type="text/javascript">
//ztree設(shè)置
var setting = {
view: {
fontCss: getFontCss
},
check: {
enable: true
},
data: {
simpleData: {
enable: true,
idKey: "id",
pIdKey: "pId",
rootPId: 0
}
},
async: {
enable: true,
url: "#{getStudentsJsonUrl}",
autoParam: ["id", "level"]
},
callback: {
beforeCheck: zTreeBeforeCheck,
onNodeCreated: zTreeOnNodeCreated,
beforeExpand: zTreeBeforeExpand
}
};
var reloadFlag = false; //是否重新異步請求
var checkFlag = true; //是否選中
//節(jié)點(diǎn)展開前
function zTreeBeforeExpand(treeId, treeNode) {
reloadFlag = false;
return true;
};
//節(jié)點(diǎn)創(chuàng)建后
function zTreeOnNodeCreated(event, treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj(treeId);
if (reloadFlag) {
if (checkFlag) {
zTree.checkNode(treeNode, true, true);
}
if (!treeNode.children) {
zTree.reAsyncChildNodes(treeNode, "refresh");
}
}
};
//選中節(jié)點(diǎn)前
function zTreeBeforeCheck(treeId, treeNode) {
var zTree = $.fn.zTree.getZTreeObj(treeId);
if (!treeNode.children) {
reloadFlag = true;
checkFlag = true;
zTree.reAsyncChildNodes(treeNode, "refresh");
}
return true;
}
//頁面加載完成
_run(function () {
require(['zTree/js/jquery.ztree.all-3.5'], function () {
$.ajax({
type: "POST",
url: "#{getStudentsJsonUrl}",
success: function (data) {
if (data && data.length != 0) { //如果結(jié)果不為空
$.fn.zTree.init($("#tree"), setting, data);
}
else { //搜索不到結(jié)果
}
}
});
});
//提交
$("#inputSubmit").click(function () {
var zTree = $.fn.zTree.getZTreeObj("tree");
var nodes = zTree.getCheckedNodes(true);
var ids = "";
var names = "";
for (var i = 0; i < nodes.length; i++) { //遍歷選擇的節(jié)點(diǎn)集合
if (!nodes[i].isParent) {
ids += nodes[i].id.replace("level" + nodes[i].level, "") + ",";
names += nodes[i].name + ",";
}
}
Simpo.ui.box.hideBox();
parent.$(".boxFrm").contents().find("#inputRange").val(names.substr(0, names.length - 1));
parent.$(".boxFrm").contents().find("#hidRange").val(ids.substr(0, ids.length - 1));
})
});
//查找節(jié)點(diǎn)
var lastNodeList = [];
var lastKey;
function searchNode() {
var zTree = $.fn.zTree.getZTreeObj("tree");
var key = $.trim($("#inputSearchNode").val());
if (key != "" && key != lastKey) {
nodeList = zTree.getNodesByParamFuzzy("name", key);
for (var i = 0, l = lastNodeList.length; i < l; i++) { //上次查詢的節(jié)點(diǎn)集合取消高亮
lastNodeList[i].highlight = false;
zTree.updateNode(lastNodeList[i]);
}
zTree.expandAll(false); //全部收縮
if (nodeList.length > 0) {
for (var i = 0, l = nodeList.length; i < l; i++) { //遍歷找到的節(jié)點(diǎn)集合
if (nodeList[i].getParentNode()) {
zTree.expandNode(nodeList[i].getParentNode(), true, false, false); //展開其父節(jié)點(diǎn)
}
nodeList[i].highlight = true;
zTree.updateNode(nodeList[i]);
}
}
zTree.refresh(); // 很重要,否則節(jié)點(diǎn)狀態(tài)更新混亂。
lastNodeList = nodeList;
lastKey = key;
}
}
//加載數(shù)據(jù)
function loadData() {
var zTree = $.fn.zTree.getZTreeObj("tree");
var rootNodes = zTree.getNodes();
reloadFlag = true;
checkFlag = false;
for (var i = 0; i < rootNodes.length; i++) {
if (!rootNodes[i].children) {
zTree.reAsyncChildNodes(rootNodes[i], "refresh"); //異步加載
}
}
}
//全部收縮
function closeAll() {
var zTree = $.fn.zTree.getZTreeObj("tree");
if ($("#inputCloseAll").val() == "全部收縮") {
zTree.expandAll(false);
$("#inputCloseAll").val("全部展開")
}
else {
zTree.expandAll(true);
$("#inputCloseAll").val("全部收縮")
}
}
//高亮樣式
function getFontCss(treeId, treeNode) {
return (treeNode.highlight) ? { color: "#A60000", "font-weight": "bold"} : { color: "#333", "font-weight": "normal" };
}
</script>
html:
<div style="width: 200px; height: 260px; overflow: auto; border: solid 1px #666;"> <ul id="tree" class="ztree"> </ul> </div>
后臺代碼(后臺返回Json數(shù)據(jù)):
public void SelStudent()
{
set("getStudentsJsonUrl", to(GetStudentsJson));
}
public void GetStudentsJson()
{
List<Dictionary<string, string>> dicList = new List<Dictionary<string, string>>();
string level = ctx.Post("level");
string id = ctx.Post("id");
if (strUtil.IsNullOrEmpty(id))
{
#region 加載班級
//獲取當(dāng)前登錄用戶
Sys_User user = AdminSecurityUtils.GetLoginUser(ctx);
//獲取當(dāng)前用戶關(guān)聯(lián)的老師
Edu_Teacher teacher = edu_TeacService.FindByUserId(user.Id);
//獲取班級集合
List<Edu_ClaNameFlow> list = edu_ClaNameFlowService.GetListByTeacherId(teacher.Id);
foreach (Edu_ClaNameFlow item in list)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("id", "level0" + item.Calss.Id.ToString());
dic.Add("pId", "0");
dic.Add("name", item.Gra.Name + item.Calss.Name);
dic.Add("isParent", "true");
dicList.Add(dic);
}
#endregion
}
else
{
if (level == "0")
{
//加載學(xué)生
List<Edu_Student> list = edu_StudService.GetListByClassId(id.Replace("level0", ""));
foreach (Edu_Student item in list)
{
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("id", "level1" + item.Id.ToString());
dic.Add("pId", id);
dic.Add("name", item.Name);
dic.Add("isParent", "false");
dicList.Add(dic);
}
}
}
echoJson(dicList);
}
更多關(guān)于ztree控件的內(nèi)容,請參考專題《jQuery插件ztree使用匯總》 。
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
jQuery dialog 異步調(diào)用ashx,webservice數(shù)據(jù)的代碼
點(diǎn)擊按鈕,在彈出的jQuery.dialog中,顯示異步返回的數(shù)據(jù)。WebService可以寫復(fù)雜的函數(shù),ashx可以根據(jù)傳過來的參數(shù)調(diào)用不同的方法,達(dá)到同樣的效果。2010-08-08
jquery動(dòng)態(tài)添加刪除一行數(shù)據(jù)示例
這篇文章主要介紹了jquery如何動(dòng)態(tài)添加刪除一行數(shù)據(jù),需要的朋友可以參考下2014-06-06
jQuery中event.target和this的區(qū)別詳解
這篇文章主要介紹了jQuery中event.target和this的區(qū)別詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-08-08
jQuery實(shí)現(xiàn)簡單的滑動(dòng)導(dǎo)航代碼(移動(dòng)端)
這篇文章主要介紹了jQuery實(shí)現(xiàn)簡單的滑動(dòng)導(dǎo)航代碼,適合用于移動(dòng)端。需要的朋友可以參考下2017-05-05
jQuery實(shí)現(xiàn)的導(dǎo)航下拉菜單效果
這篇文章主要介紹了jQuery實(shí)現(xiàn)的導(dǎo)航下拉菜單效果,涉及jQuery響應(yīng)鼠標(biāo)事件動(dòng)態(tài)操作頁面元素的相關(guān)技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2016-07-07
eclipse導(dǎo)入jquery包后報(bào)錯(cuò)的解決方法
eclipse導(dǎo)入jquery包后報(bào)錯(cuò),下面有個(gè)不錯(cuò)的解決方法,需要的朋友可以參考下2014-02-02
jquery 卷簾效果實(shí)現(xiàn)代碼(不同方向)
卷簾效果在生活中應(yīng)用比較廣泛,因?yàn)樗囊曈X效果比較不錯(cuò),本文用代碼實(shí)現(xiàn)一個(gè),感興趣的朋友不妨了解一下,或許對你學(xué)習(xí)jquery有所幫助,好了話不多說切入正題2013-02-02
jquery獲取對象的方法足以應(yīng)付常見的各種類型的對象
獲取對象是比較常見的操作了,因?yàn)橄胍僮饕粋€(gè)對象之前,一定要先獲取,這是必須的,下面整理了常見對象的獲取方法,希望對大家有所幫助2014-05-05
jQuery+HTML5美女瀑布流布局實(shí)現(xiàn)方法
這篇文章主要介紹了jQuery+HTML5美女瀑布流布局實(shí)現(xiàn)方法,涉及jQuery結(jié)合HTML5實(shí)現(xiàn)瀑布流效果的動(dòng)態(tài)加載功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-09-09
etmvc+jQuery EasyUI+combobox多值操作實(shí)現(xiàn)角色授權(quán)實(shí)例
本篇文章主要介紹了etmvc+jQuery EasyUI+combobox多值操作實(shí)現(xiàn)角色授權(quán),實(shí)現(xiàn)對角色role進(jìn)行授權(quán)操作,有需要的可以了解一下。2016-11-11

