jquery下jstree簡單應(yīng)用 - v1.0
更新時(shí)間:2011年04月14日 22:01:08 作者:
jquery下jstree簡單應(yīng)用,學(xué)習(xí)jstree的朋友可以參考下。
第一篇文章,具體使用也過去很長時(shí)間了,直接貼碼:
1.代碼中使用json數(shù)據(jù)格式(直接在頁面中組裝成的,并非后臺(tái)組裝,具體方法:function _callBack(d)) 2.提供右鍵菜單及功能實(shí)現(xiàn)
3.具有checkbox,提供獲取選中節(jié)點(diǎn)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的學(xué)習(xí)及應(yīng)用");
$("#content").html("js(jstree)和play!framework的學(xué)習(xí)及應(yīng)用");
</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("葉子節(jié)點(diǎn)不能再有子節(jié)點(diǎn)!");
}
}
);
})
.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("葉子節(jié)點(diǎn)不能再有子節(jié)點(diǎn)!");
}
}
);
})
.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>
1.代碼中使用json數(shù)據(jù)格式(直接在頁面中組裝成的,并非后臺(tái)組裝,具體方法:function _callBack(d)) 2.提供右鍵菜單及功能實(shí)現(xiàn)
3.具有checkbox,提供獲取選中節(jié)點(diǎn)ID方法:function getMenuIds()
復(fù)制代碼 代碼如下:
<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的學(xué)習(xí)及應(yīng)用");
$("#content").html("js(jstree)和play!framework的學(xué)習(xí)及應(yīng)用");
</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("葉子節(jié)點(diǎn)不能再有子節(jié)點(diǎn)!");
}
}
);
})
.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("葉子節(jié)點(diǎn)不能再有子節(jié)點(diǎn)!");
}
}
);
})
.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>
您可能感興趣的文章:
- 基于jstree使用AJAX請求獲取數(shù)據(jù)形成樹
- 基于jstree使用JSON數(shù)據(jù)組裝成樹
- jstree創(chuàng)建無限分級樹的方法【基于ajax動(dòng)態(tài)創(chuàng)建子節(jié)點(diǎn)】
- 基于BootStrap Metronic開發(fā)框架經(jīng)驗(yàn)小結(jié)【二】列表分頁處理和插件JSTree的使用
- jsTree 基于JQuery的排序節(jié)點(diǎn) Bug
- jquery.jstree 增加節(jié)點(diǎn)的雙擊事件代碼
- 基于jsTree的無限級樹JSON數(shù)據(jù)的轉(zhuǎn)換代碼
- JQery jstree 大數(shù)據(jù)量問題解決方法
- jsTree樹控件(基于jQuery, 超強(qiáng)悍)[推薦]
- jsTree事件和交互以及插件plugins詳解
相關(guān)文章
jquery validate和jquery form 插件組合實(shí)現(xiàn)驗(yàn)證表單后AJAX提交
在ajax流行的時(shí)代,好像很少能看見傳統(tǒng)的同步提交表單方式了,是啊我們當(dāng)然要用更加給力的AJAX來實(shí)現(xiàn)異步無刷新提交表單,好了開始今天的jQuery之旅吧,今天我們來利用jquery.validate和jquery.form 插件組合實(shí)現(xiàn)驗(yàn)證表單后AJAX提交 ,需要的朋友可以參考下2015-08-08jQuery選擇器源碼解讀(七):elementMatcher函數(shù)
這篇文章主要介紹了jQuery選擇器源碼解讀(七):elementMatcher函數(shù),本文講解了源碼、功能、參數(shù)、返回函數(shù) 等內(nèi)容,需要的朋友可以參考下2015-03-03jquery表單驗(yàn)證插件(jquery.validate.js)的3種使用方式
這篇文章主要介紹了jquery表單驗(yàn)證插件(jquery.validate.js)的3種使用方式,本文用詳細(xì)的代碼實(shí)例講解jquery表單驗(yàn)證插件的使用,需要的朋友可以參考下2015-03-03通過jquery獲取上傳文件名稱、類型和大小的實(shí)現(xiàn)代碼
這篇文章主要介紹了在文件上傳到服務(wù)器之前,我們可以通過jquery來獲取上傳文件的名稱,類型和尺寸大小,需要的朋友可以參考下2018-04-04jquery和js實(shí)現(xiàn)對div的隱藏和顯示方法
jquery和js分別可以實(shí)現(xiàn)對div的隱藏和顯示,方法也不一樣,jquery的show、show;js的hidden、visible2014-09-09Jquery異步請求數(shù)據(jù)實(shí)例代碼
Jquery異步請求數(shù)據(jù)實(shí)例代碼,需要的朋友可以參考下。2011-12-12jQuery實(shí)現(xiàn)動(dòng)畫效果的簡單實(shí)例
本篇文章主要是對jQuery實(shí)現(xiàn)動(dòng)畫效果的簡單實(shí)例進(jìn)行了介紹,需要的朋友可以過來參考下,希望對大家有所幫助2014-01-01