jstree
Bala...bala...这段就不翻译了.
jstree就是个基于JQUERY的树形控件.
jsTree is jquery plugin, that provides interactive trees. It is absolutely free, open source and distributed under the MIT license.
jsTree is easily extendable, themable and configurable, it supports HTML & JSON data sources, AJAX & async callback loading.
jsTree functions properly in either box-model (content-box or border-box), can be loaded as an AMD module, and has a built in mobile theme for responsive design, that can easily be customized. It uses jQuery’s event system, so binding callbacks on various events in the tree is familiar and easy.
You also get(更多功能):
* drag & drop support(拖放)
* keyboard navigation(键盘导航)
* inline edit, create and delete(行内增、删、改)
* tri-state checkboxes(checkbox图标)
* fuzzy searching (模糊搜索)
* customizable node types(定义节点类型)
除了这个readme文件外,更多的信息请前往jstree.com 或者 the discussion group.
新手入门
引入脚本
需要引入3个脚本文件:
1. jQuery (>= 1.9.1)
2. 一个jstree主题 (默认只有一个主题)
3. jstree资源文件
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script>
<link rel="stylesheet" href="//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.3/themes/default/style.min.css" />
<script src="//cdnjs.cloudflare.com/ajax/libs/jstree/3.3.3/jstree.min.js"></script>
If you decide to host jstree yourself - the files are located in the dist
folder. You can safely ignore the dist/libs
folder.
用html标签定义树
最简单的定义方法是用写死在HTML中.你只需要用jquery选择要渲染成jstree的dom元素,然后调用 .jstree()
渲染就可以了。 也可以用 $.jstree.create(element)
的方式渲染。
<div id="container">
<ul>
<li>Root node
<ul>
<li>Child node 1</li>
<li>Child node 2</li>
</ul>
</li>
</ul>
</div>
<script>
$(function() {
$('#container').jstree();
});
</script>
jstree支持用data-*属性来设置选项:
<li data-jstree='{ "selected" : true, "opened" : true }'>Root node ...
用js数组或者JSON格式定义树
HTML定义方法比较简单, 但不够灵活,所以提供了更为灵活的JS方法:
<div id="container"></div>
<script>
$(function() {
$('#container').jstree({
'core' : {
'data' : [
{ "text" : "Root node", "children" : [
{ "text" : "Child node 1" },
{ "text" : "Child node 2" }
]
}
]
}
});
});
</script>
与前面HTML的例子不同的是, 往.jstree()
传入一个config对象参数.
注意了,jstree会试图解析core.data
定义的任何数据,用来构造成树节点。如果不设置core.data
属性,jstree就会试着解析HTML的树节点.
JSON数据的固定格式
提供的JSON数据必须符合一定的格式, 每个对