前提・実現したいこと
現在、頭1の画像のような状態でテスト1を選択すると選択したノードのidから子要素を図2のように表示したいです。(左の矢印は表示しないです。あくまでも子要素の表示を表したかっただけのため。)
jsTreeで実装可能なのでしょうか?見当もつかずご質問させて頂きました。。
ノード選択時に取得している要素
original {id: "1", parent: "#", text: "テスト1", code: ""}
JSONデータ(parentGroupIdという項目に親となるIDを持たせています)
{ "hits": [ { "groupId": 1, "parentGroupId": "", "itemCode": "", "itemName": "テスト1", "sortOrder": 1 }, { "groupId": 2, "parentGroupId": "", "itemCode": "", "itemName": "テスト2", "sortOrder": 2 }, { "groupId": 3, "parentGroupId": "", "itemCode": "", "itemName": "テスト3", "sortOrder": 3 }, { "groupId": 9, "parentGroupId": "1", "itemCode": "", "itemName": "テスト1-1", "sortOrder": 1 }, { "groupId": 11, "parentGroupId": "1", "itemCode": "", "itemName": "テスト1-2", "sortOrder": 7 }, { "groupId": 14, "parentGroupId": "1", "itemCode": "", "itemName": "テスト1-3", "sortOrder": 12 }, { "groupId": 13, "parentGroupId": "1", "itemCode": "", "itemName": "テスト1-4", "sortOrder": 12 }, { "groupId": 1029, "parentGroupId": "9", "itemCode": "10", "itemName": "テスト結果10", "sortOrder": 2 }, { "groupId": 1026, "parentGroupId": "9", "itemCode": "20", "itemName": "テスト結果20", "sortOrder": 7 }, { "groupId": 1031, "parentGroupId": "9", "itemCode": "30", "itemName": "テスト結果30", "sortOrder": 11 }, { "groupId": 1076, "parentGroupId": "9", "itemCode": "", "itemName": "テスト9-1", "sortOrder": 18 } ] }
該当のソースコード
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>tree</title> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script src="https://code.jquery.com/jquery-3.5.1.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/themes/default/style.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/jstree.min.js"></script> </head> <body> <main> <div id="wrapper"> <div id="jstree"></div> </div> </main> <script type="text/javascript"> //階層変数 let json = []; $.jstree.defaults.core.themes.responsive = true; //階層構造生成 $(function() { let hostUrl = "http://localhost:3000/hits"; $.ajax({ type: 'get', url: hostUrl, dataType : 'json' }).done(function(data){ json = data; //mapとfileterを後ほど合体 const arr = json.filter(function(value){ return value.parentGroupId == ""; }); const tree = json.map((x) => { if(x.parentGroupId == ""){ x.parentGroupId = "#"; } return {'id' : x.groupId.toString() , 'parent' :x.parentGroupId.toString(), 'text' : x.itemName.toString(), 'code' : x.itemCode.toString() }; }); $('#jstree').jstree({ 'core' : { "check_callback" : false, "themes" : { "stripes" : true, "icons": false, "responsive": false }, 'data' : tree, } }); }).fail(function(){ alert('No data'); }); }); //階層構造ノード選択時処理 $('#jstree').on('select_node.jstree', function(e, data) { console.log("node_id: " , data,'original',data.node.original); console.log(data.node.original.parent); $("#"+data.node.id).prop('title', data.node.original.title); console.log(data); //子要素表示の処理 }); </script> </body> </html>
試したこと
アドバイス頂き修正しデータは取得できるようになりましたが、jsTreeに反映されません。。
<!DOCTYPE html> <html lang="ja"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>tree</title> <script type="text/javascript" src="https://www.google.com/jsapi"></script> <script src="https://code.jquery.com/jquery-3.5.1.js"></script> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/themes/default/style.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/jstree.min.js"></script> </head> <body> <main> <div id="wrapper"> <div id="jstree"></div> </div> </main> <script type="text/javascript"> //階層変数 let json = []; $.jstree.defaults.core.themes.responsive = true; //階層構造生成 $(function() { let hostUrl = "http://localhost:3000/hits"; $.ajax({ type: 'get', url: hostUrl, dataType : 'json' }).done(function(data){ json = data; //mapとfileterを後ほど合体 const arr = json.filter(function(value){ return value.parentGroupId == ""; }); const tree = arr.map((x) => { if(x.parentGroupId == ""){ x.parentGroupId = "#"; } return {'id' : x.groupId.toString() , 'parent' :x.parentGroupId.toString(), 'text' : x.itemName.toString(), 'code' : x.itemCode.toString() }; }); $('#jstree').jstree({ 'core' : { "check_callback" : false, "themes" : { "stripes" : true, "icons": false, "responsive": false }, 'data' : tree, } }); }).fail(function(){ alert('No data'); }); }); //階層構造ノード選択時処理 $('#jstree').on('select_node.jstree', function(e, data) { console.log("node_id: " , data,'original',data.node.original); console.log(data.node.original.parent); console.log(data); $(function() { let hostUrl = "http://localhost:3000/hits"; $.ajax({ type: 'get', url: hostUrl, data: { parentGroupId: data.node.original.id, }, dataType : 'json' }).done(function(data){ console.log(data); json = data; //mapとfileterを後ほど合体 const arr = json.filter(function(value){ return value.parentGroupId == ""; }); const tree = json.map((x) => { if(x.parentGroupId == ""){ x.parentGroupId = "#"; } return {'id' : x.groupId.toString() , 'parent' :x.parentGroupId.toString(), 'text' : x.itemName.toString(), 'code' : x.itemCode.toString() }; }); $('#jstree').jstree({ 'core' : { "check_callback" : false, "themes" : { "stripes" : true, "icons": false, "responsive": false }, 'data' : tree, } }); }).fail(function(){ alert('No data'); }); }); }); </script> </body> </html>
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
あなたの回答
tips
プレビュー