gpt4 book ai didi

javascript - 如何强制重新排序jsTree?

转载 作者:行者123 更新时间:2023-12-02 23:07:04 25 4
gpt4 key购买 nike

如何强制树重新排序?我添加了菜单来更改排序顺序 ASC/DESC:

"Sort": {
"separator_before": false,
"separator_after": true,
"label": "Sort",
"action": false,
"submenu": {
"Ascending": {
"seperator_before": false,
"seperator_after": false,
"label": "Ascending",
action: function (obj) {
setSortFunction('asc');
var selector = '#treeview';
var root = $(selector).jstree(true).get_node("1");//get_node("root");
$(selector).jstree(true).sort(root, true);
$(selector).jstree(true).redraw_node(root, true);
}
},
"Descending": {
"seperator_before": false,
"seperator_after": false,
"label": "Descending",
action: function (obj) {
setSortFunction('desc');
var selector = '#treeview';
var root = $(selector).jstree(true).get_node("1");//get_node("root");
$(selector).jstree(true).sort(root, true);
$(selector).jstree(true).redraw_node(root, true);
}
}
}

它更改了升序/降序顺序,但我需要使用新顺序对现有树重新排序。我尝试按照上面的代码进行操作,但它不起作用。怎么了?

最佳答案

var sortType = "asc";



$(document).ready(function() {

$("#test").on("changed.jstree", function(e, data) {

}).jstree({
plugins: ['checkbox', 'changed', 'contextmenu', 'sort'],
contextmenu: {
select_node: false,
items: contextMenu
},
checkbox: {
three_state: false
},
sort : function(a, b) {
a1 = this.get_node(a);
b1 = this.get_node(b);
if (sortType === "asc"){
return (a1.text > b1.text) ? 1 : -1;
} else {
return (a1.text > b1.text) ? -1 : 1;
}},
core: {
data: [{
id: "padre1",
parent: "#",
text: "Padre 1",
icon: 'fa fa-star text-warning'
}, {
id: "padre2",
parent: "#",
text: "Padre 2"
}, {
id: "id3",
parent: "padre1",
text: "Figlio 1 di padre 1"
}, {
id: "id4",
parent: "padre1",
text: "Figlio 2 di padre 1"
}, {
id: "id5",
parent: "padre2",
text: "Figlio 1 di padre 2"
}, {
id: "id6",
parent: "id5",
text: "Figlio 1 di figlio 1 di padre 2"
}, {
id: "id7",
parent: "id5",
text: "Figlio 2 di figlio 1 di padre 2"
}, {
id: "id8",
parent: "id5",
text: "Figlio 3 di figlio 1 di padre 2"
}, {
id: "id9",
parent: "#",
text: "Figlio 3 di figlio 1 di padre 2"
}]
}
});
});

function contextMenu(node) {
var items = {};
items.selezionaTutti = {
label: 'Seleziona tutti',
icon: 'fa fa-check-square',
action: function(questo) {
console.log(this);
console.log(questo);
}
}

items.asc = {
"seperator_before": false,
"seperator_after": false,
"label": "Ascending",
action: function (obj) {
setSortFunction('asc');
var selector = '#test';

$(selector).jstree(true).refresh();
}
}

items.desc = {
"seperator_before": false,
"seperator_after": false,
"label": "Descending",
action: function (obj) {
setSortFunction('desc');
var selector = '#test';

$(selector).jstree(true).refresh();
}
}



return items;
}

function setSortFunction(sort){
sortType = sort;
}

关于javascript - 如何强制重新排序jsTree?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57538926/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com