Angular: CSS styling

http://angularjs.org/

by Alexandre Verri

HTML

<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="https://code.jquery.com/jquery-2.1.0.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/jquery-mousewheel/3.1.6/jquery.mousewheel.min.js"></script>
<h1>Paginated Scrolling</h1>
<p>You can scroll the table body using mouse.</p>
<p>There is a strange behavior: the very first rendering will show the table with incorrect number of lines (9 instead of 10). After filtering the table and removing the filter, the table will show  the correct number of itens: 10.</p>

<div ng-app="myApp" ng-controller="MainCtrl">    
    
    Filter: <input ng-model="numberFilter"></input>    
    <br/>
    
    <table>
        <thead>
            <td>Numbers</td>
        </thead>
        <tbody scroll>
            <tr ng-repeat="d in filtered = (data | filter:numberFilter) |
                    startFrom:currentPage*pageSize | 
                    limitTo:pageSize">
                <td>{{d}}</td>
            </tr>
        </tbody>
    </table>    
    
    <br/>
    Page {{currentPage+1}} of {{numberOfPages()}}
    <br/>
    Page Size: <input ng-model="pageSize" style="width: 4em"></input>
    <br/>
    Data Size: {{data.length}}
    <br/>
    Filtered Size: {{filtered.length}}
</div>

CSS

body {
    font-family: Georgia;
}

p {
    margin: 1em 0 1em 0;
}

h1 {
    font-size: 2em;    
    line-height: 2em;
}

thead td {
    font-weight: bold;
    border-bottom: solid 2px;
}

tbody tr td {
    width: 50px;
    background-color: #99FF33;
}

tbody {
    overflow-y:scroll;
}

JavaScript

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

// The custom scroll directive. It uses the jQuery Mousewheel plugin.
app.directive("scroll", function ($window) {
    return function ($scope, $element, $attrs) {
        jQuery($element).on('mousewheel', function (event) {
            if(event.deltaY < 0) {
                if($scope.currentPage < $scope.numberOfPages()-1) {
                    $scope.currentPage = $scope.currentPage + 1
                }
            }
            else {
                if($scope.currentPage > 0) {
                    $scope.currentPage = $scope.currentPage - 1;
                }                
            }            
            $scope.$apply();
            event.preventDefault();
        });
    };
});

app.controller('MainCtrl', function ($scope) {
    
    $scope.currentPage = 0;
    $scope.pageSize = 10;
    
    $scope.$watch('numberFilter', function() {
       $scope.currentPage = 0;
    });
    
    $scope.$watch('pageSize', function() {
       $scope.currentPage = 0;
    });
    
    //fake data
    var count = 1000;
    var data = [];
    for(var i=0; i<count; i++) {
        data[i] = i+1;
    }
    $scope.data = data;

    $scope.numberOfPages = function () {
        return Math.ceil($scope.filtered.length / $scope.pageSize);
    };
});

// The custom filter to be used in ng-repeat.
app.filter('startFrom', function () {
    return function (input, start) {
        console.log('Start: ' + start);
        start = +start;
        return input.slice(start);
    };
});