jstree : jQuery ๊ธฐ๋ฐ์ผ๋ก ํธ๋ฆฌํ์์ ๊ตฌ์กฐ๋ฅผ ์ง์ํด ์น์ ์ถ๋ ฅ์ ๋์์ฃผ๋ ๋ผ์ด๋ธ๋ฌ๋ฆฌ
jstree, jquery ์คํฌ๋ฆฝํธ ๋ฐ css
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
1. node๊ฐ loaded ๋์์๋ ์คํ - $('#jstree').on('loaded.jstree', function (e, data) { });
2. node๊ฐ change ๋์์๋ ์คํ - $('#jstree').on('changed.jstree', function (e, data) { });
3. node๊ฐ select ๋์์๋ ์คํ - $('#jstree').on('select_node.jstree', function (e, data) { });
๋ชจ๋ node ์ด๊ธฐ : $('#jstree').jstree('open_all');
select_node : ํน์ ๋ ธ๋ ์ ํํ๊ธฐ - $('#jstree').jstree(true).select_node('ํน์ id');
create_node : ํน์ ๋ ธ๋ ์ถ๊ฐํ๊ธฐ - $('#jstree').jstree(true).create_node...
delete_node : ํน์ ๋ ธ๋ ์ญ์ ํ๊ธฐ - $('#jstree').jstree(true).delete_node('ํน์ id');
<!DOCTYPE html>
<html lang="ko">
<head>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<!-- jsTree theme -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/themes/default/style.min.css" />
<style>
button {
/* ์๋ต */
margin: 0;
padding: 0.5rem 1rem;
font-family: "Noto Sans KR", sans-serif;
font-size: 1rem;
font-weight: 400;
text-align: center;
text-decoration: none;
display: inline-block;
width: auto;
border: none;
border-radius: 4px;
}
#event_result{
font-size: 1rem;
font-weight: 400;
padding: 0.5rem 1rem;
}
</style>
</head>
<body>
<div id="jstree"></div>
<div id="event_result"></div>
<button id="clickBtn">click event</button>
<button id="btnCreate">create node</button>
<button id='btnDelete'>delete node</button>
<!-- jQuery -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.12.1/jquery.min.js"></script>
<!-- jsTree -->
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
<script>
$(function() {
// ํ
์คํธ ๊ฒธ json data ์์ฑ. ์ถํ ajax๋ก ๋ณ๊ฒฝ ํ ์์
// parent : ์์ node์ ์์ด๋๋ฅผ ์ง์ ํ์ฌ ๋ถ๋ชจ ๊ด๊ณ๋ฅผ์ ํจ. parent๊ฐ '#'์ธ ๊ฒฝ์ฐ๋ ์ต์์ ๋ ๋ฒจ์ ๋
ธ๋๋ฅผ ๋ํ๋.
// id : node์ id๋ฅผ ์ง์
// text : node์ ์ด๋ฆ์ ์ง์
var data = [
{ "id" : "p1", "parent" : "#", "text" : "Parent-1" },
{ "id" : "p2", "parent" : "#", "text" : "Parent-2" },
{ "id" : "p3", "parent" : "#", "text" : "Parent-3" },
{ "id" : "c1", "parent" : "p1", "text" : "Child-1" },
{ "id" : "c2", "parent" : "p1", "text" : "Child-2" },
{ "id" : "c3", "parent" : "p1", "text" : "Child-3" },
{ "id" : "c4", "parent" : "p2", "text" : "Child-4" },
{ "id" : "c5", "parent" : "p2", "text" : "Child-5" },
{ "id" : "c6", "parent" : "p3", "text" : "Child-6" },
{ "id" : "g1", "parent" : "c6", "text" : "Grand-1" },
{ "id" : "g2", "parent" : "c6", "text" : "Grand-2" },
];
// [ajax ๊ตฌํ์ผ๊ฒฝ์ฐ]
// $('#jstree').jstree({
// 'core': {
// 'data': {
// 'url': '/url',
// 'dataType': 'json'
// }
// }
// });
// ์ธ์คํด์ค๋ฅผ ์์ฑ
// jstree์ node ๋ฐ์ดํฐ ๊ฐ์ ธ์ค๊ธฐ
$('#jstree').jstree({
"core": {
"check_callback": true,
"data": data
},
}).on('create_node.jstree', function(e, data) {
console.log('saved');
});
// ์ด๋ฒคํธ๋ฅผ ์ค์
// btnCreate ๋ฒํผ ํด๋ฆญ ์
$('#btnCreate').click(function() {
console.log("btnCreate");
// ๋ถ๋ชจ ์์ ์์ฑ ํ
์คํธ parent๋ #์
$('#jstree').jstree(true).create_node('#', {"id": "p4", "text": "Parent-4"}, "last", function() {
console.log('Parent Created');
});
//์์ ์์ ์์ฑ ํ
์คํธ create_node('์์ฑํ ๋
ธ๋์ ๋ถ๋ชจ ๋
ธ๋', {์์ฑํ ๋
ธ๋์ ์ ๋ณด}, ์์)
$('#jstree').jstree(true).create_node('p3', {"id": "c7", "text": "Child 7"}, "last", function() {
console.log('Child Created');
});
});
$('#btnDelete').click(function() {
console.log('btnDelete');
// ์์์ ์์ ์์ฑํ ๋ถ๋ชจ p4 ์ญ์
$('#jstree').jstree(true).delete_node('p4');
});
// node loaded ๋์์๋ ํจ์
$('#jstree').on('loaded.jstree', function (e, data) {
console.log('loaded.jstree');
// loaded ๋์์๋ ๋ชจ๋ node ์ด๊ธฐ
//$('#jstree').jstree("open_all");
// loaded ๋์์๋ ํน์ ํ node ์ด๊ธฐ
$('#jstree').jstree('open_node', 'p1');
});
// node๊ฐ change ๋์์๋ ํจ์
$('#jstree').on('changed.jstree', function (e, data) {
console.log(data.selected);
var i,j,r = [];
for(i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
}
// div์ ํ์ฌ ์ ํ ๋ ๊ฐ์ ๋ํ๋ด๊ธฐ ์ํจ.
$('#event_result').html("* [ selected ] : " + r.join(', '));
});
// node select ๋์์๋ ํจ์
$('#jstree').on('select_node.jstree', function (e, data) {
console.log("select.jstree");
});
// clickBtn ๋ฒํผ ํด๋ฆญ ์
$('#clickBtn').on('click',function(){
console.log('clickBtn click');
// clickBtn ๋ฒํผ ํด๋ฆญ ์ ํน์ node ์ ํ๋๊ฒ
$('#jstree').jstree(true).select_node('c5'); // ๋ฒํผ ํด๋ฆญ ์ ํน์ ๋
ธ๋ ์ ํ๋๊ฒ (id๋ก)
});
});
</script>
</body>
</html>
์ฐธ๊ณ : https://www.jstree.com/
์ฐธ๊ณ : https://github.com/vakata/jstree/blob/master/demo/basic/index.html
์ฐธ๊ณ : https://www.incodom.kr/jsTree
'js' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[JavaScript] file drag and drop (0) | 2023.12.11 |
---|---|
for in ๋ฌธ ์ฌ์ฉ ์ ์ฃผ์ํ ์ (2) | 2023.12.08 |
<optgroup> select box ๊ทธ๋ฃนํ (2) | 2023.12.05 |
[jQuery] $.isNumeric() ์ซ์ ํ์ธ ํจ์ (0) | 2022.04.29 |
[JavaScript] ์ผ์ชฝ 0 ์ ๊ฑฐํ๋ ๋๊ฐ์ง ๋ฐฉ๋ฒ (0) | 2022.04.13 |