###前提・実現したいこと
APIから取得するデータでツリー表示をさせ、画面上にデータとして残すようなページをサンプルで作っています。
どうか皆様のアドバイス(ソースコード)などのお力添えをお願いいたします。
環境等は最下部に記載しています。
以下が実現したいことです。
Jsonデータ ⇒ 取得してツリー表示(初期表示) ⇒ ツリー内のボタン(プラスボタンなど) ⇒対象のJsonデータを再度取得 ⇒ 取得したデータを画面上の任意の箇所に表示ボタンが押された場合にのみ表示
####イメージ
-書店 - BOOK ワンピース☑️ +ホテル +電気 +八百屋 表示ボタン 表示場所[ワンピース]
チェック箇所の情報をHTML(thymeleaf)内の任意の場所に表示する。
表示内容はイメージのとこにあるワンピースという名称でページ数などは裏で結びついて持っていきたい。
###発生している問題・エラーメッセージ
AjaxとJavaでの連携でJsonを取得して、ツリー表示できない。
実現できるなら、AjaxでもJavaでもどちらでもいいのですが、Jsonをツリー表示するのがわからなく、プラスボタンで再度Jsonデータを呼びに行く方法がわからないという現状です。
###該当のソースコード
Java
1@Service 2public class ZipCodeService { 3 4 @Autowired 5 @Qualifier("zipCodeSearchRestTemplate") 6 RestTemplate restTemplate; 7 8 /** サンプル検索API リクエストURL */ 9 private static final String URL = "http://zipcloud.ibsnet.co.jp/api/search?zipcode={zipcode}"; 10 11 public ZipCodeDto service(String zipcode) { 12 return restTemplate.getForObject(URL, ZipCodeDto.class, zipcode); 13 } 14 15}
java
1@Controller 2public class ZipCodeController { 3 4 @Autowired 5 ZipCodeService zpcService; 6 7 /** 8 * サンプル入力フォーム 9 * @return "zipcode" 10 */ 11 @RequestMapping("/zipcode") 12 public String zipcodeForm(HttpSession session, Model model) { 13 return "zipcode"; 14 } 15 16 /** 17 * サンプル情報表示 18 * @return "zipcode-confirm" 19 */ 20 @RequestMapping(value="/zipcode/confirm", method=RequestMethod.POST) 21 public String zipcodeConfirm(HttpSession session, Model model, 22 @RequestParam("zipcode") String zipcode){ 23 24 // 一応必須チェックのみ 数字・桁数チェックは省略 25 // nullまたは空文字の場合、入力フォームにエラーメッセージを表示 26 if (zipcode == null || zipcode.equals("")) { 27 model.addAttribute("errorMessage", "郵便番号を入力してください。"); 28 return zipcodeForm(session, model); 29 } 30 31 // サンプル検索APIサービス呼び出し 32 ZipCodeDto zipCodeDto = zpcService.service(zipcode); 33 // thymeleafでリストを展開して表示する 34 model.addAttribute("zipcodeList", zipCodeDto.getResults()); 35 return "zipcode-confirm"; 36 } 37} 38
HTML
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 2<html xmlns="http://www.w3.org/1999/xhtml" lang="ja"> 3 <head> 4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 5 <meta http-equiv="imagetoolbar" content="no" /> 6 <title>設置サンプル</title> 7 <link rel="stylesheet" type="text/css" href="/content/lib/yui/build/treeview/treeview.css?_yuiversion=2.4.1" /> 8 <link rel="stylesheet" type="text/css" href="/content/lib/yui/build/treeview/tree.css?_yuiversion=2.4.1" /> 9 <script type="text/javascript" src="/content/lib/yui/build/yahoo/yahoo.js?_yuiversion=2.4.1"></script> 10 <script type="text/javascript" src="/content/lib/yui/build/event/event.js?_yuiversion=2.4.1"></script> 11 <script type="text/javascript" src="/content/lib/yui/build/treeview/treeview.js?_yuiversion=2.4.1"></script> 12 <style type="text/css"> 13 * { 14 margin:0; padding:0; 15 } 16 body{ 17 margin:0 auto; padding:0; 18 background-color:#666; 19 color:#666; 20 font:81%/1.5 verdana,sans-serif; 21 text-align:center; 22 } 23 #wrap { 24 width:500px; 25 margin:0 auto; padding:0; 26 background-color:#fff; 27 text-align:center; 28 } 29 #content { 30 margin:0 20px; padding:0; 31 text-align:left; 32 } 33 #footer { 34 margin:1em 0 0 0; padding:.2em .5em; 35 background-color:#999; 36 color:#fff; 37 font-size:78%; 38 text-align:right; 39 } 40 #footer * { 41 font-style:normal; 42 font-size:100%; 43 color:#fff; 44 } 45 h1 { 46 margin:0 0 1em 0; padding:.5em 1em; 47 background-color:#999; 48 color:#fff; 49 font-size:100%; 50 text-align:left; 51 } 52 h1 a { 53 color:#fff; 54 } 55 p { 56 margin:1em 0; padding:0; 57 } 58 img { 59 border:0; 60 } 61 </style> 62 </head> 63 <body> 64 <div id="wrap"> 65 <h1>設置サンプル</h1> 66 <p>参照:<a href="http://developer.yahoo.com/yui/examples/treeview/folder_style.html">YUI Library » Tree View Control » Folder-Style TreeView Design</a></p> 67 <div id="content"> 68 <p> 69 <a id="expand" href="#">Expand all</a> | 70 <a id="collapse" href="#">Collapse all</a> 71 </p> 72 <div id="treeDiv1"></div> 73 <script type="text/javascript"> 74 //an anonymous function wraps our code to keep our variables 75 //in function scope rather than in the global namespace: 76 (function() { 77 var tree; //will hold our TreeView instance 78 function treeInit() { 79 YAHOO.log("Example's treeInit function firing.", "info", "example"); 80 //Hand off ot a method that randomly generates tree nodes: 81 buildRandomTextNodeTree(); 82 //handler for expanding all nodes 83 YAHOO.util.Event.on("expand", "click", function(e) { 84 YAHOO.log("Expanding all TreeView nodes.", "info", "example"); 85 tree.expandAll(); 86 YAHOO.util.Event.preventDefault(e); 87 }); 88 //handler for collapsing all nodes 89 YAHOO.util.Event.on("collapse", "click", function(e) { 90 YAHOO.log("Collapsing all TreeView nodes.", "info", "example"); 91 tree.collapseAll(); 92 YAHOO.util.Event.preventDefault(e); 93 }); 94 } 95 //This method will build a TreeView instance and populate it with 96 //between 3 and 7 top-level nodes 97 function buildRandomTextNodeTree() { 98 //instantiate the tree: 99 tree = new YAHOO.widget.TreeView("treeDiv1"); 100 101 //create top-level nodes 102 for (var i = 0; i < Math.floor((Math.random()*4) + 3); i++) { 103 var tmpNode = new YAHOO.widget.TextNode("label-" + i, tree.getRoot(), false); 104 105 //we'll delegate to another function to build child nodes: 106 buildRandomTextBranch(tmpNode); 107 } 108 //once it's all built out, we need to render 109 //our TreeView instance: 110 tree.draw(); 111 } 112 //This function adds a random number <4 of child nodes to a given 113 //node, stopping at a specific node depth: 114 function buildRandomTextBranch(node) { 115 if (node.depth < 6) { 116 YAHOO.log("buildRandomTextBranch: " + node.index); 117 for ( var i = 0; i < Math.floor(Math.random() * 4) ; i++ ) { 118 var tmpNode = new YAHOO.widget.TextNode(node.label + "-" + i, node, false); 119 buildRandomTextBranch(tmpNode); 120 } 121 } 122 } 123 //When the DOM is done loading, we can initialize our TreeView 124 //instance: 125 YAHOO.util.Event.onDOMReady(treeInit); 126 })();//anonymous function wrapper closed; () notation executes function 127 </script> 128 </div><!-- div#wrap/div#content --> 129 </div><!-- div#wrap --> 130 </body> 131</html> 132
js
1jQuery( function() { 2 jQuery( '#jquery-sample-button' ) . toggle( 3 function() { 4 jQuery . ajax( { 5 url: 'jquery-sample-ajax-json.php', 6 dataType: 'json', 7 data: { 8 year: '2011', 9 month: '11', 10 day: '25' 11 }, 12 success: function( data ) { 13 jQuery . each( data, function( key, value ) { 14 jQuery( '#jquery-sample-ajax' ) . append( '<p>' + key + ': ' + value + '</p>' ); 15 } ); 16 jQuery( '#jquery-sample-textStatus' ) . text( '読み込み成功' ); 17 }, 18 error: function( data ) { 19 jQuery( '#jquery-sample-textStatus' ) . text( '読み込み失敗' ); 20 } 21 } ); 22 }, 23 function() { 24 jQuery( '#jquery-sample-ajax' ) . html( '' ); 25 jQuery( '#jquery-sample-textStatus' ) . text( '' ); 26 } 27 ); 28} );
###対象のJSONデータ
Json
1{ 2 "book1":{ 3"title": "Book", 4 "year": 2005 , 5"page": 399 6}, 7"book2":{ 8 "title": "ワンピース", 9 "year": 2006 , 10"page": 650 11 } 12}
###補足情報(言語/FW/ツール等のバージョンなど)
STS、SpringBoot、Java8,Javascript、HTML、Timeleaf

回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。