質問編集履歴

1

修正後ソース追記

2021/04/14 08:10

投稿

tanakamaro
tanakamaro

スコア13

test CHANGED
File without changes
test CHANGED
@@ -358,7 +358,219 @@
358
358
 
359
359
  ### 試したこと
360
360
 
361
- 図1から子要素を表示す方法見当も付かずご質問させて頂きました。。
361
+ アドバイス頂き修正しデータは取得できようになりましたが、jsTreeに反映れま。。
362
+
363
+ ```ここに言語を入力
364
+
365
+ <!DOCTYPE html>
366
+
367
+ <html lang="ja">
368
+
369
+ <head>
370
+
371
+ <meta charset="UTF-8">
372
+
373
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
374
+
375
+ <title>tree</title>
376
+
377
+ <script type="text/javascript" src="https://www.google.com/jsapi"></script>
378
+
379
+ <script src="https://code.jquery.com/jquery-3.5.1.js"></script>
380
+
381
+ <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/themes/default/style.min.css">
382
+
383
+ <script src="https://cdnjs.cloudflare.com/ajax/libs/jstree/3.3.2/jstree.min.js"></script>
384
+
385
+
386
+
387
+
388
+
389
+ </head>
390
+
391
+ <body>
392
+
393
+ <main>
394
+
395
+ <div id="wrapper">
396
+
397
+ <div id="jstree"></div>
398
+
399
+ </div>
400
+
401
+
402
+
403
+ </main>
404
+
405
+ <script type="text/javascript">
406
+
407
+ //階層変数
408
+
409
+ let json = [];
410
+
411
+ $.jstree.defaults.core.themes.responsive = true;
412
+
413
+
414
+
415
+
416
+
417
+ //階層構造生成
418
+
419
+ $(function() {
420
+
421
+ let hostUrl = "http://localhost:3000/hits";
422
+
423
+ $.ajax({
424
+
425
+ type: 'get',
426
+
427
+ url: hostUrl,
428
+
429
+ dataType : 'json'
430
+
431
+ }).done(function(data){
432
+
433
+ json = data;
434
+
435
+ //mapとfileterを後ほど合体
436
+
437
+ const arr = json.filter(function(value){
438
+
439
+ return value.parentGroupId == "";
440
+
441
+ });
442
+
443
+ const tree = arr.map((x) => {
444
+
445
+ if(x.parentGroupId == ""){
446
+
447
+ x.parentGroupId = "#";
448
+
449
+ }
450
+
451
+ return {'id' : x.groupId.toString() , 'parent' :x.parentGroupId.toString(), 'text' : x.itemName.toString(), 'code' : x.itemCode.toString() };
452
+
453
+ });
454
+
455
+ $('#jstree').jstree({ 'core' : {
456
+
457
+ "check_callback" : false,
458
+
459
+ "themes" : { "stripes" : true, "icons": false, "responsive": false },
460
+
461
+ 'data' : tree,
462
+
463
+ }
464
+
465
+ });
466
+
467
+
468
+
469
+ }).fail(function(){
470
+
471
+ alert('No data');
472
+
473
+ });
474
+
475
+
476
+
477
+ });
478
+
479
+ //階層構造ノード選択時処理
480
+
481
+ $('#jstree').on('select_node.jstree', function(e, data) {
482
+
483
+ console.log("node_id: " , data,'original',data.node.original);
484
+
485
+ console.log(data.node.original.parent);
486
+
487
+
488
+
489
+ console.log(data);
490
+
491
+ $(function() {
492
+
493
+ let hostUrl = "http://localhost:3000/hits";
494
+
495
+ $.ajax({
496
+
497
+ type: 'get',
498
+
499
+ url: hostUrl,
500
+
501
+ data: {
502
+
503
+ parentGroupId: data.node.original.id,
504
+
505
+ },
506
+
507
+ dataType : 'json'
508
+
509
+ }).done(function(data){
510
+
511
+ console.log(data);
512
+
513
+ json = data;
514
+
515
+ //mapとfileterを後ほど合体
516
+
517
+ const arr = json.filter(function(value){
518
+
519
+ return value.parentGroupId == "";
520
+
521
+ });
522
+
523
+ const tree = json.map((x) => {
524
+
525
+ if(x.parentGroupId == ""){
526
+
527
+ x.parentGroupId = "#";
528
+
529
+ }
530
+
531
+ return {'id' : x.groupId.toString() , 'parent' :x.parentGroupId.toString(), 'text' : x.itemName.toString(), 'code' : x.itemCode.toString() };
532
+
533
+ });
534
+
535
+ $('#jstree').jstree({ 'core' : {
536
+
537
+ "check_callback" : false,
538
+
539
+ "themes" : { "stripes" : true, "icons": false, "responsive": false },
540
+
541
+ 'data' : tree,
542
+
543
+ }
544
+
545
+ });
546
+
547
+
548
+
549
+ }).fail(function(){
550
+
551
+ alert('No data');
552
+
553
+ });
554
+
555
+
556
+
557
+ });
558
+
559
+ });
560
+
561
+
562
+
563
+
564
+
565
+
566
+
567
+ </script>
568
+
569
+ </body>
570
+
571
+ </html>
572
+
573
+ ```
362
574
 
363
575
 
364
576