To filter using a property with several depths, we need to create our own filter. What I mean, we need to create our own function to filter the data in the object and return the desired object (filtered object).
For example, I need to filter data from the object below -
[ { "document":{ "documentid":"1", "documenttitle":"test 1", "documentdescription":"abcdef" } }, { "document":{ "documentid":"2", "documenttitle":"dfjhkjhf", "documentdescription":"dfhjshfjdhsj" } } ]
In HTML, we use ng-repeat to display a list of documents -
<div> //search input textbox <input ng-model="searchDocument" placeholder="Search"> </div> <div ng-repeat="document in documentList | filter: filteredDocument"> //our html code </div>
In the controller, we write a filter function to return the filtered object using two object properties: "documenttitle" and "documentdescription", an example code below:
function filterDocuments(document) { if($scope.searchDocument) { if(document.documentTitle.toLowerCase().indexOf($scope.searchDocument.toLowerCase()) !== -1 || document.document.shortDescription.toLowerCase().indexOf($scope.searchDocument.toLowerCase()) !== -1) { //returns filtered object return document } }else { return document; } }
Where $ scope.searchDocument is an area variable bound to a search text field (HTML input tag) where the user can enter text to search.
Ravindra Vairagi Sep 16 '16 at 4:57 2016-09-16 04:57
source share