Search code examples
javascriptangularjsangularjs-filter

angularjs filter using comparator with multiple filters


I am attempting to make an angularjs filter. I am filtering on three attributes. the first two are comparator false and the last is comparator true. is this possible without making a custom filter? I tried below but it throws an error.

|

 filter :
     {
        'PatientFullName' : vm.headerService.searchText,
        'Archived' : vm.headerService.Archived,
        'PhysicianID' : {vm.headerService.selectedPhysician.PhysicianID,true}
     }

here is the whole html

<div class="col-sm-6 col-md-4" ng-repeat="patient in vm.patientlist | orderBy: vm.headerService.currentSort.orderBy:vm.headerService.currentSort.sortDescending
     | filter :
     {
        'PatientFullName' : vm.headerService.searchText,
        'Archived' : vm.headerService.Archived,
        'PhysicianID' : {vm.headerService.selectedPhysician.PhysicianID,true}
     }">

here is the error

angular.js:14794 Error: [$parse:syntax] Syntax Error: Token '.' is unexpected, expecting [}] at column 263 of the expression [vm.patientlist | orderBy: vm.headerService.currentSort.orderBy:vm.headerService.currentSort.sortDescending | filter : { 'PatientFullName' : vm.headerService.searchText, 'Archived' : vm.headerService.Archived, 'PhysicianID' : {vm.headerService.selectedPhysician.PhysicianID,true} }] starting at [.headerService.selectedPhysician.PhysicianID,true} }].


Solution

  • {vm.headerService.selectedPhysician.PhysicianID,true} is not correct object. To perform filtering by different value of comparator you may chain filtering:

    <div class="col-sm-6 col-md-4" ng-repeat="patient in vm.patientlist | orderBy: vm.headerService.currentSort.orderBy:vm.headerService.currentSort.sortDescending
     | filter :
     {
        'PatientFullName' : vm.headerService.searchText,
        'Archived' : vm.headerService.Archived
     }
     | filter : 
    {        
        'PhysicianID' : vm.headerService.selectedPhysician.PhysicianID
    }: true">
    

    But better rewrite that into controller's method. It will improve the readability:

    <div class="col-sm-6 col-md-4" 
     ng-repeat="patient in vm.patientlist | 
                orderBy: vm.headerService.currentSort.orderBy
                       : vm.headerService.currentSort.sortDescending
               |filter : vm.myFilteringMethod">