我有一个数据集,我试图使用Angular中的选择列表进行过滤。选择列表中填充了数据中的条目,并且我使用角度过滤器对其进行过滤,以便为我提供“唯一性”信息。
问题在于,由于选择列表正在按唯一身份进行过滤,如果我使用选择列表来过滤数据集,那么它只返回一个项目而不是所有符合该条件的项目。< / p>
我也不确定如何让过滤器列表协同工作,因此每个列表都会进一步减少数据集。
最后,当选择列表项设置回默认值时,我不确定如何重置数据。
http://plnkr.co/edit/5s1BN7?p=preview
var app = angular.module('plunker', ['angular.filter']);
app.controller('MainCtrl', function($scope, $anchorScroll, $location) {
$scope.cart = [];
$scope.addToCart = function(index) {
$scope.cart.push(index);
$scope.cartCount = $scope.cart.length;
}
$scope.activeRow = function(index) {
$scope.selectedRow = index;
$location.hash();
$anchorScroll('anchor-' + index);
}
$scope.gotoAnchor = function(x) {
var newHash = 'anchor' + x;
}
$scope.dataObject = [{
"Number": "001",
"Status": "NCB",
"Compound": "CD19A"
}, {
"Number": "002",
"Status": "NCA",
"Compound": "CD19B"
}, {
"Number": "003",
"Status": "NCA",
"Compound": "CD33C"
}, {
"Number": "001",
"Status": "NCA",
"Compound": "CD33D"
}, {
"Number": "002",
"Status": "NCB",
"Compound": "CD33E"
}, {
"Number": "003",
"Status": "NCB",
"Compound": "CD20F"
}, {
"Number": "001",
"Status": "NCB",
"Compound": "CD20G"
}, {
"Number": "002",
"Status": "NCC",
"Compound": "CD20H"
}, {
"Number": "003",
"Status": "NCC",
"Compound": "CD33I"
}, {
"Number": "001",
"Status": "NCC",
"Compound": "CD33J"
}
];
});
&#13;
/* Put your css in here */
body {
background: #eee;
}
div.cart {
display: block;
height: 70px;
background: silver;
margin-left: 20px;
width: 200px;
padding: 5px 10px;
margin-bottom: 20px;
margin-top: 20px;
}
.cart h1 {
color: #fff;
line-height: 20px;
}
.item-list-wrapper {
height: 300px;
width: 740px;
border: 1px solid #ddd;
overflow-y: scroll;
margin-left: 20px;
}
.item-list {
height: 70px;
width: 100%;
margin-bottom: 10px;
box-shadow: 0 2px 2px rgba(0, 0, 0, 0.2);
border: 1px solid #fff;
background: #efefe4;
}
li {
display: inline-block;
list-style: none;
padding: 0 40px 40px 40px;
font-size: 24px;
}
.open {
height: 300px;
background: #fff;
}
&#13;
<!DOCTYPE html>
<html ng-app="plunker">
<head>
<meta charset="utf-8" />
<title>AngularJS Plunker</title>
<link data-require="bootstrap@*" data-semver="3.3.5" rel="stylesheet" href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" />
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css" />
<script data-require="angular.js@1.4.x" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.4.7/angular.min.js" data-semver="1.4.7"></script>
<script data-require="angular.js@1.4.x" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular-messages.js"></script>
<script src="angular-filter.min.js"></script>
<script src="app.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.7/angular-filter.js"></script>
</head>
<body ng-controller="MainCtrl">
<div ng-view=""></div>
<div>
<h2>Filter results</h2>
<select name="some_name" id="some_name" onchange="" class="form-control" ng-model="selectNumber" ng-options="data.Number for data in dataObject | unique: 'Number' ">
<option value="">Select Number</option>
</select>
<select name="some_name" id="some_name" onchange="" class="form-control" ng-model="selectStatus" ng-options="data.Status for data in dataObject | unique: 'Status' ">
<option value="">Select Status</option>
</select>
<select name="some_name" id="some_name" onchange="" class="form-control" ng-model="selectCompound" ng-options="data.Compound for data in dataObject | unique: 'Compound' ">
<option value="">Select Compound</option>
</select>
</div>
<div class="cart">
<h1>Cart: {{cartCount}}</h1>
</div>
<div class="item-list-wrapper">
<div class="item-list" ng-repeat="data in dataObject | filter: selectNumber | filter: selectStatus | filter: selectCompound" ng-click="activeRow($index)" ng-class="{'open':$index == selectedRow}">
<a id="anchor-{{$index}}"></a>
<ul>
<li>{{data.Number}}</li>
<li>{{data.Status}}</li>
<li>{{data.Compound}}</li>
<li>
<a href="" ng-click="addToCart()">Add to Cart</a>
</li>
</ul>
</div>
</div>
<!--item-list-wrapper -->
</body>
</html>
&#13;
答案 0 :(得分:2)
在模型中,选择属性值而不是完整对象:
ng-options="data.Number as data.Number for data in dataObject | unique: 'Number' "
此外,当您使用 angular-filter 时,请将filter
替换为filterBy
:
filterBy: ['Number']: selectNumber
这种替换的原因是它将所选值重置为空字符串时正确处理的情况。