Can I display/create jsTree on button click?

笑着哭i 提交于 2019-12-12 11:21:39

问题


I have a div id="result" that I use to display results of users actions. I put there plain text and html too. After each user action this div is overwritten with new results.

Now I want to display inside this div an instance of jsTree I have working jsTree that loads nicely on page load but it doesn't load after <div id="tree"></div> is inserted inside my result div.

I tried two lines below but none worked.

 $("#tree").jstree("loaded");  
 $("#tree").jstree("refresh"); 

Any idea how to make the jsTree to be displayed a after button click? Please play on jsfiddle where you can see one tree already and I want another to be displayed after clicking the button. I am after the tree not the text.

I think I need to reload or refresh the tree after I place jstree div inside html.


回答1:


tree2 was not created in the first place since <div id="tree2"> was not there when you call '$("#tree2").jstree'.

So when you press the button, first tree is correctly refreshed but tree2 is not shown for the reason above.

At button pressed, you have to recreate tree2 by calling '$("#tree2").jstree', or you can just hide <div id="tree2"> when you call $("#tree2").jstree. I just updated your fiddle ( http://jsfiddle.net/fmn6g/10/) so you can try.




回答2:


To make it happen on a Button Click, just do this:

<script type="text/javascript">
$(document).ready(function() {
$('#button-id').click(function() {
 $("#tree").jstree("loaded");  
 $("#tree").jstree("refresh"); 
});
});
</script>

Remember to change button-id to the actual id of the button.

Hope this helps.



来源:https://stackoverflow.com/questions/7117538/can-i-display-create-jstree-on-button-click

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!