简体   繁体   English

使用Js在Laravel刀片中的表中显示动态数据时出现问题

[英]Problem displaying dynamic data in a table in Laravel blade using Js

I am working an a Laravel project whereby am collecting some data from the backend. 我正在一个Laravel项目中,该项目正在从后端收集一些数据。 The data is in hierarchical/tree-like structure with the highest being asm then usm then ag . 数据采用分层/树状结构,最高的是asm,然后是usm,然后是ag Each of the variable is a collection of associative arrays whereby each array has a unique id and collection of policies . 每个变量都是关联数组的集合,由此每个数组都有唯一的ID和策略的集合 In the view I have partitioned it into 2 columns (using bootstrap grid layout) mainly the left column (col-md-4) and right column (col-md-8). 在视图中,我将其分为两列(使用引导网格布局),主要是左列(col-md-4)和右列(col-md-8)。

In the left column (col-md-4) I have used bootstrap collapsible menus to display the arrays in a hierarchical structure which works fine. 在左列(col-md-4)中,我使用了引导程序可折叠菜单,以按层次结构显示阵列,效果很好。 In the right column (col-md-8) I have a problem displaying the respective policies once the user hovers or clicks on id of a respective asm ,usm or ag. 在右列(col-md-8)中,一旦用户将鼠标悬停或单击相应的asm,usm或ag的ID,我便无法显示相应的策略。

I have tried to use some Javascript but it is not working as expected. 我试图使用一些Javascript,但是它没有按预期工作。

//asm variable (level 1)
array:3 [▼
  0 => array:3 [▼
    "id" => "157"
    "unit_sales" => array:7 [▶]
    "policies" => array:3144 [▶]
  ]
  1 => array:3 [▼
    "id" => "73401"
    "unit_sales" => array:1 [ …1]
    "policies" => array:8 [ …8]
  ]
  2 => array:3 [▼
    "id" => "6511"
    "unit_sales" => array:7 [ …7]
    "policies" => array:1987 [ …1987]
  ]
]

//usm variable (level 2)
array:3 [▼
  0 => array:3 [▼
    "id" => "1525"
    "ag" => array:10 [▶]
    "policies" => array:374 [▶]
  ]
  1 => array:3 [▼
    "id" => "74696"
    "ag" => array:12 [▶]
    "policies" => array:496 [▶]
  ]
  2 => array:3 [▼
    "id" => "47060"
    "ag" => array:1 [▶]
    "policies" => array:129 [▶]
  ]
]

//ag variable (level 3)
array:3 [▼
  0 => array:2 [▼
    "agent_no" => "42184"
    "policies" => array:38 [▶]
  ]
  1 => array:2 [▼
    "agent_no" => "21718"
    "policies" => array:59 [▶]
  ]
  2 => array:2 [▼
    "agent_no" => "78863"
    "policies" => array:3 [▶]
  ]
]

Left column am displaying the dropdown 左列显示下拉菜单

<div class="col-md-4">
  <!--RSM looged in (See ASM downwards)-->
  @if(isset($asm, $usm , $ag))
    <div id="MainMenu">
      <div class="list-group panel">
        <!-- Level 1 -->
        @php
          $i = 1;
        @endphp
        @foreach($asm as $a)
         <a href="#demo{{$i}}" class="list-group-item list-group-item-primary" data-toggle="collapse" data-parent="#MainMenu" id="{{ $a['id'] }}"> Agency Sales Managers ID : {{ $a['id'] }} </a>
            <div class="collapse" id="demo{{$i}}">
            <!-- Level 2 -->
            @foreach($usm as $u)
              <a href="#SubMenu{{$i}}" class="list-group-item list-group-item-success" data-toggle="collapse" data-parent="#SubMenu1" style="color: red;" id="{{ $u['id'] }}"> Unit Sales Managers ID : {{ $u['id'] }} <i class="fa fa-caret-down"></i></a>

              <div class="collapse list-group-submenu" id="SubMenu{{$i}}">
                <!-- Level 3 -->
                  @foreach($ag as $Agt)
                    <a href="#SubSubMenu1{{$i}}" class="list-group-item" data-toggle="collapse" data-parent="#SubSubMenu1" id="{{ $Agt['agent_no'] }}"> Agents Number : {{ $Agt['agent_no'] }}</a>
                  @endforeach
               <!-- END level 3-->
              </div>
              @endforeach 
              <!--END level 2-->
            </div>
         @php $i++; @endphp
        @endforeach
        <!-- END level 1-->
      </div> 
    </div>
  @endif
</div>

Right column (Table am populating the policies dynamically) 右列(表正在动态填充策略)

<div class="col-md-8">
 <table class="table table-hover mg-b-0 tx-11" id="summary-table">
    <thead>
      <tr>
        <th>POLICIES</th>
      </tr>
    </thead>
      <tbody>
        <tr> <!-- Add policies dynamically via JS --></tr> 
      </tbody>
</table>
</div>

Javascript code am using 正在使用Javascript代码

<script>
//Returns true if asm , usm and ag variables are set
<?php if(isset($asm) and isset($usm) and isset($ag)){ ?>
      $( document ).ready(function() {
          var asmData = {!! json_encode($asm) !!};
          var usmData = {!! json_encode($usm) !!};
          var agData = {!! json_encode($ag) !!};
      });

      $(document).on("mouseenter", "a", function() {
          //Execute ASM
          var asmPolicies = '';
          //Fetch id of a tag in the DOM
          var asmId = $(this).attr('id');
          for(var i = 0; i < asmData.length; i++) {
              if(asmId == asmData[i]['id']) {
                  for(var j = 0; j < asmData[i]['policies'].length; j++){
                      asmPolicies += '<tr><td>' + asmData[i]['policies'][j] + '</td></tr>';
                  }
              }

          }
          $('#summary-table tbody tr').html(asmPolicies);
          //END ASM

          //Execute USM
          var usmPolicies = '';
          //Fetch id of a tag in the DOM
          var usmId = $(this).attr('id');
          for(var i = 0; i < usmData.length; i++) {
              if(usmId == usmData[i]['id']) {
                  for(var j = 0; j < usmData[i]['policies'].length; j++){
                      usmPolicies += '<tr><td>' + usmData[i]['policies'][j] + '</td></tr>';
                  }
              }

          }
          $('#summary-table tbody tr').html(usmPolicies);
          //END USM

          //Execute agents
          var agPolicies = '';
          //Fetch id of a tag in the DOM
          var agId = $(this).attr('id');
          for(var i = 0; i < agData.length; i++) {
              if(agId == agData[i]['agent_no']) {
                  for(var j = 0; j < agData[i]['policies'].length; j++){
                      agPolicies += '<tr><td>' + agData[i]['policies'][j] + '</td></tr>';
                  }
              }

          }
          $('#summary-table tbody tr').html(agPolicies);
          //END Agents
      });
<?php } ?>
</script>

try doing this 尝试这样做

//Returns true if asm , usm and ag variables are set
<?php if(isset($asm) and isset($usm) and isset($ag)){ ?>
  $( document ).ready(function() {
      var asmData = {!! json_encode($asm) !!};
      var usmData = {!! json_encode($usm) !!};
      var agData = {!! json_encode($ag) !!};


  $(document).on("mouseenter", "a", function() {

      // here we delete the old data from the table 
      $('#summary-table tbody tr').html('');

      //Execute ASM
      var asmPolicies = '';
      //Fetch id of a tag in the DOM
      var asmId = $(this).attr('id');
      for(var i = 0; i < asmData.length; i++) {
          if(asmId == asmData[i]['id']) {
              for(var j = 0; j < asmData[i]['policies'].length; j++){
                  asmPolicies += '<tr><td>' + asmData[i]['policies'][j] + '</td></tr>';
              }
          }

      }
      // we append asmPolicies Html to the table
      $('#summary-table tbody tr').append(asmPolicies);
      //END ASM

      //Execute USM
      var usmPolicies = '';
      //Fetch id of a tag in the DOM
      var usmId = $(this).attr('id');
      for(var i = 0; i < usmData.length; i++) {
          if(usmId == usmData[i]['id']) {
              for(var j = 0; j < usmData[i]['policies'].length; j++){
                  usmPolicies += '<tr><td>' + usmData[i]['policies'][j] + '</td></tr>';
              }
          }

      }
      // we append usmPolicies to the table
      $('#summary-table tbody tr').append(usmPolicies);
      //END USM

      //Execute agents
      var agPolicies = '';
      //Fetch id of a tag in the DOM
      var agId = $(this).attr('id');
      for(var i = 0; i < agData.length; i++) {
          if(agId == agData[i]['agent_no']) {
              for(var j = 0; j < agData[i]['policies'].length; j++){
                  agPolicies += '<tr><td>' + agData[i]['policies'][j] + '</td></tr>';
              }
          }

      }
      // we append agPoliciesHtml to the table
      $('#summary-table tbody tr').append(agPolicies);
      //END Agents
  });
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM