JSFiddle - React, Tailwind, and code Playground
by Lucian Maran
HTML
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type='text/javascript' src="//dl.dropboxusercontent.com/u/43065769/tree.jquery.js"></script>
<h2>Search as you type in jqTree</h2>
<input id="searchField" placeholder="search..." />
<label> Ex: type 'child2'</label>
<div id="tree1"></div>
CSS
ol, li{list-style: none;}
label{color:gray}
a + span{color:red; font-weight:bold}
JavaScript
var data = [
{
label: 'node1',
children: [
{ label: 'chil-d1' },
{ label: 'c-hild2' }
]
},
{
label: 'c.h.i.l.d.'
},
{
label: 'node2',
children: [
{ label: '1-Child2' },
{
label: 'node3',
children: [
{
label: 'node4',
children: [
{ label: '2child22' },
{ label: 'xxx' }
]
}]
},
{ label: '3child3' },
{ label: 'child21' }
]
},
{
label: 'node5',
children: [
{ label: 'a-xx-aaa' },
{ label: '4child2' }
]
},
{
label: 'aayyyy'
}
];
$(document).ready(function () {
$('#tree1').tree({
data: data,
onCreateLi: function (node, $li) {
var searchValue = $('#searchField').val().toLowerCase();
var nodeValue = $li.find('.jqtree-title').text().toLowerCase();
if ($li.find('.jqtree-toggler').length == 0) { // child (leaf)
if (nodeValue.indexOf(searchValue) == -1) { // child mismatch
$li.find('.jqtree-title').hide(); // hide child
}
else { // child match
// show child and all parent folders, down to root
for (var i = node.parent.getLevel() ; i > 0; i--) {
$(node.parent.element).find("div").first().show();
node.parent = node.parent.parent;
}
}
}
else { // folder
$li.find('.jqtree-element').hide(); //initial hide all folders
}
}
});
// search on type
// http://stackoverflow.com/questions/10318575/jquery-search-as-you-type-with-ajax
var thread =...