Sanjeev Singh
Sanjeev Singh

Reputation: 4066

Filter not working with dynamic input and ng-repeat

I am trying to filter records in my repeater on the basis of a input text box but it is working unexpectedly and not filtering the correct record.

My html code:

<input ng-model="txtTest" type="text" class="form-control" id="txtTest"></input>

<table>
    <tr ng-repeat="SampleData in SampleInfo|MaxAmountFilter:txtTest" ng-form="SampleForm">
        <td>
            <div>
                <input type="text" name="DateOfLoss" ng-Model="SampleData.DateOfLoss" </input>
            </div>
        </td>
        <td>
            <div>
                <input type="text" name="LossDesc" ng-model="SampleData.LossDesc"> </input>
            </div>
        </td>
    </tr>
</table>

My Directive:

    AngularApp.filter('MaxAmountFilter', function () {
        return function(AmountArray, AmountEntered ) {
          var filteredAmount = [];

          angular.forEach(AmountArray, function (amt) {
              if (AmountEntered >= amt.LossAmount) {
              filteredAmount.push(amt);
            }
          });

          return filteredAmount;

        };
    });

My scope data -

    $scope.SampleInfo = [
        { "DateOfLoss": "01/01/2014", "LossAmount": "100", "LossDesc": "sasa"}, 
        { "DateOfLoss": "01/01/2015", "LossAmount": "500", "LossDesc": "ssss" }, 
        { "DateOfLoss": "01/01/2011", "LossAmount": "102", "LossDesc": "ddd" }, 
        { "DateOfLoss": "01/01/2012", "LossAmount": "700", "LossDesc": "hhhh"}, 
        { "DateOfLoss": "01/01/2010", "LossAmount": "250", "LossDesc": "dsdsd"}


];

This is working fine for static values ie when I do not filter on the basis of input values of text box. What is wrong with the code. Please suggest!

EDIT: Plnkr here Type 101, 102 then it works but when you type 10000 in the text box It does not work. Now remove MaxAmountFilter:txtTest from the filter and simply add MaxAmountFilter:10000 This will work.

Upvotes: 3

Views: 1495

Answers (1)

JB Nizet
JB Nizet

Reputation: 691785

Your input doesn't have an ng-model="txtTest". So the text entered in the amount is not saved in txtTest and txtTest is thus undefined.

EDIT:

another problem is that you're using strings to represent numbers. See this updated plunkr. Using an input of type number, and replacing your strings in the objects by numbers make everything go fine.

Upvotes: 2

Related Questions