質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

Q&A

0回答

1000閲覧

[jsTree]ノードを選択した際にajax通信で下の階層を表示したいです。可能なのでしょうか?

tanakamaro

総合スコア13

JavaScript

JavaScriptは、プログラミング言語のひとつです。ネットスケープコミュニケーションズで開発されました。 開発当初はLiveScriptと呼ばれていましたが、業務提携していたサン・マイクロシステムズが開発したJavaが脚光を浴びていたことから、JavaScriptと改名されました。 動きのあるWebページを作ることを目的に開発されたもので、主要なWebブラウザのほとんどに搭載されています。

jQuery

jQueryは、JavaScriptライブラリのひとつです。 簡単な記述で、JavaScriptコードを実行できるように設計されています。 2006年1月に、ジョン・レシグが発表しました。 jQueryは独特の記述法を用いており、機能のほとんどは「$関数」や「jQueryオブジェクト」のメソッドとして定義されています。

0グッド

0クリップ

投稿2021/04/14 07:43

編集2022/01/12 10:55

前提・実現したいこと

現在、頭1の画像のような状態でテスト1を選択すると選択したノードのidから子要素を図2のように表示したいです。(左の矢印は表示しないです。あくまでも子要素の表示を表したかっただけのため。)
jsTreeで実装可能なのでしょうか?見当もつかずご質問させて頂きました。。

図1
イメージ説明
図2
イメージ説明

ノード選択時に取得している要素

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/ツールのバージョンなど)

ここにより詳細な情報を記載してください。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

tanakamaro

2021/04/14 08:06

ありがとうございます! 試してみて、コンソールで確認しましたところデータは取得できていますがjsTreeに反映されません。。 ソースは質問の試したことに追記します。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

まだ回答がついていません

会員登録して回答してみよう

アカウントをお持ちの方は

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問