0

I just got into AngularJS and I'm trying to filter a table. So far I create my table dynamically, which works fine.

HTML:

<div ng-app="tableApp" ng-controller="tableAppController">
       <input type="text" class="form-control" ng-model="searchKey" placeholder="search..."/>

                        <div class="table-responsive">
                            <table class="table table-striped" id="trackingTable">
                                <thead>
                                    <tr>
                                        <th>No.r.</th>
                                        <th>a</th>
                                        <th>b</th>
                                        <th>c</th>
                                    </tr>
                                </thead>
                                <tbody>
                                    <tr ng-repeat="x in deviceData">
                                        <td>{{ $index + 1 }}</td>
                                        <td>{{x.a}}</td>
                                        <td>{{x.b}}</td>
                                        <td>{{x.c}}</td>
                                    </tr>
                                </tbody>
                            </table>
                        </div>
                    </div>
                    <script src="scripts/AngularJS/tableAppController.js"></script>

tableAppController.js:

var app = angular.module('tableApp', []);
app.controller('tableAppController', function ($scope) {
$scope.deviceData = [{a: "123", b: "123", c: "01.01.2001"}, {a: "dummyDeviceID2", b: "dummyCarID98", c: "01.01.2001"}];
});

I'm now trying to implement an filter, which filters data from the table based on an user input from a textfield. But only when a user types something inside the textbox ofcourse. I started to change the line <tr ng-repeat="x in deviceData"> in the html file to <tr ng-repeat="x in deviceData | filter:{'a':searchKey}"> Why is this approach not working and how can I solve this problem?

2 Answers 2

1

You should be able to use keys on your search object to do this:

<input type="text" class="form-control" ng-model="searchKey.a" placeholder="search..."/>

<tr ng-repeat="x in deviceData | filter:searchKey">

See line 36 in the official example for more context: http://plnkr.co/edit/YUfG7yzxBQ0gT9bsnTN2?p=preview

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

Comments

0

Try this :

{{ filter_expression | filter : expression : comparator}}

<tr ng-repeat="x in deviceData | filter : searchKey">

AngularJS Official Documentation : filter

Plnkr : http://plnkr.co/edit/r6cchqzftaRiH543L1NE?p=preview

3 Comments

This will not filter only on the a property, as OP requested
@tyler, i checked and its working file. I updated the plnkr..please check.
Perfect -- type 98 into your plunkr's search box and you will see that it's not filtering on the a property only. However if you change line 10 to read ng-model="searchKey.a" (as suggested in my answer), it will filter on the a property only

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.