var _node = null, _all_match = 0, _current_match = 0;
$(document).ready(function() {
$('#area_setting_ou_tree').jstree({
'core' : {
'data' : {
"url" : "url",
"method" : "POST",
"dataType" : "json"
},
"multiple" : false,
"check_callback" : true,
"animation" : 0
},
'plugins': [
"sort",
"search",
"types"
],
'sort' : function(a, b) {
return this.get_type(a) === this.get_type(b) ? (this.get_text(a) > this.get_text(b) ? 1 : -1) : (this.get_type(a) >= this.get_type(b) ? 1 : -1);
},
'search' : {
'search_leaves_only' : true,
'search_callback' : function(string,node){
if(string !== '' && node.type === 'staff' && node.text.toLocaleLowerCase().match(string.toLocaleLowerCase())) {
_instance.open_all();
$('#' + node.id).addClass('jstree-search');
}else {
$('#' + node.id).removeClass('jstree-search');
}
}
},
'types' : {
"staff" : {
"icon" : "url"
},
"ou_end" : {
"icon" : "url"
},
"ou_not_end" : {
"icon" : "url"
}
}
}).on('search.jstree', function() {
var _selected_node = $('#area_setting_ou_tree ul li .jstree-search');
if(_selected_node.length === 0) return; var _instance = $('#area_setting_ou_tree').jstree(true); _all_match = _selected_node.length;
$('#all_selected').text(_all_match);
_current_match = 1;
$('#current_selected').text(_current_match); var node_id = '';
_selected_node.each(function(index, dom) {
node_id = $(this).attr('id');
if(index === 0) {
_instance.select_node(node_id);
$('#' + node_id).addClass('jstree-search');
location.hash = '#' + node_id;
}
_node.add(node_id);
});
}); var _instance = $('#area_setting_ou_tree').jstree(true);
/*
* search event for Tree
*/
var area_setting_ou_tree_to = false;
$('#area_setting_ou_tree_search').keyup(function () {
setTimeout(function() {
_all_match = 0;
_current_match = 0;
$('#current_selected').text(0);
$('#all_selected').text(0);
$('#area_set_main').html('');
_node = null;
_node = new DoubleLinkedList();
var _instance = $('#area_setting_ou_tree').jstree(true);
_instance.deselect_all();
$('#selected_message').css('visibility','visible');
$('#pre_selected').prop('disabled',false);
$('#next_selected').prop('disabled',false); if(area_setting_ou_tree_to) { clearTimeout(area_setting_ou_tree_to); }
area_setting_ou_tree_to = setTimeout(function () {
var company_department_tree_search_val = $('#area_setting_ou_tree_search').val();
$('#area_setting_ou_tree').jstree(true).search(company_department_tree_search_val);
}, 250); if($('#area_setting_ou_tree_search').val() === '') {
$('#selected_message').css('visibility','hidden');
$('#pre_selected').prop('disabled',true);
$('#next_selected').prop('disabled',true);
$('.jstree-search').removeClass('jstree-search');
$('#area_set_main').html('');
}
}, 200);
}); /*
* next step for Tree
*/
$('#next_selected').on('click', function(){
if(_node.size() === 0) return;
var _instance = $('#area_setting_ou_tree').jstree(true); // selected node is not search results
var _current_selected = null;
if(_node.find(_instance.get_selected()[0])) {
_current_selected = _node.find(_instance.get_selected()[0]);
} else {
_current_selected = _node.getHead();
_current_match = 0;
} if(_current_selected.next) {
var _temp = _current_selected.next.element;
_instance.deselect_all();
_instance.select_node(_temp);
location.hash = '#' + _temp;
showInstitutionByClickTree(_instance.get_type(_temp), _temp);
_current_match += 1;
$('#current_selected').text(_current_match);
} else {
//跳转到第一个
_current_match = 1;
var _temp = _node.getHead().next.element;
_instance.deselect_all();
_instance.select_node(_temp);
location.hash = '#' + _temp;
//showInstitutionByClickTree(_instance.get_type(_temp), _temp);
$('#current_selected').text(_current_match);
} });
/*
* previous step for Tree
*/
$('#pre_selected').on('click', function(){
if(_node.size() === 0) return;
var _instance = $('#area_setting_ou_tree').jstree(true); // selected node is not search results
var _current_selected = null;
if(!_node.find(_instance.get_selected()[0])) {
_current_selected = _node.findLast();
_current_match = _node.size();
var _temp = _current_selected.element;
_instance.deselect_all();
_instance.select_node(_temp);
location.hash = '#' + _temp;
showInstitutionByClickTree(_instance.get_type(_temp), _temp);
$('#current_selected').text(_current_match);
return;
} _current_selected = _node.find(_instance.get_selected()[0]);
if(_current_selected.previous != _node.getHead()) {
var _temp = _current_selected.previous.element;
_instance.deselect_all();
_instance.select_node(_temp);
_current_match -= 1;
$('#current_selected').text(_current_match);
} else {
//跳转到最后一个
_current_selected = _node.findLast();
_current_match = _node.size();
var _temp = _current_selected.element;
_instance.deselect_all();
_instance.select_node(_temp);
$('#current_selected').text(_current_match);
}
});
});
双链表
function Node(element){
this.element = element;
this.next = null;
this.previous = null;
} function DoubleLinkedList(){
this._head = new Node("This is Head Node.");
this._size = 0;
} DoubleLinkedList.prototype.getHead = function(){
return this._head;
}; DoubleLinkedList.prototype.isEmpty = function(){
return this._size === 0;
}; DoubleLinkedList.prototype.size = function(){
return this._size;
}; DoubleLinkedList.prototype.findLast = function(){
var currNode = this.getHead();
while(currNode.next){
currNode = currNode.next;
}
return currNode;
}; DoubleLinkedList.prototype.add = function(item){
if(item == null)
return null;
this.insert(item);
}; DoubleLinkedList.prototype.remove = function(item){
if(item) {
var node = this.find(item);
if(node == null)
return ;
if (node.next === null) {
node.previous.next = null;
node.previous = null;
} else{
node.previous.next = node.next;
node.next.previous = node.previous;
node.next = null;
node.previous = null;
}
this._size--;
}
}; DoubleLinkedList.prototype.find = function(item){
if(item == null)
return null;
var currNode = this.getHead();
while(currNode && currNode.element !== item){
currNode = currNode.next;
}
return currNode;
}; DoubleLinkedList.prototype.insert = function(newElement, item){
var newNode = new Node(newElement);
var finder = item ? this.find(item) : null;
if(!finder){
var last = this.findLast();
newNode.previous = last;
last.next = newNode;
}
else{
newNode.next = finder.next;
newNode.previous = finder;
finder.next.previous = newNode;
finder.next = newNode;
}
this._size++;
}; DoubleLinkedList.prototype.dispReverse = function(){
var currNode = this.findLast();
while(currNode != this.getHead()){
console.log(currNode.element);
currNode = currNode.previous;
}
}; DoubleLinkedList.prototype.display = function(){
var currNode = this.getHead().next;
while(currNode){
console.log(currNode.element);
currNode = currNode.next;
}
};
jstree:版本号-3.0.9
API地址:https://www.jstree.com/api/
数据:ajax后台生成
数据结构:双链表