使用 Javascript 在 Laravel blade 视图中显示数组中的动态数据时出现问题
Problem displaying dynamic data in an array in Laravel blade view using Javascript
正在开发一个 Laravel 应用程序,其中我在数组中有一些数据。数据是一个 collection 的关联数组,其中每个数组都有一个 标识号 和一个 collection 的策略代码 它。正在获取数据并显示到 blade。在视图中,我在 2 列中进行了分区(使用 bootstrap 网格系统)。在左列 (col-md-4) 循环变量并显示 工作正常 的标识号。在右栏中,我有一个 table,应该根据 identity_no.
的状态显示相应的策略代码
我想实现一个功能,当用户点击或悬停在特定的身份号码上时,相应的政策代码应显示在 [=44= 中的右栏 (col-md-8) ] tbody 标签。后续身份号码应重复相同操作(应仅在单击或悬停身份号码后显示)。
数组 collection 存储在名为 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 我已经合并了按钮上的悬停和单击事件。希望这可能有所帮助
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
希望对您有所帮助:)
正在开发一个 Laravel 应用程序,其中我在数组中有一些数据。数据是一个 collection 的关联数组,其中每个数组都有一个 标识号 和一个 collection 的策略代码 它。正在获取数据并显示到 blade。在视图中,我在 2 列中进行了分区(使用 bootstrap 网格系统)。在左列 (col-md-4) 循环变量并显示 工作正常 的标识号。在右栏中,我有一个 table,应该根据 identity_no.
的状态显示相应的策略代码我想实现一个功能,当用户点击或悬停在特定的身份号码上时,相应的政策代码应显示在 [=44= 中的右栏 (col-md-8) ] tbody 标签。后续身份号码应重复相同操作(应仅在单击或悬停身份号码后显示)。
数组 collection 存储在名为 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 我已经合并了按钮上的悬停和单击事件。希望这可能有所帮助
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
希望对您有所帮助:)