Carol.Kar
Carol.Kar

Reputation: 5355

FilterText with angularjs

I have a simple searchController:

testapp.controller("searchController", function($scope, $rootScope, $http, $location) {

    $scope.filterText = null;

    var load = function() {
        console.log('call load()...');

        var url = 'product.json';

        if($rootScope && $rootScope.appUrl) {
            url = $rootScope.appUrl + '/' + url;
        }
        console.log(url);

        $http.get(url)
        .success(function(data, status, headers, config) {
            $scope.product = data;
            angular.copy($scope.product, $scope.copy);
        });
    }

    load();

}); 

As you can see I have declared filterText. My view looks like that:

<div class="container main-frame" ng-app="testapp"
    ng-controller="searchController" ng-init="init()">
    <h1 class="page-header">Products</h1>
    <!-- Filter Start -->
    <div class="search-box row-fluid form-inline">
        <label>Filter: </label> <input type="text" ng-model="filterText" /> 
    </div>
    <div class="results-top"></div>
    <!-- Filter End -->
    <table class="table">
        <thead>
            <tr>
                <th width="25px">ID</th>
                <th>TITLE</th>
                <th>PRICE</th>
                <th>Description</th>
                <th width="50px"></th>
            </tr>
        </thead>
        <tbody>
            <tr ng-repeat="p in product track by p.id | filter: filterText">
                <td>{{p.id}}</td>
                <td>{{p.title}}</td>
                <td>{{p.price}}</td>
                <td>{{p.description}}</td>
            </tr>
        </tbody>
    </table>
    <!-- ng-show="user.username" -->
    <p>
</div>

My problem is nothing happens when putting something into my input. Any suggestions how to fix that?

I appreciate your answer!

Upvotes: 0

Views: 181

Answers (1)

Artyom Pranovich
Artyom Pranovich

Reputation: 6962

Issue related with track by expression.

Please, try add track by at the end of ng-repeat:

<tr ng-repeat="p in product | filter: filterText track by p.id">

Or remove track by at all.

I create JSFiddle with correct code. Please see:

JSFiddle

Upvotes: 1

Related Questions