デビッドの反応は素晴らしく、効率的です。a_attr 属性を使用してさまざまなノードを区別し、それに基づいてさまざまなコンテキスト メニューを生成できるソリューションの別のバリエーションを見つけました。
以下の例では、Folder と Files の 2 種類のノードを使用しています。グリフィコンを使用して、さまざまなアイコンも使用しました。ファイル タイプ ノードの場合、名前を変更して削除するためのコンテキスト メニューのみを取得できます。フォルダーの場合、すべてのオプションがあり、ファイルの作成、フォルダーの作成、名前の変更、削除があります。
完全なコード スニペットについては、https://everyething.com/Example-of-jsTree-with-different-context-menu-for-different-node-typeを参照してください。
$('#SimpleJSTree').jstree({
"core": {
"check_callback": true,
'data': jsondata
},
"plugins": ["contextmenu"],
"contextmenu": {
"items": function ($node) {
var tree = $("#SimpleJSTree").jstree(true);
if($node.a_attr.type === 'file')
return getFileContextMenu($node, tree);
else
return getFolderContextMenu($node, tree);
}
}
});
初期の json データは以下のとおりで、ノード タイプは a_attr 内に記載されています。
var jsondata = [
{ "id": "ajson1", "parent": "#", "text": "Simple root node", icon: 'glyphicon glyphicon-folder-open', "a_attr": {type:'folder'} },
{ "id": "ajson2", "parent": "#", "text": "Root node 2", icon: 'glyphicon glyphicon-folder-open', "a_attr": {type:'folder'} },
{ "id": "ajson3", "parent": "ajson2", "text": "Child 1", icon: 'glyphicon glyphicon-folder-open', "a_attr": {type:'folder'} },
{ "id": "ajson4", "parent": "ajson2", "text": "Child 2", icon: 'glyphicon glyphicon-folder-open', "a_attr": {type:'folder'} },
];
ファイルとフォルダーを作成するためのコンテキスト メニュー項目の一部として、ファイル アクションとして以下の同様のコードを使用します。
action: function (obj) {
$node = tree.create_node($node, { text: 'New File', icon: 'glyphicon glyphicon-file', a_attr:{type:'file'} });
tree.deselect_all();
tree.select_node($node);
}
フォルダ アクションとして:
action: function (obj) {
$node = tree.create_node($node, { text: 'New Folder', icon:'glyphicon glyphicon-folder-open', a_attr:{type:'folder'} });
tree.deselect_all();
tree.select_node($node);
}