如何将数组应用于视图
how to apply array to view
我正在做数组排序。但我不知道如何在视图中应用这个排序数组并给出合适的ui。这是我的数组排序代码。在点击我能够排序的结果,但它只显示数组值。
<script type="text/javascript">
function sort(key) {
var sortTypes = {
alpha: function (a, b) { return a[key].localeCompare(b[key]); },
numerical: function (a, b) { return a[key] - b[key]; }
},
keys = {
rateType: 'alpha',
interestRateMin: 'numerical',
financingPercentageMax: 'numerical',
interestRateMax: 'numerical',
financingPercentageMin: 'numerical',
bankName: 'alpha',
repaymentTenureInYears: 'numerical',
age: 'numerical',
maxLoanAmt: 'numerical',
};
return sortTypes[keys[key]];
}
var data = <?php echo json_encode($json['resultList']); ?>;
document.addEventListener('change', function () {
var select = document.getElementById('sort-by'),
index = select.selectedIndex,
key = select.options[index].value
data.sort(sort(key));
document.getElementById('out').innerHTML = JSON.stringify(data, 0, 4);
});
</script>
在适当的ui中生成web服务的PHP代码
<div id="out">
<?php if(count($json['resultList']) > 0): ?>
<?php
foreach ($json['resultList'] as $key=>$value) {
if($json["resultList"][$key]["interestRateMin"] == $json["resultList"][$key]["interestRateMax"]){
$interest = $json["resultList"][$key]["interestRateMin"];
}
else{
$interest = $json["resultList"][$key]["interestRateMin"].' - '.$json["resultList"][$key]["interestRateMax"];
}
if($json["resultList"][$key]["financingPercentageMin"] == $json["resultList"][$key]["financingPercentageMax"]){
$financing = $json["resultList"][$key]["financingPercentageMin"];
}
else{
$financing = $json["resultList"][$key]["financingPercentageMin"].' - '.$json["resultList"][$key]["financingPercentageMax"];
}
echo '
<div class="cr-content">
<div class="bank-rates">
<ul>
<li>
<div class="innr-spl" style="padding: 5px 0;">
<span style="display:block;"><img src="'.$json["resultList"][$key]["imageUrl"].'"></span>
<span class="hide">'.$json["resultList"][$key]["bankName"].'</span>
</div>
</li>
<li><div class="innr-spl2">' . $interest . '%' . '</div></li>
<li><div class="innr-spl2">' . $financing . '</div></li>
<li class="hide"><div class="innr-spl3 kmore" id="'.$key.'"><i class="fa fa-chevron-circle-down" aria-hidden="true"></i>View Datails</div> </li>
<li>
<div class="apply-now innr-spl"><a href="http://www.zengalol.com/gold-loan/" type="button" role="button" target="_blank" class="btn-apply">Apply Now</a></div>
</li>
</ul>
<div style="clear:both"></div>
<ul class="details" id="ban_'.$key.'" style = "display:none;">
<li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Maximum Loan Amount : '.$json["resultList"][$key]["maxLoanAmt"].'</li>
<li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Max Tenure (In Years) : '.$json["resultList"][$key]["repaymentTenureInYears"].'</li>
<li><i class="fa fa-thumbs-o-up" aria-hidden="true"></i> Interest Rate Type : '.$json["resultList"][$key]["rateType"].'</li>
</ul>
</div>
</div>
';
$i++;
}
?>
<?php endif;?>
</div>
你必须添加html。不只是JSON-string。
例如:data = JSON.parse(data);
var html = '<ul><li>'+data.join("</li> <li>")+'</li></ul>';
document.getElementById('out').innerHTML = html;
我不确定,如果你能复制我的代码示例。但也可能不会,因为我不想把列表的html代码写进我的例子里。但你可以把它当作一个提示。
相关文章:
- 如何将HTML id分配给元素,以及如何将JavaScript应用于元素
- 如何将返回的值应用于多个不同位置的多个选择器
- ThreeJS将画布中的文本渲染为纹理,然后应用于平面
- 如何使jQuery中的悬停函数单独应用于数据库映像
- 如何将jquery函数仅应用于大屏幕
- 将jQuery事件应用于所有类元素
- 在将绑定应用于控制器之后,是否会发出Angular$scope事件
- 如何在ExtJs中获得将应用于给定类列表的样式
- 将 CSS 应用于禁用的输入按钮
- 如何从应用于正文的css中排除特定的表单
- jQuery+Android.将功能(Touchstart、touchmove)应用于实时加载的内容
- 将敲除绑定应用于模板,并将结果作为字符串获取
- 页面加载后,将FooTable断点应用于网格视图中的列标题
- 将 JQuery 脚本应用于特定的网格视图
- AngularJS,将更改应用于工厂的视图
- 将闪烁边框应用于应用器中的表视图
- 绑定数据列表后,将 css 样式重新应用于视图的一部分
- 在Node.js Express应用程序中加载特定于视图的JavaScript
- 如何将工厂的更新应用于视图
- 如何将数组应用于视图