Angular.js - Filter an array in a table based on user input - HTML

Asked

Viewed 450 times

1

I want to make a table in which the td are the name and price of an item. And I want the user to be able to search for an item by its name through a input. This is already working. But the table is not appearing as I want, as I insert the array into a single td and I only get one row of items, while I want a column. I have so: inserir a descrição da imagem aqui

But I want the names to appear in column and not in line. My code is:

<div class="container">
    <form class="navbar-form" role="search">
        <div ng-app="myApp" ng-controller="namesCtrl">
            <p>Pesquise um menu</p>
            <p><input type="text" ng-model="test"></p>
    </form>
    <div class="row">
        <div class="table-responsive">
            <table class="table table-hover">
                <thead>
                    <tr>
                        <th>Nome</th>
                        <th>Preço</th>
                    </tr>
                </thead>
                <tbody id="myTable">
                    <tr>
                        <td ng-repeat="x in names | filter:test">{{ x }}</td>
                    </tr>
                </tbody>
            </table>
        </div>
    </div>
</div>

script:

<script>
    angular.module('myApp', []).controller('namesCtrl', function($scope) {
        $scope.names = [
            'Jani',
            'Carl',
            'Margareth',
            'Hege',
            'Joe',
            'Gustav',
            'Birgit',
            'Mary',
            'Kai'
        ];
    });
</script>

1 answer

2


Just change it here:

<tr>
    <td ng-repeat="x in names | filter:test">{{ x }}</td>
</tr>

for

<tr ng-repeat="x in names | filter:test">
    <td>{{ x }}</td>
</tr>

By creating the ng-repeat in the td, you repeat the column and not the tr (line)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.