[英]Problem displaying dynamic data in a table in Laravel blade using Js
[英]Problem displaying dynamic data in an array in Laravel blade view using Javascript
在Laravel應用程序上工作,在該應用程序中數組中包含一些數據。 數據是關聯數組的集合,其中每個數組都有一個標識號和其中的一組策略代碼 。 正在獲取數據並顯示在刀片上。 在視圖中,我分為兩列(使用引導網格系統) 。 在左列(col-md-4)上遍歷該變量並顯示可以正常工作的標識號。 在右列中,有一個表格,據此可以根據identity_no的狀態顯示相應的策略代碼。
我想實現一種功能,當用戶單擊或將鼠標懸停在特定的標識號上時,相應的策略代碼應顯示在表tbody標記的右列(col-md-8)上。 后續的身份編號應重復相同的操作(應僅在單擊或懸停該身份編號后才顯示)。
存儲在名為asm的變量中的數組集合
array:1 [▼
0 => array:2 [▼
"identity_no" => "71360"
"policy_code" => array:2 [▼
0 => "IL2***********"
1 => "IL2***********"
2 => "IL2***********"
]
]
1 => array:2 [▼
"identity_no" => "68181"
"policy_code" => array:3 [▼
0 => "IL2**********"
1 => "IL2***********"
2 => "IL2***********"
3 => "IL2***********"
]
]
2 => array:2 [▼
"identity_no" => "53983"
"policy_code" => array:4 [▼
0 => "IL2*************"
1 => "IL2*************"
2 => "IL2*************"
3 => "IL2*************"
4 => "IL2*************"
5 => "IL2*************"
]
]
]
視圖布局
<div class="row">
<!-- lEFT column -->
<div class="col-md-4">
<div id="MainMenu">
<div class="list-group panel">
<!-- Level 1 -->
@foreach($asm as $a)
<a href="#" class="list-group-item list-group-item-primary" > Identity No: {{ $a['identity_no'] }} </a>
@endforeach
<!-- END level 1-->
</div>
</div>
</div>
<!-- END left column-->
<!-- Right column-->
<div class="col-md-8">
<table id="summary-table">
<thead>
<tr>
<th>Policy Codes</th>
</tr>
</thead>
<tbody>
<tr>
<td> <!-- Add dynamic policy codes--></td>
</tr>
</tbody>
</table>
</div>
嘗試這個! 我只知道javascript,所以解決方案在純javascript和jquery中,我已經將鼠標懸停和click事件合並到了按鈕上。 希望這會有所幫助
asm = [{ identity_no: '1', policy_code: "bla bla111111111" }, { identity_no: "2", policy_code: "bla bla2222222" }, { identity_no: "3", policy_code: "bla bla3333" }]; function btns() { var btn = $("<button class='tags' id=" + asm[i].identity_no + ">" + asm[i].identity_no + "</button>"); $(btn).attr("data-search", asm[i].identity_no) $(btn).appendTo("#buttons") } $('#buttons').on('click mouseover', 'button', function() { console.log('click on', $(this).attr('id')); var a = asm.filter(x => x.identity_no === $(this).attr('id')).map(x => x.policy_code); console.log("found!--", a) // show this on other side of col }); for (i = 0; i < asm.length; i++) { btns(); }
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.0.2/jquery.min.js"></script> <div id="buttons"></div>
您可以這樣做(在我的本地設備上測試並發現它正常工作):
<script>
var data;
$( document ).ready(function() {
data = {!! json_encode($asm) !!};
});
$(document).on("mouseenter", "a", function() {
var policyCodes = '';
var identityNo = $(this).attr('id');
for(var i = 0; i < data.length; i++) {
if(identityNo == data[i]['identity_no']) {
for(var j = 0;j < data[i]['policy_code'].length;j++){
policyCodes += '<td>' + data[i]['policy_code'][j] + '</td>';
}
}
}
console.log(policyCodes);
$('#summary-table tbody tr').html(policyCodes);
});
</script>
@foreach($asm as $a)
<a href="#" class="list-group-item list-group-item-primary" id="{{ $a['identity_no'] }}" > Identity No: {{ $a['identity_no'] }} </a>
@endforeach
希望能幫助到你 :)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.