jquery下jstree简单应用 - v1.0


Posted in Javascript onApril 14, 2011

第一篇文章,具体使用也过去很长时间了,直接贴码:
1.代码中使用json数据格式(直接在页面中组装成的,并非后台组装,具体方法:function _callBack(d)) 2.提供右键菜单及功能实现
3.具有checkbox,提供获取选中节点ID方法:function getMenuIds()

<script type="text/javascript" src="@{'/public/javascripts/jquery-1.4.2.min.js'}"></script> 
<script type="text/javascript" src="@{'/public/javascripts/jsTree/jquery.jstree.js'}"></script> 
<script type="text/javascript" src="@{'/public/javascripts/jsTree/_lib/jquery.hotkeys.js'}"></script> 
<script type="text/javascript" src="@{'/public/javascripts/jsTree/_lib/jquery.cookie.js'}"></script> 
<link type="text/css" rel="stylesheet" href="@{'/public/javascripts/jsTree/_docs/!style.css'}"/> 
<script type="text/javascript"> 
</script> 
<table width="100%" height="100%" cellspacing="0" CELLPADDING="0" 
border="5px"> 
<caption align="top"> 
<div id='title' align="center"></div> 
<br /> 
<input type="button" style='left' id='tmp' value='test' onclick="getMenuIds()"></input> 
<a href='@{UserApplication.logout()}' style="display: block; float: right;"> 退出</a> 
<font color="green"><div id="userinfo" style="display: block; float: right;"></div></font><br/> 
<tr> 
<td style="width: 20%; height: 700px"> 
<div id='tree' style="height: 100%" class='body'></div> 
</td> 
<td> 
<div id='content' class="code_f" 
style='width: 98%; height: 698px; padding-top: 10px; padding-left: 10px;padding-right: -10px;'></div> 
</td> 
</tr> 
</table> 
<script type="text/javascript"> 
$("#title").html("js(jstree)和play!framework的学习及应用"); 
$("#content").html("js(jstree)和play!framework的学习及应用"); 
</script> 
<script type="text/javascript"> 
function getMenuIds(){ 
var idArray = new Array(); 
$("#tree").find(".jstree-checked, .jstree-undetermined ").each(function(){ 
var isChild = true; 
if($(this).find('li').length != 0){ 
idArray.push($(this).attr("id")); 
isChild = false; 
} 
if(isChild){idArray.push($(this).attr("id"));} 
}); 
//var ids=idArray.join(','); 
alert(idArray); 
//alert(ids); 
} 
function nodeEvent(desc,id){ 
$("#content").load("@{Application.codepiece()}",{"desc":desc,"id":id}); 
} 
function _callBack(d){ 
var re = [], expIds = [], attr = {}; 
jQuery.each(d, function(i){ 
var state = 'closed'; 
var data = ''; 
var onclick = ''; 
var href = ''; 
var image = ''; 
if (!d[i].leaf == '0') { 
state = null; 
var desc = d[i].decription; 
var id = d[i].id; 
image = "@{'/public/images/file.png'}" 
onclick = "nodeEvent(\"" + desc + "\"," +id + ")"; 
href = "javascript:nodeEvent(\"" + desc + "\"," + id + ");"; 
}else{ 
onclick = ""; 
//image = "@{'/public/images/folder.png'}" 
} 
re.push({ 
"attr": { 
"id": d[i].id 
}, 
"data": { 
"title": d[i].name, 
"attr": {"onClick" : onclick} 
}, 
"state": state, 
"icon": image 
}); 
}); 
return re; 
} 
$(function () { 
var ctmitems = {}; 
var isadmin = 0; 
var plugins = []; 
#{if user} 
ctmitems = {"ccp": null}; 
plugins = [ "themes", "json_data", "ui","hotkeys", "crrm", 'dnd', "search", "types", "cookies", "contextmenu", "checkbox"]; 
isadmin = 1; 
$('#userinfo').html('管理员:'+ '${session.get("user")}'); 
#{/if} 
#{else} 
$('#userinfo').html('普通用户:'+ '${session.get("user")}'); 
ctmitems = {"new": null,"ccp": null,"rename": null, "remove": null} 
plugins = [ "themes", "json_data", "ui","hotkeys", "crrm", "search", "types", "cookies", "contextmenu" ]; 
#{/else} 
var tree = $("#tree").jstree({ 
themes: { 
"theme": "apple", 
"dots" : false, 
"icons" : true 
}, 
"json_data": { 
"ajax": { 
"url": '@{Application.getData()}', 
"async": true, 
"data": function(n){ 
return { 
"id" : n.attr ? n.attr("id") : null 
}; 
}, 
"success": function(d){ 
return _callBack(d); 
} 
}, 
"progressive_render" : true 
}, 
"ui":{ 
"initially_select":["1"] 
}, 
"core" : { 
"initially_open" : ["1", "7"] 
}, 
"contextmenu": { 
"select_node":false, 
"show_at_node":true, 
"items": ctmitems 
}, 
"dnd" : { 
"drop_target" : false, 
"drag_target" : false 
}, 
"plugins" : plugins 
}) 
.bind("create.jstree", function (e, data) { 
if(data.rslt.parent.attr("id")); 
$.post( 
"@{Application.addNode()}", 
{ 
"operation" : "create_node", 
"parentID" : data.rslt.parent.attr("id"), 
"name" : data.rslt.name, 
"isleaf" : 0 
}, 
function (r) { 
if(r.status) { 
data.inst.refresh(); 
} 
else { 
$.jstree.rollback(data.rlbk); 
alert("叶子节点不能再有子节点!"); 
} 
} 
); 
}) 
.bind("createleaf.jstree", function (e, data) { 
if(data.rslt.parent.attr("id")); 
$.post( 
"@{Application.addNode()}", 
{ 
"operation" : "create_node", 
"parentID" : data.rslt.parent.attr("id"), 
"name" : data.rslt.name, 
"isleaf" : 1 
}, 
function (r) { 
if(r.status) { 
data.inst.refresh(); 
} 
else { 
$.jstree.rollback(data.rlbk); 
alert("叶子节点不能再有子节点!"); 
} 
} 
); 
}) 
.bind("move_node.jstree", function (e, data) { 
data.rslt.o.each(function (i) { 
$.ajax({ 
async : false, 
type: 'POST', 
url: "@{Application.moveNode()}", 
data : { 
"operation" : "move_node", 
"id" : this.id, 
"parentID" : data.rslt.np.attr("id"), 
"isadmin" : isadmin 
}, 
success : function (r) { 
if(!r.status) { 
data.inst.refresh(); 
} 
else { 
} 
} 
}); 
}); 
}) 
.bind("rename.jstree", function (e, data) { 
if (data.rslt.new_name == data.rslt.old_name){ 
return ; 
} 
$.post( 
"@{Application.editNode()}", 
{ 
"operation" : "rename_node", 
"id" : data.rslt.obj.attr("id"), 
"name" : data.rslt.new_name 
}, 
function (r) { 
if(!r.status) { 
data.inst.refresh(); 
}else{ 
} 
} 
); 
}) 
.bind("remove.jstree", function (e, data) { 
data.rslt.obj.each(function () { 
$.ajax({ 
async : false, 
type: 'POST', 
url: "@{Application.removeNode()}", 
data : { 
"operation" : "remove_node", 
"id" : this.id 
}, 
success : function (r) { 
if(!r.status) { 
data.inst.refresh(); 
} 
} 
}); 
}); 
}); 
}); 
</script>
Javascript 相关文章推荐
javascript 中对象的继承〔转贴〕
Jan 22 Javascript
js 判断上传文件大小及格式代码
Nov 13 Javascript
调用DOM对象的focus使文本框获得焦点
Feb 19 Javascript
jQuery-1.9.1源码分析系列(十)事件系统之事件体系结构
Nov 19 Javascript
Bootstrap CSS布局之按钮
Dec 17 Javascript
微信小程序通过保存图片分享到朋友圈功能
May 24 Javascript
D3.js实现拓扑图的示例代码
Jun 30 Javascript
JS实现点击按钮可实现编辑功能
Jul 03 Javascript
jQuery实现炫丽的3d旋转星空效果
Jul 04 jQuery
webpack4.x开发环境配置详解
Aug 04 Javascript
JS实现TITLE悬停长久显示效果完整示例
Feb 11 Javascript
JavaScript中document.activeELement焦点元素介绍
Nov 27 Javascript
JQuery之拖拽插件实现代码
Apr 14 #Javascript
jQuery创建插件的代码分析
Apr 14 #Javascript
Jquery公告滚动+AJAX后台得到数据
Apr 14 #Javascript
jquery中eq和get的区别与使用方法
Apr 14 #Javascript
基于jquery的blockui插件显示弹出层
Apr 14 #Javascript
强大的jquery插件jqeuryUI做网页对话框效果!简单
Apr 14 #Javascript
让textarea自动调整大小的js代码
Apr 12 #Javascript
You might like
MySQL授权问题总结
2007/05/06 PHP
PHP 事件机制(2)
2011/03/23 PHP
PHP命名空间namespace的定义方法详解
2017/03/29 PHP
使用WAMP搭建PHP本地开发环境
2017/05/10 PHP
php微信开发之图片回复功能
2018/06/14 PHP
laravel框架select2多选插件初始化默认选中项操作示例
2020/02/18 PHP
Thinkphp5+Redis实现商品秒杀代码实例讲解
2020/12/29 PHP
javascript showModalDialog,open取得父窗口的方法
2010/03/10 Javascript
firebug的一个有趣现象介绍
2011/11/30 Javascript
jQuery实现鼠标滑过Div层背景变颜色的方法
2015/02/17 Javascript
谈谈我对JavaScript原型和闭包系列理解(随手笔记8)
2015/12/24 Javascript
Node.js常用工具之util模块
2017/03/09 Javascript
Javascript之图片的延迟加载的实例详解
2017/07/24 Javascript
javascript实现最长公共子序列实例代码
2018/02/05 Javascript
vue基础之事件v-onclick=&quot;函数&quot;用法示例
2019/03/11 Javascript
layui的面包屑或者表单不显示的解决方法
2019/09/05 Javascript
js实现一款简单踩白块小游戏(曾经很火)
2019/12/02 Javascript
file-loader打包图片文件时路径错误输出为[object-module]的解决方法
2020/01/03 Javascript
js实现坦克大战游戏
2020/02/24 Javascript
使用Node.js实现base64和png文件相互转换的方法
2020/03/11 Javascript
[03:04]DOTA2英雄基础教程 影魔
2013/12/11 DOTA
[05:13]TI4 中国战队 机场出征!!
2014/07/07 DOTA
Tornado Web Server框架编写简易Python服务器
2018/07/28 Python
浅析Python四种数据类型
2018/09/26 Python
Python SMTP发送邮件遇到的一些问题及解决办法
2018/10/24 Python
Pytorch 实现sobel算子的卷积操作详解
2020/01/10 Python
天猫超市:阿里巴巴打造的网上超市
2016/11/02 全球购物
华美博弈C/VC工程师笔试试题
2012/07/16 面试题
简述网络文件系统NFS,并说明其作用
2016/10/19 面试题
最新自我评价范文
2013/11/16 职场文书
制药工程专业个人求职自荐信
2014/01/25 职场文书
优秀交警事迹材料
2014/01/26 职场文书
营销总监岗位职责范本
2014/02/26 职场文书
入党自荐书范文
2014/03/09 职场文书
羽毛球比赛策划方案
2014/06/13 职场文书
2016简历自荐信优秀范文
2016/01/29 职场文书