1

I have an app that shows a customers health history, where each insertion has an ID. Looks like this:

enter image description here

I have a search feature, where as you type, i would like the filter the display by the IDs

<input id="episode-id-input" placeholder="Search by episode ID..." type="text" class="validate" ng-model="vm.searchEpisodeById">

The way my data gets loaded is like this, through an ng-repeat:

  <div ng-class="{
    'kept'     :person.pastAppointmentStatus == 'KEPT APPT.',
    'cancelled':person.pastAppointmentStatus == 'CANCELLED',
  }" class="past-appointments-meta-holder">


      <div class="meta-holder-title">
        <p class="meta-holder-title-name">
          {{person.activeReferralsType}}
        </p>
        <p class="meta-holder-title-date">
          {{person.activeReferralsDate| date:'MMMM dd, yyyy'}} | {{person.activeReferralsTime}}
        </p>
        <p class="meta-holder-title-status">
          {{person.pastAppointmentStatus}}
        </p>
        <p class="episode-id">
          EPISODE ID: {{person.episodeId}}
        </p>
      </div>

The Object looks like this:

, "historyContainer" : [
                {
                   "activeReferralsType" : "new patient diabetic eye exam"
                  , "episodeId" : "9876"
                  , "activeReferralsDate" : new Date('1998-10-19')
                  , "activeReferralsTime" : "2:00PM"
                  , "referredFromName" : "Karen Rush, MD"
                  , "referredFromAddressFacilityName" : "Random Family Practice"
                  , "referredFromAddressFacilityAddressLineOne" : "1108 Cedar Road"
                  , "referredFromAddressFacilityAddressLineTwo" : "Chesapeake"

                }
                ,{
                   "activeReferralsType" : "new patient diabetic eye exam"
                  , "episodeId" : "87678678"
                  , "activeReferralsDate" : new Date('1998-10-19')
                  , "activeReferralsTime" : "2:00PM"
                  , "referredFromName" : "Karen Rush, MD"
                  , "referredFromAddressFacilityName" : "Random Family Practice"
                  , "referredFromAddressFacilityAddressLineOne" : "1108 Cedar Road"
                  , "referredFromAddressFacilityAddressLineTwo" : "Chesapeake"

                }
              ]

I have never used a filter function in angular before, could someone please show me how I would filter the ng-repeat after it has loaded, to show only the divs that have the ID typed in by the user?

2 Answers 2

2

You can do something like this. Refer to this fiddle for full code.

  <div class="meta-holder-title" ng-repeat="person in person | filter: { episodeId: searchEpisodeById }">
Sign up to request clarification or add additional context in comments.

4 Comments

The problem with this approach is that it will also filter on basis of activeReferralsType rather than just filtering on basis of ids
@madhur I got what you mean. I have updated the answer to cater for the situation.
Thanks! it looks to be correct, however i am getting the error "Error: [filter:notarray] Expected array but received: 0" even though our objects are the same...
I removed the "track by index" in the ng-repeat and it fixed the issue, but i need the index later on... do you know how to compensate?
0

You would like to do it this way, hope it helps

function myCtrl($scope) {
  $scope.persons = [
    {episodeId:1},
    {episodeId:2},
    {episodeId:3},
    {episodeId:4},
    {episodeId:5},
    {episodeId:6},
    {episodeId:7}
    ];
}
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app >
<div ng-controller="myCtrl">
  <input id="episode-id-input" placeholder="Search by episode ID..." type="text" class="validate" ng-model="search.episodeId">
  <div ng-repeat="person in persons| filter:search">
          <p class="episode-id">
            EPISODE ID: {{person.episodeId}}
          </p>
      </div>
    </div>
 </div>

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.