개발이야기/jQuery

[jQuery] jstree checkbox 사용법, 예제, 이벤트 핸들링

후린개발자 2023. 7. 18.
반응형

아래 코드는 jsTree를 사용하여 트리 구조를 생성하고, 다양한 이벤트를 활용하여 트리 노드의 상태 변경 및 핸들링을 수행하고 있습니다.
jsTree를 사용하기 위해서는 css와 javascript 라이브러리를 가져옵니다.
트리구조는 <div id="jstree"> 내부에 <ul>과 <li> 태그를 사용해 각각의 id 속성을 가지고 있습니다.

$('#jstree').on('이벤트명.jstree', function (e, data) { ... }) 를 통해 이벤트마다 콘솔 로그에 정보를 출력합니다. 변경된 노드 ID, 노드가 열렸을 때, 닫혔을 때, 선택되었을 때, 선택 해제되었을 때 등의 이벤트를 콘솔에서 확인할 수 있습니다.

 

<!DOCTYPE html>
<html>
<head>
    <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/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script>
</head>
<body>
    <div id="jstree">
        <ul>
            <li id="root">Root node
                <ul>
                    <li class="group" id="groupA">A 그룹
                        <ul>
                            <li id="child1">Child node 1</li>
                            <li id="child2">Child node 2</li>
                        </ul>
                    </li>
                    <li class="group" id="groupB">B 그룹
                        <ul>
                            <li id="child3">Child node 3</li>
                            <li id="child4">Child node 4</li>
                        </ul>
                    </li>
                </ul>
            </li>
        </ul>
    </div>

    <button id="confirmationBtn">선택확인</button>

    <script>
        $(function () {
            $('#jstree').jstree({
                'plugins': ['checkbox']
            });

            $('#confirmationBtn').click(function () {
                var selectedNodes = $('#jstree').jstree('get_selected', true);
                var selectedNodeIds = selectedNodes.map(function (node) {
                    return node.id;
                });
                alert("선택된 항목 ID: " + selectedNodeIds.join(", "));
            });

            // changed.jstree 이벤트 핸들링
            $('#jstree').on('changed.jstree', function (e, data) {
                console.log('변경된 노드 ID:', data.selected);
            });

            // open_node.jstree 이벤트 핸들링
            $('#jstree').on('open_node.jstree', function (e, data) {
                console.log('노드가 열렸습니다:', data.node.text);
            });

            // close_node.jstree 이벤트 핸들링
            $('#jstree').on('close_node.jstree', function (e, data) {
                console.log('노드가 닫혔습니다:', data.node.text);
            });

            // select_node.jstree 이벤트 핸들링
            $('#jstree').on('select_node.jstree', function (e, data) {
                console.log('노드가 선택되었습니다:', data.node.text);
            });

            // deselect_node.jstree 이벤트 핸들링
            $('#jstree').on('deselect_node.jstree', function (e, data) {
                console.log('노드 선택이 해제되었습니다:', data.node.text);
            });
        });
    </script>
</body>
</html>

 

 

예제코드 화면

반응형

댓글

💲 추천 글