javascript - angularjs: passing value to custom filter to enable it on dropdown click -
i have data. default data should shown when user click on value in drop-down filter should start filtering data. following below approach.
my filter this
filter('myfilter', function() { return function(data, userinput) { var filteredarray = []; // show whole data if (true) {/*want insert enablefilter variable here*/ angular.foreach(data, function(dataobj, key) { console.log(dataobj); filteredarray.push(dataobj); }) } //this show filtered data else { angular.foreach(userinput, function(value, key) { angular.foreach(data, function(dataobj, key) { if (dataobj.type.indexof(value) > -1 && filteredarray.indexof(dataobj) == -1) { filteredarray.push(dataobj); } }) }); } return filteredarray; } });
to user click event using ng-change this
<select name="multipleselect" id="multipleselect" ng-model="data.multipleselect" ng-change=click() multiple> <option value="classa">class a</option> <option value="classb">class b</option> <option value="classc">class c</option> <option value="classd">class d</option> <option value="classe">class e</option>
how can push value (i.e. enablefilter) controller filter,(i tried - passing arguments angularjs filters didn't work ) when user click on drop down filter start filtering data. controller is-
angular.module('myapp', []) .controller('mycontroller', ['$scope', function($scope) { enablefilter = true; $scope.enablefilter = true; /*on user click*/ $scope.click = function() { console.log("user click"); enablefilter = false; console.log(enablefilter); }; $scope.data = { multipleselect: [] }; //data $scope.data = [{ "id": "1", "type": ["classa", "classb", "classc"], "name": "name 1" }, { "id": "2", "type": ["classb", "classc", "classe"], "name": "name 2" }, { "id": "3", "type": ["classc"], "name": "name 3" }, { "id": "4", "type": ["classd", "classc"], "name": "name 4" }, { "id": "5", "type": ["classa", "classb", "classc", "classd", "classe"], "name": "name 5" }]; }])
here plunker (http://plnkr.co/edit/qlse8wyfdrbvk1um6ry1?p=preview)
if (true) {/*want insert enablefilter variable here*/
change true false filters codde work.
the way wanted :
http://plnkr.co/edit/70jtmsfwtjq4nbnb7evy?p=preview
i have passed scope param filter , set true , when user selects drop down gets set false , filter starts applying.
Comments
Post a Comment