发布:2022/12/13 15:01:01作者:管理员 来源:本站 浏览次数:982
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 and AJAX 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.
Just a few of the features worth noting:
drag & drop support keyboard navigation inline edit, create and delete tri-state checkboxes fuzzy searching customizable node types
jsTree是jquery插件,它提供交互式树。它是绝对免费的,开源的,并在MIT许可证下分发。jsTree易于扩展、主题化和可配置,它支持HTML和JSON数据源以及AJAX加载。
jsTree在任何一种盒子模型(内容框或边框框)中都能正常工作,可以作为AMD模块加载,并具有内置的移动主题,用于响应设计,可以轻松定制。它使用jQuery的事件系统,因此绑定树中各种事件的回调既熟悉又容易。
只有几个值得注意的功能:
拖放支持键盘导航内联编辑、创建和删除三态复选框模糊搜索自定义节点类型
Creating an instance as described in the overview does not modify any of the defaults:
$('#jstree').jstree();
You can change the defaults for all future instances:
$.jstree.defaults.core.themes.variant = "large";
$('#jstree').jstree();
But most of the time you will want to change the defaults only for the instance that is being created. This is achieved by passing in a config object when creating the instance:
$('#jstree').jstree({
"plugins" : [ "wholerow", "checkbox" ]
});
As seen in the previous example - there is one special key in the config object named plugins. It is an array of strings, which contain the names of the plugins you want active on that instance.
All options that do not depend on a plugin are contained in a key of the config object named core, the options for each plugin are contained within a key with the same name as the plugin:
$('#jstree').jstree({
"core" : {
"themes" : {
"variant" : "large"
}
},
"checkbox" : {
"keep_selected_style" : false
},
"plugins" : [ "wholerow", "checkbox" ]
});
You can have a look at all the options and their default values. This list is what you can configure on each instance.
For example, by default the tree allows multiple selection as stated in $.jstree.defaults.core.multiple, to overwrite that make sure your config object contains "core" : { "multiple" : false }. If you have multiple overrides for the same key (like "core" here), group them:
$("#jstree").jstree({
"core" : {
"multiple" : false,
"animation" : 0
}
});
Basic markup
jsTree can turn a regular unordered list into a tree. The minimal required markup is a <ul> node with some nested <li> nodes with some text inside.
You should have a container wrapping the <ul> and create the instance on that container. Like so:
$('#html1').jstree();.
<div id="html1">
<ul>
<li>Root node 1</li>
<li>Root node 2</li>
</ul>
</div>
Root node 1
Root node 2
Nodes with children
To create a node with child nodes simpy nest an <ul>.
Internally jstree converts the text to a link, so if there already is a link in the markup jstree won't mind. Like Child node 2.
Clicking on the link however will not direct the user to a new page, to do that - intercept the changed.jstree event and act accordingly.
Keep reading for the section on handling events.
<div id="html1">
<ul>
<li>Root node 1
<ul>
<li>Child node 1</li>
<li><a href="#">Child node 2</a></li>
</ul>
</li>
</ul>
</div>
Root node 1
Setting initial state with classes
To make a node initially selected you can set the jstree-clicked class on the <a> element.
Similarly you can set the jstree-open class on any <li> element to make it initially extended, so that its children are visible.
It is a good idea to give your nodes unique IDs by adding the id attribute to any <li> element. This will be useful if you need to sync with a backend as you will get the ID back in any events jstree triggers.
…
<li class="jstree-open" id="node_1">Root
<ul>
<li>
<a href="#" class="jstree-clicked">Child</a>
</li>
</ul>
</li>
…
Root
Child
Setting initial state with data attribute
You can also set the state on a node using a data-jstree attribute.
You can use any combination of the following: opened, selected, disabled, icon.
Specifying an address (anything containing a /) will display that image as the node icon. Using a string will apply that class to the <i> element that is used to represent the icon.
For example if you are using Twitter Bootstrap you can use "icon" : "glyphicon glyphicon-leaf" to display a leaf icon.
<li data-jstree='{"opened":true,"selected":true}'>Root
<ul>
<li data-jstree='{"disabled":true}'>Child</li>
<li data-jstree='{"icon":"//jstree.com/tree.png"}'>
Child</li>
<li data-jstree='{"icon":"glyphicon glyphicon-leaf"}'>
Child</li>
</ul>
</li>
Root
Child
Child
Child
Loading with AJAX
You can also use AJAX to populate the tree with HTML your server returns. The format remains the same as the above, the only difference is that the HTML is not inside the container, but returned from the server.
To take advantage of this option you need to use the $.jstree.defaults.core.data config option.
Just use a standard jQuery-like AJAX config and jstree will automatically make an AJAX request populate the tree with the response.
Add a class of jstree-closed to any LI node you return and do not nest an UL node and jstree will make another AJAX call as soon as the user opens this node.
In addition to the standard jQuery ajax options here you can supply functions for data and url, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used as URL and data respectively.
$('#tree').jstree({
'core' : {
'data' : {
'url' : 'ajax_nodes.html',
'data' : function (node) {
return { 'id' : node.id };
}
}
}
});
// Example response:
<ul>
<li>Node 1</li>
<li class="jstree-closed">Node 2</li>
</ul>
Node 1
Node 2
The format
jsTree needs a specific format to work with JSON. In the standard syntax no fields are required - pass only what you need. Keep in mind you will be able to access any additional properties you specify - jsTree won't touch them and you will be able to use them later on (using the original property on each node).
To change the icon of the node use the icon property. Specifying a string containing a / will display that image as the node icon. Using any other string will apply that class to the <i> element that is used to represent the icon. You can use boolean false to make jsTree render the node with no icon.
You can set the state on a node using the state property. Use any combination of the following: opened, selected, disabled.
Both li_attr and a_attr are passed directly to jQuery's attr function.
When using AJAX set children to boolean true and jsTree will render the node as closed and make an additional request for that node when the user opens it.
Any nested children should either be objects following the same format, or plain strings (in which case the string is used for the node's text and everything else is autogenerated).
// Expected format of the node (there are no required fields)
{
id : "string" // will be autogenerated if omitted
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
children : [] // array of strings or objects
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
Alternative JSON format
If you do not want to use the nested children approach, you can use the alternative syntax where each node object has two required fields: id & parent and no children property (everything else remains the same).
jsTree will automatically build the hierarchy. To indicate a node should be a root node set its parent property to "#".
This should be used mainly when you render the whole tree at once and is useful when data is stored in a database using adjacency.
// Alternative format of the node (id & parent are required)
{
id : "string" // required
parent : "string" // required
text : "string" // node text
icon : "string" // string for custom
state : {
opened : boolean // is the node open
disabled : boolean // is the node disabled
selected : boolean // is the node selected
},
li_attr : {} // attributes for the generated LI node
a_attr : {} // attributes for the generated A node
}
Using JSON
To populate the tree with a JSON object you need to use the $.jstree.defaults.core.data config option.
The expected format is an array of nodes, where each node should be an object as described above or a simple string (in which case the string is used for the node's text property and everything else is autogenerated). Any nested nodes are supplied in the same manner in the children property of their parent.
$('#using_json').jstree({ 'core' : {
'data' : [
'Simple root node',
{
'text' : 'Root node 2',
'state' : {
'opened' : true,
'selected' : true
},
'children' : [
{ 'text' : 'Child 1' },
'Child 2'
]
}
]
} });
Simple root node
Root node 2
Child 1
Child 2
Using the alternative JSON format
$('#using_json_2').jstree({ 'core' : {
'data' : [
{ "id" : "ajson1", "parent" : "#", "text" : "Simple root node" },
{ "id" : "ajson2", "parent" : "#", "text" : "Root node 2" },
{ "id" : "ajson3", "parent" : "ajson2", "text" : "Child 1" },
{ "id" : "ajson4", "parent" : "ajson2", "text" : "Child 2" },
]
} });
Simple root node
Root node 2
Child 1
Child 2
Using AJAX
You can also use AJAX to populate the tree with JSON your server returns. The format remains the same as the above, the only difference is that the JSON is not inside the config object, but returned from the server.
To take advantage of this option you need to use the $.jstree.defaults.core.data config option.
Just use a standard jQuery-like AJAX config and jstree will automatically make an AJAX request populate the tree with the response.
In addition to the standard jQuery ajax options here you can supply functions for data and url, the functions will be run in the current instance's scope and a param will be passed indicating which node is being loaded, the return value of those functions will be used as URL and data respectively.
If you do not return correct json headers from the server, at least set the dataType jQuery AJAX option to "json".
$('#tree').jstree({
'core' : {
'data' : {
'url' : function (node) {
return node.id === '#' ?
'ajax_roots.json' :
'ajax_children.json';
},
'data' : function (node) {
return { 'id' : node.id };
}
}
});
Root 1
Root 2
Using a function
You can supply a function too. That function will receive two arguments - the node being loaded and a callback function to call with the children for that node once you are ready.
$('#tree').jstree({
'core' : {
'data' : function (obj, cb) {
cb.call(this,
['Root 1', 'Root 2']);
}
}});
jsTree triggers various events on the container. You can review the list of all events to know what to listen for.
To get more information about the event inspect its data argument.
In most cases where a node is involved you will get the whole node object passed in. If you get an ID string somewhere and want to inspect the node just use .get_node(). The internal node object is very similar to the JSON format used for loading, but has a few extra properties, which might be useful: children is an array of all the IDs of the node's immediate children, children_d is an array of all the IDs of the node's descendants, parent is the IDs of the node's parent and parents is an array of all the IDs of the node's ancestors.
$('#jstree')
// listen for event
.on('changed.jstree', function (e, data) {
var i, j, r = [];
for(i = 0, j = data.selected.length; i < j; i++) {
r.push(data.instance.get_node(data.selected[i]).text);
}
$('#event_result').html('Selected: ' + r.join(', '));
})
// create the instance
.jstree();
Root 1
Child 1
Child 2
Root 2
Child 3
Child 4
Selected:
Root 1
To invoke a method on an instance you must obtain a reference of the instance and invoke the method. The example shows how to obtain a reference and invoke a method.
Check the API for a list of available methods.
// 3 ways of doing the same thing
$('#jstree').jstree(true)
.select_node('mn1');
$('#jstree')
.jstree('select_node', 'mn2');
$.jstree.reference('#jstree')
.select_node('mn3');
Node 1
Node 2
Node 3
© Copyright 2014 - 2024 柏港建站平台 ejk5.com. 渝ICP备16000791号-4