admin管理员组文章数量:1434904
I have this JSON data:
{
"doorTypes": [
{
"name": "Flat Panel",
"image": "doors/flatpanel.png",
"type": "Wood"
},
{
"name": "Raised Panel",
"image": "doors/raisedpanel.png",
"type": "Wood"
},
{
"name": "Slab Door",
"image": "doors/slabdoor.png",
"type": "Wood"
}],
"woods": [
{
"name": "Alder",
"image": "species/alder.png",
"weights":[
{
"name": "Flat Panel",
"weight": 1.19
},
{
"name": "Raised Panel",
"weight": 1.76
},
{
"name": "Slab Door",
"weight": 1.97
}]
},
{
"name": "Ash",
"image": "species/ash.png",
"weights":[
{
"name": "Flat Panel",
"weight": 1.7
}]
},
{
"name": "Bamboo",
"image": "species/bamboo.png",
"weights":[
{
"name": "Slab Door",
"weight": 2.7
}]
},
{
"name": "Beech",
"image": "species/beech.png",
"weights":[
{
"name": "Raised Panel",
"weight": 2.27
},
{
"name": "Slab Door",
"weight": 2.54
}]
}]
}
Depending on what doorType
you select, I'd like to filter the wood
types. For example, if you select Raised Panel
, I only want the woods that have a weight with Raised Panel
to show up. So in this case, Alder and Beech
would show, not Ash
or Bamboo
Right now I am strictly using ng-repeat and it is displaying all the wood types. I've looked at the docs for ng-filter, but I am not sure how to apply it in a case where the weights
object has multiple properties.
My current ng-repeat:
<ul class="touchcarousel-container">
<li class="touchcarousel-item" ng-repeat="obj in currentObject">
<div class="img-select" ng-click="setActive(this)" ng-class="{itemSelected : isActive(this)}">
<div align="center">
<img ng-src="resources/images/aventos/{{obj.image}}" />
</div>
<div class="img-title">{{obj.name}}</div>
</div>
</li>
</ul>
I am also open to changing my JSON if it makes more sense to do so.
EDIT
This was my solution:
<li class="touchcarousel-item" ng-repeat="obj in currentObject" ng-show="woodTypes(obj)">
and then:
$scope.woodTypes = function(obj)
{
var shown = false;
for (var i = 0; i < obj.weights.length; i++)
{
if (obj.weights[i].name == $scope.cabinetDetails.door.name)
{
shown = true;
break;
}
}
return shown;
}
I have this JSON data:
{
"doorTypes": [
{
"name": "Flat Panel",
"image": "doors/flatpanel.png",
"type": "Wood"
},
{
"name": "Raised Panel",
"image": "doors/raisedpanel.png",
"type": "Wood"
},
{
"name": "Slab Door",
"image": "doors/slabdoor.png",
"type": "Wood"
}],
"woods": [
{
"name": "Alder",
"image": "species/alder.png",
"weights":[
{
"name": "Flat Panel",
"weight": 1.19
},
{
"name": "Raised Panel",
"weight": 1.76
},
{
"name": "Slab Door",
"weight": 1.97
}]
},
{
"name": "Ash",
"image": "species/ash.png",
"weights":[
{
"name": "Flat Panel",
"weight": 1.7
}]
},
{
"name": "Bamboo",
"image": "species/bamboo.png",
"weights":[
{
"name": "Slab Door",
"weight": 2.7
}]
},
{
"name": "Beech",
"image": "species/beech.png",
"weights":[
{
"name": "Raised Panel",
"weight": 2.27
},
{
"name": "Slab Door",
"weight": 2.54
}]
}]
}
Depending on what doorType
you select, I'd like to filter the wood
types. For example, if you select Raised Panel
, I only want the woods that have a weight with Raised Panel
to show up. So in this case, Alder and Beech
would show, not Ash
or Bamboo
Right now I am strictly using ng-repeat and it is displaying all the wood types. I've looked at the docs for ng-filter, but I am not sure how to apply it in a case where the weights
object has multiple properties.
My current ng-repeat:
<ul class="touchcarousel-container">
<li class="touchcarousel-item" ng-repeat="obj in currentObject">
<div class="img-select" ng-click="setActive(this)" ng-class="{itemSelected : isActive(this)}">
<div align="center">
<img ng-src="resources/images/aventos/{{obj.image}}" />
</div>
<div class="img-title">{{obj.name}}</div>
</div>
</li>
</ul>
I am also open to changing my JSON if it makes more sense to do so.
EDIT
This was my solution:
<li class="touchcarousel-item" ng-repeat="obj in currentObject" ng-show="woodTypes(obj)">
and then:
$scope.woodTypes = function(obj)
{
var shown = false;
for (var i = 0; i < obj.weights.length; i++)
{
if (obj.weights[i].name == $scope.cabinetDetails.door.name)
{
shown = true;
break;
}
}
return shown;
}
Share
Improve this question
edited Aug 13, 2013 at 22:53
Ronnie
asked Aug 13, 2013 at 19:49
RonnieRonnie
11.2k22 gold badges84 silver badges142 bronze badges
3
- Checkout stackoverflow./questions/13711960/… – Christopher Marshall Commented Aug 13, 2013 at 19:51
-
That is just changing the order. I need them to not appear which is why I believe I need a
filter
not anorderBy
– Ronnie Commented Aug 13, 2013 at 19:58 - ahh my apologies, I misunderstood. – Christopher Marshall Commented Aug 13, 2013 at 20:00
1 Answer
Reset to default 4Try writing a custom filter, and using some kind of bination like this.
<select ng-options="foo.blah for foo in foos" ng-model="selection"></select>
<li ng-repeat="obj in objects|filter:selection|filterFunction">{{obj}}</li>
.filter('filterFunction', function() {
return function (objects) {
var filter_objects = [];
for (var i = 0; i < objects.length; i++) {
for (var j = 0; j < objects[i].weights.length; j++) {
if (objects[i].weights[j].name === "Raised Panel") {
filter_objects.push(objects[i]);
}
}
}
return filter_objects;
}
});
本文标签: javascriptangular ngrepeat with multiple filter optionsStack Overflow
版权声明:本文标题:javascript - angular ng-repeat with multiple filter options - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745634516a2667473.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论