javascript - How can you filter objects by an array in angularjs -
was wondering if can filter existing array array of filter.
so our original json was:
[ { "bookname": "harryporter", "year": "2000", "author":"j. k. rowling" }, { "bookname": "sleepless", "year": "2003", "author":"connie ann michael" }, { "bookname": "no man's land", "year": "1993" "author":"harold pinter" } ];
filter array:
[ "year":"1993" "bookname":"no man" ]
also there way dynamically add elements array when edit input, perhaps type in text ?
for example:
input
<div> <input type="text" ng-model={{ filterarray.insert("bookname":"value") }}>book name </div>
filter
<div ng-repeat="books in originaljson | filter: filterarray"> {{books.author}} </div>
i hope have explained question enough, cheers
if want filter multiple values, can pass in filter-object.
each key corresponds field-name , each value filters items based on corresponding key.
book name: <input type="search" ng-model="filterobj.bookname" /> year: <input type="search" ng-model="filterobj.year" /> <div ng-repeat="book in books | filter:filterobj">...</div>
see, also, short demo.
if want let user dymanically specify field filter by, can (although not common practice):
filter by: <input type="search" ng-model="dynamicfield" placeholder="field filter by" /> <input type="search" ng-model="filterobj[dynamicfield]" placeholder="value filter by" /> <div ng-repeat="book in books | filter:filterobj">...</div>
Comments
Post a Comment