JQ Tree Show Only Selected Nodes With Their Parent On Button Click
up vote
0
down vote
favorite
I have to show only selected nodes and parents, and hide rest nodes when a button is clicked. Only nothing happens when I click the button.
The jsfiddle
http://jsfiddle.net/375emow0/
The code with commenting
start on button click
$('button[name="psychTree-selected"]').click( function(node)
get the selected nodes
nodesSelected = $('#psychTree').tree('getSelectedNodes');
create an array for shown nodes
nodeIdsToStay = ;
walk through selected nodes
nodesSelected.forEach( function(node)
var path = $('#psychTree').tree('getData');
path.forEach(function(n)
if (nodeIdsToStay.indexOf(n)===-1)
nodeIdsToStay.push(n);
)
)
hide the nodes not in the array
$('#psychTree').find('li').each( function()
if ( nodeIdsToStay.indexOf(this.id) === -1 )
$(this).hide();
)
)
I know how to hide the selected nodes but apparently non selected nodes do not have an identifiable class for me to search and hide by http://jsfiddle.net/tom1nkfr/
`$('button[name="psychTree-selected"]').click( function() `
`$('#psychTree').find('.jqtree-selected').each( function()`
`$(this).hide();`
`)`
`)`
jquery jqtree
add a comment |
up vote
0
down vote
favorite
I have to show only selected nodes and parents, and hide rest nodes when a button is clicked. Only nothing happens when I click the button.
The jsfiddle
http://jsfiddle.net/375emow0/
The code with commenting
start on button click
$('button[name="psychTree-selected"]').click( function(node)
get the selected nodes
nodesSelected = $('#psychTree').tree('getSelectedNodes');
create an array for shown nodes
nodeIdsToStay = ;
walk through selected nodes
nodesSelected.forEach( function(node)
var path = $('#psychTree').tree('getData');
path.forEach(function(n)
if (nodeIdsToStay.indexOf(n)===-1)
nodeIdsToStay.push(n);
)
)
hide the nodes not in the array
$('#psychTree').find('li').each( function()
if ( nodeIdsToStay.indexOf(this.id) === -1 )
$(this).hide();
)
)
I know how to hide the selected nodes but apparently non selected nodes do not have an identifiable class for me to search and hide by http://jsfiddle.net/tom1nkfr/
`$('button[name="psychTree-selected"]').click( function() `
`$('#psychTree').find('.jqtree-selected').each( function()`
`$(this).hide();`
`)`
`)`
jquery jqtree
Do you have a specific question/error you're trying to resolve or are you hoping someone will do your work for you?
– Snake14
Nov 10 at 20:50
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I have to show only selected nodes and parents, and hide rest nodes when a button is clicked. Only nothing happens when I click the button.
The jsfiddle
http://jsfiddle.net/375emow0/
The code with commenting
start on button click
$('button[name="psychTree-selected"]').click( function(node)
get the selected nodes
nodesSelected = $('#psychTree').tree('getSelectedNodes');
create an array for shown nodes
nodeIdsToStay = ;
walk through selected nodes
nodesSelected.forEach( function(node)
var path = $('#psychTree').tree('getData');
path.forEach(function(n)
if (nodeIdsToStay.indexOf(n)===-1)
nodeIdsToStay.push(n);
)
)
hide the nodes not in the array
$('#psychTree').find('li').each( function()
if ( nodeIdsToStay.indexOf(this.id) === -1 )
$(this).hide();
)
)
I know how to hide the selected nodes but apparently non selected nodes do not have an identifiable class for me to search and hide by http://jsfiddle.net/tom1nkfr/
`$('button[name="psychTree-selected"]').click( function() `
`$('#psychTree').find('.jqtree-selected').each( function()`
`$(this).hide();`
`)`
`)`
jquery jqtree
I have to show only selected nodes and parents, and hide rest nodes when a button is clicked. Only nothing happens when I click the button.
The jsfiddle
http://jsfiddle.net/375emow0/
The code with commenting
start on button click
$('button[name="psychTree-selected"]').click( function(node)
get the selected nodes
nodesSelected = $('#psychTree').tree('getSelectedNodes');
create an array for shown nodes
nodeIdsToStay = ;
walk through selected nodes
nodesSelected.forEach( function(node)
var path = $('#psychTree').tree('getData');
path.forEach(function(n)
if (nodeIdsToStay.indexOf(n)===-1)
nodeIdsToStay.push(n);
)
)
hide the nodes not in the array
$('#psychTree').find('li').each( function()
if ( nodeIdsToStay.indexOf(this.id) === -1 )
$(this).hide();
)
)
I know how to hide the selected nodes but apparently non selected nodes do not have an identifiable class for me to search and hide by http://jsfiddle.net/tom1nkfr/
`$('button[name="psychTree-selected"]').click( function() `
`$('#psychTree').find('.jqtree-selected').each( function()`
`$(this).hide();`
`)`
`)`
jquery jqtree
jquery jqtree
edited Nov 10 at 21:57
asked Nov 10 at 20:45
Chiemi Sayuri
32
32
Do you have a specific question/error you're trying to resolve or are you hoping someone will do your work for you?
– Snake14
Nov 10 at 20:50
add a comment |
Do you have a specific question/error you're trying to resolve or are you hoping someone will do your work for you?
– Snake14
Nov 10 at 20:50
Do you have a specific question/error you're trying to resolve or are you hoping someone will do your work for you?
– Snake14
Nov 10 at 20:50
Do you have a specific question/error you're trying to resolve or are you hoping someone will do your work for you?
– Snake14
Nov 10 at 20:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
I'd recommend a simpler approach - it looks like you're trying to find all the nodes that are selected, push those into an array
, then iterating over the entire jsTree and hiding any nodes not in the array
you created. Instead, leverage the CSS classes that jsTree is already applying to selected nodes and just hide all those that don't have it.
Simplified JS:
$('button[name="psychTree-selected"]').click(function()
$('#psychTree li.jqtree_common').each(function(index,elem)
if(!$(elem).hasClass('jqtree-folder') && !$(elem).hasClass('jqtree-selected'))
$(elem).hide();
);
)
Still triggered on your button click, but now it iterates through all the tree nodes and hides any that are (1) not selected and (2) not parent/folder nodes.
Working JSFiddle: http://jsfiddle.net/tbwjau5m/
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
I'd recommend a simpler approach - it looks like you're trying to find all the nodes that are selected, push those into an array
, then iterating over the entire jsTree and hiding any nodes not in the array
you created. Instead, leverage the CSS classes that jsTree is already applying to selected nodes and just hide all those that don't have it.
Simplified JS:
$('button[name="psychTree-selected"]').click(function()
$('#psychTree li.jqtree_common').each(function(index,elem)
if(!$(elem).hasClass('jqtree-folder') && !$(elem).hasClass('jqtree-selected'))
$(elem).hide();
);
)
Still triggered on your button click, but now it iterates through all the tree nodes and hides any that are (1) not selected and (2) not parent/folder nodes.
Working JSFiddle: http://jsfiddle.net/tbwjau5m/
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
add a comment |
up vote
0
down vote
accepted
I'd recommend a simpler approach - it looks like you're trying to find all the nodes that are selected, push those into an array
, then iterating over the entire jsTree and hiding any nodes not in the array
you created. Instead, leverage the CSS classes that jsTree is already applying to selected nodes and just hide all those that don't have it.
Simplified JS:
$('button[name="psychTree-selected"]').click(function()
$('#psychTree li.jqtree_common').each(function(index,elem)
if(!$(elem).hasClass('jqtree-folder') && !$(elem).hasClass('jqtree-selected'))
$(elem).hide();
);
)
Still triggered on your button click, but now it iterates through all the tree nodes and hides any that are (1) not selected and (2) not parent/folder nodes.
Working JSFiddle: http://jsfiddle.net/tbwjau5m/
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
add a comment |
up vote
0
down vote
accepted
up vote
0
down vote
accepted
I'd recommend a simpler approach - it looks like you're trying to find all the nodes that are selected, push those into an array
, then iterating over the entire jsTree and hiding any nodes not in the array
you created. Instead, leverage the CSS classes that jsTree is already applying to selected nodes and just hide all those that don't have it.
Simplified JS:
$('button[name="psychTree-selected"]').click(function()
$('#psychTree li.jqtree_common').each(function(index,elem)
if(!$(elem).hasClass('jqtree-folder') && !$(elem).hasClass('jqtree-selected'))
$(elem).hide();
);
)
Still triggered on your button click, but now it iterates through all the tree nodes and hides any that are (1) not selected and (2) not parent/folder nodes.
Working JSFiddle: http://jsfiddle.net/tbwjau5m/
I'd recommend a simpler approach - it looks like you're trying to find all the nodes that are selected, push those into an array
, then iterating over the entire jsTree and hiding any nodes not in the array
you created. Instead, leverage the CSS classes that jsTree is already applying to selected nodes and just hide all those that don't have it.
Simplified JS:
$('button[name="psychTree-selected"]').click(function()
$('#psychTree li.jqtree_common').each(function(index,elem)
if(!$(elem).hasClass('jqtree-folder') && !$(elem).hasClass('jqtree-selected'))
$(elem).hide();
);
)
Still triggered on your button click, but now it iterates through all the tree nodes and hides any that are (1) not selected and (2) not parent/folder nodes.
Working JSFiddle: http://jsfiddle.net/tbwjau5m/
answered Nov 11 at 0:20
Stevangelista
1,6291615
1,6291615
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
add a comment |
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
Wow thanks. I don't know why I didn't think of just using the classes. I mean I used the class to show only selected. I make things too complicated.
– Chiemi Sayuri
Nov 11 at 8:48
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
@ChiemiSayuri - great, glad to hear. If my answer suffices, please accept it.
– Stevangelista
Nov 11 at 14:57
add a comment |
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53243237%2fjq-tree-show-only-selected-nodes-with-their-parent-on-button-click%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Do you have a specific question/error you're trying to resolve or are you hoping someone will do your work for you?
– Snake14
Nov 10 at 20:50