gpt4 book ai didi

javascript - AngularJS 基于 JSON 记录过滤数据

转载 作者:行者123 更新时间:2023-12-02 14:08:22 24 4
gpt4 key购买 nike

我有一个 JSON 文件,想要打印 <divs>基于餐厅的地址,但我想根据“r.Address”字段对它们进行分组。与所有带有“地址”的记录一样,德里应该首先显示,然后是“昌迪加尔”的所有记录,依此类推。我怎样才能做到这一点?

以下是我的一些代码

                    <a href="#" ng-repeat="r in resturant" id="rest-list-f" class="offVal">
<div class="col-md-6 col-centered form-planner-inner">
<span class="form-text">{{ r.Name }}</span><br>
<address><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span> &nbsp;{{ r.Address }}</address>
<img class="img-responsive" src="{{ r.Image }}" alt="{{ r.Name }} Image"><br>
<hr/>
<table class="eventActive">
<caption><strong><center><a href="#">Click To Read Reviews</a></center></strong></caption>
<tr>
<td><span class="glyphicon glyphicon-cutlery" aria-hidden="true"></span> &nbsp;{{ r.Type }}</td>
<td><span class="glyphicon glyphicon-time" aria-hidden="true"></span> &nbsp;{{ r.Hours }}</td>
</tr>
</table>
</div>
</a>

这是我的 JSON 代码

   {
"records": [
{
"Name": "Sagar Ratna",
"Image": "./images/sr.jpg",
"Address": "Mohali",
"Type": "South Indian",
"Hours" : "10:00-23:30"
},
{
"Name": "The Night Factory",
"Image": "./images/nf.jpg",
"Address": "Chandigarh",
"Type": "Chinese",
"Hours" : "24/7"
},
{
"Name": "Whistling Duck",
"Image": "./images/ed.jpg",
"Address": "Rajpura",
"Type": "Chinese",
"Hours" : "11:30-23:30"
},
{
"Name": "Uncle Jack's",
"Image": "./images/uj.jpg",
"Address": "Mohali",
"Type": "Pizza",
"Hours" : "10:00-22:30"
},
{
"Name": "Corner Griller",
"Image": "./images/cg.jpg",
"Address": "Rajpura",
"Type": "North Indian",
"Hours" : "11:00-23:30"
}, {
"Name": "Starbucks",
"Image": "./images/st.jpg",
"Address": "Delhi",
"Type": "Coffee",
"Hours" : "24/7"
}
]
}

这是我想要的输出:

<div> Restaurant Name, Address: Delhi </div>
<div> Restaurant Name, Address: Mohali </div>
<div> Restaurant Name, Address: Mohali </div>
<div> Restaurant Name, Address: Rajpura </div>
<div> Restaurant Name, Address: Rajpura </div>
<div> Restaurant Name, Address: Rajpura </div>
<div> Restaurant Name, Address: Chandigarh </div>

最佳答案

我对你的代码做了一些更改。请尝试这个。

JSON

  $scope.Restaurents =  {
"records": [
{
"Name": "Sagar Ratna",
"Image": "./images/sr.jpg",
"Address": "Mohali",
"Type": "South Indian",
"Hours" : "10:00-23:30"
},
{
"Name": "The Night Factory",
"Image": "./images/nf.jpg",
"Address": "Chandigarh",
"Type": "Chinese",
"Hours" : "24/7"
},
{
"Name": "Whistling Duck",
"Image": "./images/ed.jpg",
"Address": "Rajpura",
"Type": "Chinese",
"Hours" : "11:30-23:30"
},
{
"Name": "Uncle Jack's",
"Image": "./images/uj.jpg",
"Address": "Mohali",
"Type": "Pizza",
"Hours" : "10:00-22:30"
},
{
"Name": "Corner Griller",
"Image": "./images/cg.jpg",
"Address": "Rajpura",
"Type": "North Indian",
"Hours" : "11:00-23:30"
}, {
"Name": "Starbucks",
"Image": "./images/st.jpg",
"Address": "Delhi",
"Type": "Coffee",
"Hours" : "24/7"
}
]
}

HTML

<a href="#" ng-repeat="r in Restaurents.records | orderBy:'Address'" id="rest-list-f" class="offVal">
<div class="col-md-6 col-centered form-planner-inner">
<span class="form-text">{{ r.Name }}</span><br>
<address><span class="glyphicon glyphicon-map-marker" aria-hidden="true"></span> &nbsp;{{ r.Address }}</address>
<img class="img-responsive" src="{{ r.Image }}" alt="{{ r.Name }} Image"><br>
<hr />
<table class="eventActive">
<caption><strong><center><a href="#">Click To Read Reviews</a></center></strong></caption>
<tr>
<td><span class="glyphicon glyphicon-cutlery" aria-hidden="true"></span> &nbsp;{{ r.Type }}</td>
<td><span class="glyphicon glyphicon-time" aria-hidden="true"></span> &nbsp;{{ r.Hours }}</td>
</tr>
</table>
</div>
</a>

希望能有所帮助。您还可以在 Controller 端处理时对 json 数组进行分组。

关于javascript - AngularJS 基于 JSON 记录过滤数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39859275/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com