Reputation: 417
Listed all the array of object as a lists using ng-repeat and added checkbox for each value.
Here I want to filter the checkbox and form a new JSON on click Apply Filter based on check/uncheck the value(Checkbox's).
Actual
I've tried adding the selected and unselected checkbox in the models scope object($scope.models) by concatenating the filter name and its values
(i.e) : data1(filter name)+1(value) = data11
On clicking apply filter looping the existing filters array and compare with the model object and push the matches in the new array.
Apply Filter function
HTML
<li ng-repeat="(key, filter) in filters">
<a href="" data-target="#{{filter.name}}" data-toggle="collapse" aria-expanded="{{enableShow.indexOf(filter.name) > -1 ? true : false}}">
{{filter.name}}
</a>
<ul class="collapse list-unstyled" id="{{filter.name}}" ng-class="{show : enableShow.indexOf(filter.name) > -1}">
<li ng-repeat="v in filter.values">
<label class="w-100" ng-show="filter.type == 'CheckBox'">
<span class="ml-3 p-1 d-block">
{{v.value}}
<input type="checkbox" ng-model="models[filter.name + v.value]" class="pull-right mt-1 ml-1" style="margin-right: 7%" />
</span>
</label>
</li>
</ul>
JS
$scope.applyFilters = function() {
var arr = [];
_.map($scope.filters, function(d) {
_.map(d.values, function(v) {
var name = d.name + v.value;
for (key in $scope.models) {
if (key == name) {
arr.push({
name: d.name,
values: v
});
}
}
});
});
console.log(arr);
};
Expected
On clicking apply filter want to form a new JSON which contains only the selected values in its respective object.
{
"results": [
{
"name": "data1",
"type": "CheckBox",
"values": [
{
"value": "1"
},
{
"value": "4"
}
]
},
{
"name": "data2",
"type": "CheckBox",
"values": [
{
"value": "1"
}
]
},
{
"name": "data5",
"type": "CheckBox",
"values": [
{
"value": "3"
}
]
},
{
"name": "data6",
"type": "CheckBox",
"values": [
{
"value": "2"
}
]
}
]
}
Thanks in advance.
Upvotes: 2
Views: 226
Reputation: 191976
Iterate the original data structure with _.map()
and transform it to the requested format. Use _.filter()
or _.reject()
to remove sections without selected values (plunker):
$scope.applyFilters = function() {
var arr = _($scope.filters)
.map(function(o, i) {
var section = _.pick(o, ['name', 'type']);
section.values = _(o.values)
.filter(function(v, j) {
return $scope.models['data' + (i + 1) + (j + 1)];
})
.map(_.partialRight(_.pick, 'value'))
.value();
return section;
})
.reject(function(o) { return _.isEmpty(o.values); })
.value();
console.log(arr);
};
Upvotes: 0
Reputation: 1487
You could use obj.hasOwnProperty(prop)
to contains only the selected values in its respective object.
Just change your applyFilters
to this:
$scope.applyFilters = function() {
var arr = [];
_.map($scope.filters, function(d) {
_.map(d.values, function(v) {
var name = d.name + v.value;
for (key in $scope.models) {
if (key == name && $scope.models.hasOwnProperty(key) && $scope.models[key]) {
arr.push({
name: d.name,
values: v
});
}
}
});
});
console.log(arr);
};
plunker: https://next.plnkr.co/edit/waIwyXgEkdfPtybq
Hope it helps!
Upvotes: 0