How to select only visible items in search results

I have a list of items with a check box for each item. I added a checkAll button to select all my items.

http://jsbin.com/dipumemo/3 ( edit )

How can I improve my user interface and select only visible elements when I click the "checkAll" button?

my workflow:

  • add 1 to the filter (hide with display: no, list 2)
  • click on checkboxAll
  • clear filter
  • Check only the list 1.

enter image description hereenter image description here

My html:

<div ng-app="listsModule">
    <div ng-controller="listsController">
        <input type="text" id="filter_lists" ng-model="search" placeholder="Search a list">

        <table>
            <thead>
            <tr>
                <th><input type="checkbox" ng-model="checkAll"/></th>
                <th>List name</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="list in lists | filter:search | orderBy:'name'">
                <td><input type="checkbox" ng-model="list.isSelected" ng-checked="checkAll"></td>
                <td>{{ list.name }}</td>
                <td>{{ list.isSelected }}</td>
            </tr>
            </tbody>
        </table>
    </div>
</div>

My javascript:

var app = angular.module('listsModule', []);

app.controller('listsController', function ($scope) {
    $scope.lists = [
        {"id": 39, "name": "list1", "isSelected": false},
        {"id": 40, "name": "list2", "isSelected": false}
    ]
});
+3
source share
1 answer

. . checkAll .

HTML:

<div ng-app="listsModule">
    <div ng-controller="listsController">
        <input type="text" id="filter_lists" ng-model="search" placeholder="Search a list">

        <table>
            <thead>
            <tr>
                <th><input type="checkbox" ng-model="checkAll"/></th>
                <th>List name</th>
            </tr>
            </thead>
            <tbody>
            <tr ng-repeat="list in filtered = (lists | filter:{name:search}) | orderBy:'name'">
                <td><input type="checkbox" ng-model="list.isSelected"></td>
                <td>{{ list.name }}</td>
                <td>{{ list.isSelected }}</td>
            </tr>
            </tbody>
        </table>
        <div>Selected:
            <div ng-repeat="list in filtered | filter:{isSelected:true} | orderBy:'name'">
                {{list.name}}
            </div>
        </div>
    </div>  
</div>

JavaScript:

var app = angular.module("listsModule", []);

app.controller('listsController', ['$scope', function($scope) {
    $scope.lists = [
        {"id": 39, "name": "Fluffy", "isSelected": false},
        {"id": 40, "name": "Muffy", "isSelected": false},
        {"id": 41, "name": "Dingo Jr.", "isSelected": false},
        {"id": 42, "name": "Bertram", "isSelected": false}
    ];

    $scope.$watch('checkAll', function(newValue, oldValue) {
        if ($scope.filtered)
        for (i=0; i<$scope.filtered.length; i++) {
            $scope.filtered[i].isSelected = newValue;
        }
    });
}]);
+2

All Articles