2

how to create Custom filter angularjs javascript controller side? i would like to search in array called segments by SegmentId, to create filter that do foreach on segments array search by SegmentId -

    //Controller

    $scope.GetSegmentDetails = function (id) {
        $scope.SegmentName = $filter('SegmentById')($scope.segments,id);
    }


    //Filter
    app.filter('SegmentById', function () {
        return function (input, searchPerson) {
        if (!searchPerson)
            return input;

        var results = [];
        angular.forEach(input, function (person) {
        }
    });

    return results;
}

});

5
  • please post an example model (i.e. $scope.segments) Commented Nov 4, 2014 at 15:21
  • I don't understand you want to create a filter to put logic in the controller? then you don't need a filter you can do it directly in the controller, filters are used to filter displayed data binded in HTML Commented Nov 4, 2014 at 15:22
  • @boindiil [{"SegmentId":"1","Description":"hod Registrations"}, {"SegmentId":"2","Description":"hod Inactive"}, {"SegmentId":"3","Description":"hod testUpd"}, {"SegmentId":"8","Description":"hod test"}, {"SegmentId":"1111","Description":"hod Release"}, {"SegmentId":"12","Description":"hod Requests"}, {"SegmentId":"13","Description":"hod Welcome Back"}] Commented Nov 4, 2014 at 15:31
  • @Charlie yes i know i can do it directly as private function, there is also a need to do it on the page it self, the same functionality... Commented Nov 4, 2014 at 15:32
  • oh ok well have you tried to import the filter object of your angular module in your controller? because I am not sure components in a module know about each others implicitly not sure how to do it but you should find smth with google Commented Nov 4, 2014 at 15:43

2 Answers 2

4

You dont have to write your one filter to filter by SegmentId. Here you have an example

function MyCtrl($scope, $filter) {
  $scope.data = [{"SegmentId":"1","Description":"hod Registrations"}, {"SegmentId":"2","Description":"hod Inactive"}, {"SegmentId":"3","Description":"hod testUpd"}, {"SegmentId":"8","Description":"hod test"}, {"SegmentId":"1111","Description":"hod Release"}, {"SegmentId":"12","Description":"hod Requests"}, {"SegmentId":"13","Description":"hod Welcome Back"}]
  
  $scope.filterData = function(segmentId) {
    return $filter('filter')($scope.data, { SegmentId: segmentId });
  }
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="MyCtrl">
  Full:
  <ul>
    <li ng-repeat="Segment in data">
      {{Segment.SegmentId}}
    </li>
  </ul>
  Filtered in View:
  <ul>
    <li ng-repeat="Segment in data | filter:{SegmentId:1111}">
      {{Segment.SegmentId}}
    </li>
  </ul>
  Filtered in Controller:
  <ul>
    <li ng-repeat="Segment in filterData(1111)">
      {{Segment.SegmentId}}
    </li>
  </ul>
</div>

Sign up to request clarification or add additional context in comments.

4 Comments

Great thanks! working great i have another issue with the order of loading module into controller. which mean $filter suppose to be second after $scope
Take a look a this link
@boindiil How to achieve SegmentId !== segmentId using $filter ?
@gauravbhavsar You could use the negation within filter
1

Only need to add in your controller the filter dependency and then call it.

app.controller("YourController", ['$scope', '$filter', function ($scope, $filter){

$filter('filterName')($args));

}]);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.