比较 Angularjs 中 2 个嵌套数组之间的对象并显示相应的匹配数据
Comparing objects between 2 nested arrays in Angularjs and display respective matching data
我有两个数组:第一个数组 empdata
保存 empid
和该员工参加的活动,第二个数组保存活动详细信息。
我需要将数组中的 empid
与用户输入进行比较,并使用可折叠的 jQuery 显示各个员工参加的特定事件详细信息。还可以找到总价。
$scope.empdata=[];
$scope.data = [];
//first array data
$scope.empdata.push({
empid:'empid_1',
events:[{
event:'First Event'
},
{
event:'Second Event'
}
]})
$scope.empdata.push({
empid:'empid_2',
events:[{
event:'First Event'
},
{
event:'Second Event'
},
{
event:'Third Event'
}]
})
//second array data
$scope.data.push({
event:'First Event',
date: '10-jun-2015',
type:[{
name: 'Hotel Booking',
price: 400.00
},{
name: 'Flight',
price: 400.00
},{
name: 'Honorarium',
price: 900.00
}]
})
$scope.data.push({
event:'Second Event',
date: '27-july-2015',
type:[{
name: 'Hotel Booking',
price: 530.00
},{
name: 'Train',
price: 400.00
},{
name: 'Honorarium',
price: 600.00
}]
})
$scope.data.push({
event:'Third Event',
date: '20-aug-2015',
type:[{
name: 'Hotel Booking',
price: 910.00
},{
name: 'Flight',
price: 500.00
},{
name: 'Honorarium',
price: 1500.00
}]
})
提前致谢!
您可以为此使用 custom filter
。
看下面的例子
var app = angular.module('app', []);
app.filter('findobj', function() {
return function(dataobj, selected) {
return dataobj.filter(function(data) {
return (selected || []).some(function(s) {
return data.event === s.event;
});
});
};
});
app.controller('ctrl', function($scope) {
$scope.empdata = [];
$scope.data = [];
$scope.empdata.push({
empid: 'empid_1',
events: [{
event: 'First Event'
}, {
event: 'Second Event'
}]
})
$scope.empdata.push({
empid: 'empid_2',
events: [{
event: 'First Event'
}, {
event: 'Second Event'
}, {
event: 'Third Event'
}]
})
$scope.empdata.push({
empid: 'empid_3',
events: [{
event: 'Forth Event'
}, {
event: 'Fifth Event'
}]
})
//second array data
$scope.data.push({
event: 'First Event',
date: '10-jun-2015',
type: [{
name: 'Hotel Booking',
price: 400.00
}, {
name: 'Flight',
price: 400.00
}, {
name: 'Honorarium',
price: 900.00
}]
})
$scope.data.push({
event: 'Second Event',
date: '27-july-2015',
type: [{
name: 'Hotel Booking',
price: 530.00
}, {
name: 'Train',
price: 400.00
}, {
name: 'Honorarium',
price: 600.00
}]
})
$scope.data.push({
event: 'Forth Event',
date: '27-july-2015',
type: [{
name: 'Hotel Booking',
price: 530.00
}, {
name: 'Train',
price: 400.00
}, {
name: 'Honorarium',
price: 600.00
}]
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<select ng-options="emp.events as emp.empid for emp in empdata" ng-model="selected"></select>
<hr/>Selected: {{selected}}
<hr/>
<div ng-repeat="d in data | findobj:selected ">{{d}}</div>
</div>
</div>
我有两个数组:第一个数组 empdata
保存 empid
和该员工参加的活动,第二个数组保存活动详细信息。
我需要将数组中的 empid
与用户输入进行比较,并使用可折叠的 jQuery 显示各个员工参加的特定事件详细信息。还可以找到总价。
$scope.empdata=[];
$scope.data = [];
//first array data
$scope.empdata.push({
empid:'empid_1',
events:[{
event:'First Event'
},
{
event:'Second Event'
}
]})
$scope.empdata.push({
empid:'empid_2',
events:[{
event:'First Event'
},
{
event:'Second Event'
},
{
event:'Third Event'
}]
})
//second array data
$scope.data.push({
event:'First Event',
date: '10-jun-2015',
type:[{
name: 'Hotel Booking',
price: 400.00
},{
name: 'Flight',
price: 400.00
},{
name: 'Honorarium',
price: 900.00
}]
})
$scope.data.push({
event:'Second Event',
date: '27-july-2015',
type:[{
name: 'Hotel Booking',
price: 530.00
},{
name: 'Train',
price: 400.00
},{
name: 'Honorarium',
price: 600.00
}]
})
$scope.data.push({
event:'Third Event',
date: '20-aug-2015',
type:[{
name: 'Hotel Booking',
price: 910.00
},{
name: 'Flight',
price: 500.00
},{
name: 'Honorarium',
price: 1500.00
}]
})
提前致谢!
您可以为此使用 custom filter
。
看下面的例子
var app = angular.module('app', []);
app.filter('findobj', function() {
return function(dataobj, selected) {
return dataobj.filter(function(data) {
return (selected || []).some(function(s) {
return data.event === s.event;
});
});
};
});
app.controller('ctrl', function($scope) {
$scope.empdata = [];
$scope.data = [];
$scope.empdata.push({
empid: 'empid_1',
events: [{
event: 'First Event'
}, {
event: 'Second Event'
}]
})
$scope.empdata.push({
empid: 'empid_2',
events: [{
event: 'First Event'
}, {
event: 'Second Event'
}, {
event: 'Third Event'
}]
})
$scope.empdata.push({
empid: 'empid_3',
events: [{
event: 'Forth Event'
}, {
event: 'Fifth Event'
}]
})
//second array data
$scope.data.push({
event: 'First Event',
date: '10-jun-2015',
type: [{
name: 'Hotel Booking',
price: 400.00
}, {
name: 'Flight',
price: 400.00
}, {
name: 'Honorarium',
price: 900.00
}]
})
$scope.data.push({
event: 'Second Event',
date: '27-july-2015',
type: [{
name: 'Hotel Booking',
price: 530.00
}, {
name: 'Train',
price: 400.00
}, {
name: 'Honorarium',
price: 600.00
}]
})
$scope.data.push({
event: 'Forth Event',
date: '27-july-2015',
type: [{
name: 'Hotel Booking',
price: 530.00
}, {
name: 'Train',
price: 400.00
}, {
name: 'Honorarium',
price: 600.00
}]
})
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app">
<div ng-controller="ctrl">
<select ng-options="emp.events as emp.empid for emp in empdata" ng-model="selected"></select>
<hr/>Selected: {{selected}}
<hr/>
<div ng-repeat="d in data | findobj:selected ">{{d}}</div>
</div>
</div>