2016-08-24 2 views
4

나는이 나무부모 노드의 모든 중첩 된 자식 노드를 찾는 방법은 무엇입니까?

var data1 = [{ 
    "id": "W", 
    "text": "World", 
    "state": { "opened": true }, 
    "children": [{"text": "Asia"}, 
       {"text": "Africa"}, 
       {"text": "Europe", 
       "state": { "opened": false }, 
       "children": [ "France","Germany","UK" ] 
    }] 
}]; 

$('#data').jstree({ 
    core: { 
     data: data1, 
     check_callback: false 
    }, 
    checkbox: {  

     whole_node : false, 
     tie_selection : false 
    }, 
    plugins: ['checkbox','search'] 
}) 

내가 원하는 것은 부모 노드의 모든 중첩 또는 깊은 자식 노드를 얻는 것입니다이 JSTree

enter image description here

하고 다음 코드를 가지고있다. 어떻게해야합니까?

+0

당신은 데이터 1 '처럼이 얻을 수 없습니다 [0]. 이드,'? – abpatil

+0

아니요. 상위 노드이므로 World를 선택한 경우 중첩 된 자식을 포함하여 6 개의 하위가 있으므로 모든 하위 목록 또는 길이를 얻으려면 어떻게해야합니까? – legend

답변

1

내가 원하는 것은 부모 노드의 모든 중첩 된 또는 깊은 자식 노드를 얻는 것입니다. 어떻게해야합니까?

시작할 시작 노드가 있어야합니다. 현재 선택된 노드를 얻기 위해 "select_node.jstree"이벤트를 수신한다고 가정 해 봅시다.

찾고 계신 주요 기능은 .get_children_dom(node) and .is_leaf(node)입니다.

전체 예는 다음과 같습니다 모든 기능

.on('check_node.jstree', function(e, obj) { 
    var currentNode = obj.node; 
    $('#data').jstree(true).open_all(currentNode); 
    var allChildren = $('#data').jstree(true).get_children_dom(currentNode); 
    var result = [currentNode.text]; 
    allChildren.find('li').andSelf().each(function(index, element) { 
     if ($('#data').jstree(true).is_leaf(element)) { 
      result.push(element.textContent); 
     } else { 
      var nod = $('#data').jstree(true).get_node(element); 
      result.push(nod.text); 
     } 
    }); 
    console.log(result.join(', ')); 
}); 

전체 조각은 다음과 같습니다

var data1 = [{ 
 
    "id": "W", 
 
    "text": "World", 
 
    "state": {"opened": true}, 
 
    "children": [{"text": "Asia"}, 
 
       {"text": "Africa"}, 
 
       { 
 
       "text": "Europe", 
 
       "state": {"opened": false}, 
 
       "children": ["France", "Germany", "UK"] 
 
       }] 
 
}]; 
 

 
$(function() { 
 
    $('#data').jstree({ 
 
    core: { 
 
     data: data1, 
 
     check_callback: false 
 
    }, 
 
    checkbox: { 
 

 
     whole_node: false, 
 
     tie_selection: false 
 
    }, 
 
    plugins: ['checkbox', 'search'] 
 
    }).on('check_node.jstree.jstree', function(e, obj) { 
 
    var currentNode = obj.node; 
 
    $('#data').jstree(true).open_all(currentNode); 
 
    var allChildren = $('#data').jstree(true).get_children_dom(currentNode); 
 
    var result = [currentNode.text]; 
 
    allChildren.find('li').andSelf().each(function(index, element) { 
 
     if ($('#data').jstree(true).is_leaf(element)) { 
 
     result.push(element.textContent); 
 
     } else { 
 
     var nod = $('#data').jstree(true).get_node(element); 
 
     result.push(nod.text); 
 
     } 
 
    }); 
 
    console.log(result.join(', ')); 
 
    }); 
 

 
    $('#btnClose').on('click', function(e) { 
 
    $('#data').jstree(true).close_all(); 
 
    }); 
 

 
    var to = false; 
 
    $('#search').on('input', function(e) { 
 
    if (to) { clearTimeout(to); } 
 
    to = setTimeout(function() { 
 
     var v = $('#search').val(); 
 
     $('#data').jstree(true).search(v); 
 
    }, 250); 
 
    }); 
 

 
    $('#btnCheckAll').on('click', function(e) { 
 
    $('#data').jstree(true).check_all(); 
 
    }); 
 

 
    $('#btnUnCheckAll').on('click', function(e) { 
 
    $('#data').jstree(true).uncheck_all(); 
 
    }); 
 
});
button { 
 
    background-color: transparent; 
 
    color: red; 
 
    border-style: none; 
 
}
<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/1.12.1/jquery.min.js"></script> 
 
<script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.2.1/jstree.min.js"></script> 
 

 

 
<label form="search">Products: </label> 
 
<input type="text" value="" id="search" placeholder="Search products"> 
 
<div id="divPanel"> 
 
    <button id="btnCheckAll">Check All</button><button id="btnUnCheckAll">UnCheck All</button><button id="btnClose">Close</button> 
 
    <div id="data"> 
 

 
     <ul> 
 
      <li>Root node 1 
 
       <ul> 
 
        <li id="child_node_1">Child node 1</li> 
 
        <li>Child node 2</li> 
 
       </ul> 
 
      </li> 
 
      <li>Root node 2</li> 
 
     </ul> 
 
    </div> 
 
</div>

+0

도움을 주셔서 감사합니다. 그러나 'select_node'대신 'check_node'를 사용하는 대신 코드에 약간의 비틀기가 있습니다. 노드가 아닌 확인란을 클릭하면 작업을 수행 할 수 있습니다. 그렇다면 특정 노드를 클릭 할 때 노드의 하위 노드와 하위 노드를 모두 얻는 방법 – legend

+0

@code_legend 'select_node'대신 'check_node'를 사용하여 스 니펫을 업데이트했습니다. 노드를 선택 (선택 취소하지 않음)하면 결과 배열에 all (현재 노드 (하위) 및 하위 노드)의 텍스트가 포함됩니다. 스 니펫을 실행하고 스 니펫 콘솔에서 메시지를 확인하십시오. 문제가 발생하면 알려주십시오. – gaetanoM

+0

감사합니다. 작동하는 이유는 'check_node.jstree.jstree'에서 .jstree를 두 번 사용하는 이유는 무엇입니까? – legend