jsTree 是一个jquery 插件, 提供交互式树.它是完全免费的,开源的,并根据MIT许可进行分发。jsTree易于扩展,可定义和配置,它支持HTML和JSON数据源以及AJAX加载。(https://www.jstree.com.cn/ 中文网).可实现如下效果:
右键单击节点可以创建,删除,修改,移动节点,默认是英文的. 可以通过配置
.jstree.defaults.contextmenu.items来定制.(请记住,默认情况下,禁止对树的所有修改,包括创建,重命名,移动,删除,要启用它们,必须将core.checkcallback设置为true).jsTree官网api文档对每个参数都有简单说明,具体的配置可以查看jsTree源码,例如对.jstree.defaults.contextmenu.items来定制. (请记住,默认情况下,禁止对树的所有修改,包括创建,重命名,移动,删除, 要启用它们,必须将core.check_callback设置为true).
jsTree官网api文档对每个参数都有简单说明, 具体的配置可以查看jsTree源码,
例如对.jstree.defaults.contextmenu.items来定制.(请记住,默认情况下,禁止对树的所有修改,包括创建,重命名,移动,删除,要启用它们,必须将core.checkcallback设置为true).jsTree官网api文档对每个参数都有简单说明,具体的配置可以查看jsTree源码,例如对.jstree.defaults.contextmenu.items的配置, 源码如下:
$.jstree.defaults.contextmenu = {
/**
* a boolean indicating if the node should be selected when the context menu is invoked on it. Defaults to `true`.
* @name $.jstree.defaults.contextmenu.select_node
* @plugin contextmenu
*/
select_node : true,
/**
* a boolean indicating if the menu should be shown aligned with the node. Defaults to `true`, otherwise the mouse coordinates are used.
* @name $.jstree.defaults.contextmenu.show_at_node
* @plugin contextmenu
*/
show_at_node : true,
/**
* an object of actions, or a function that accepts a node and a callback function and calls the callback function with an object of actions available for that node (you can also return the items too).
*
* Each action consists of a key (a unique name) and a value which is an object with the following properties (only label and action are required). Once a menu item is activated the `action` function will be invoked with an object containing the following keys: item - the contextmenu item definition as seen below, reference - the DOM node that was used (the tree node), element - the contextmenu DOM element, position - an object with x/y properties indicating the position of the menu.
*
* * `separator_before` - a boolean indicating if there should be a separator before this item
* * `separator_after` - a boolean indicating if there should be a separator after this item
* * `_disabled` - a boolean indicating if this action should be disabled
* * `label` - a string - the name of the action (could be a function returning a string)
* * `title` - a string - an optional tooltip for the item
* * `action` - a function to be executed if this item is chosen, the function will receive
* * `icon` - a string, can be a path to an icon or a className, if using an image that is in the current directory use a `./` prefix, otherwise it will be detected as a class
* * `shortcut` - keyCode which will trigger the action if the menu is open (for example `113` for rename, which equals F2)
* * `shortcut_label` - shortcut label (like for example `F2` for rename)
* * `submenu` - an object with the same structure as $.jstree.defaults.contextmenu.items which can be used to create a submenu - each key will be rendered as a separate option in a submenu that will appear once the current item is hovered
*
* @name $.jstree.defaults.contextmenu.items
* @plugin contextmenu
*/
items : function (o, cb) { // Could be an object directly
return {
"create" : {
"separator_before" : false,
"separator_after" : true,
"_disabled" : false, //(this.check("create_node", data.reference, {}, "last")),
"label" : "Create",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
inst.create_node(obj, {}, "last", function (new_node) {
try {
inst.edit(new_node);
} catch (ex) {
setTimeout(function () { inst.edit(new_node); },0);
}
});
}
},
"rename" : {
"separator_before" : false,
"separator_after" : false,
"_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
"label" : "Rename",
/*!
"shortcut" : 113,
"shortcut_label" : 'F2',
"icon" : "glyphicon glyphicon-leaf",
*/
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
inst.edit(obj);
}
},
"remove" : {
"separator_before" : false,
"icon" : false,
"separator_after" : false,
"_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
"label" : "Delete",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.delete_node(inst.get_selected());
}
else {
inst.delete_node(obj);
}
}
},
"ccp" : {
"separator_before" : true,
"icon" : false,
"separator_after" : false,
"label" : "Edit",
"action" : false,
"submenu" : {
"cut" : {
"separator_before" : false,
"separator_after" : false,
"label" : "Cut",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.cut(inst.get_top_selected());
}
else {
inst.cut(obj);
}
}
},
"copy" : {
"separator_before" : false,
"icon" : false,
"separator_after" : false,
"label" : "Copy",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.copy(inst.get_top_selected());
}
else {
inst.copy(obj);
}
}
},
"paste" : {
"separator_before" : false,
"icon" : false,
"_disabled" : function (data) {
return !$.jstree.reference(data.reference).can_paste();
},
"separator_after" : false,
"label" : "Paste",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
inst.paste(obj);
}
}
}
}
};
}
};
这里的create,rename,remove,ccp分别对应创建,重命名,删除,拖动, 可以按自己的需求自定义,
$("#list").jstree({
'core' : {
'check_callback': true,
'multiple' : false,
'data' : {
'url' : '/admin/category/tree?page='+ <?php echo $page; ?>+'&per-page='+<?php echo $perpage; ?>,
'data' : function(node){
return {'id': node.categoryid}
}
},
'themes': {
'stripes': true,
'variant': "large"
}
},
"contextmenu": {
select_node: false,
show_at_node: true,
items: {
"新建分类": {
"label": "添加分类",
"icon": "glyphicon glyphicon-plus",
"action": function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
inst.create_node(obj, {}, "last", function (new_node) {
try {
new_node.text = "新建分类";
inst.edit(new_node);
} catch (ex) {
setTimeout(function () {
inst.edit(new_node);
}, 0);
}
});
}
},
"修改分类":{
"separator_before" : false,
"separator_after" : false,
"_disabled" : false, //(this.check("rename_node", data.reference, this.get_parent(data.reference), "")),
"label" : "修改分类",
"shortcut_label" : 'F2',
"icon" : "glyphicon glyphicon-leaf",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
inst.edit(obj);
}
},
"删除分类":{
"separator_before" : false,
"icon" : false,
"separator_after" : false,
"_disabled" : false, //(this.check("delete_node", data.reference, this.get_parent(data.reference), "")),
"label" : "删除分类",
"icon" :"glyphicon glyphicon-remove",
"action" : function (data) {
var inst = $.jstree.reference(data.reference),
obj = inst.get_node(data.reference);
if(inst.is_selected(obj)) {
inst.delete_node(inst.get_selected());
}else {
inst.delete_node(obj);
}
}
}
},
},
'plugins' : [ 'contextmenu','search','state','types','wholerow']
});
实现效果:
对于jstree其他更多功能,比如搜索,事件等, 如果api文档没有详细说明如何实现,都应该去查看源码,分析源码,然后实现功能.