Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <html ng-app="stickyDividersDemo">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no, width=device-width">

  <title>Sticky Dividers</title>
  <link href="https://code.ionicframework.com/1.0.0-beta.11/css/ionic.css" rel="stylesheet">

</head>

<body ng-controller="DemoController">
  <ion-header-bar class="bar-positive">
    <h1 class="title">Sticky Dividers</h1><button ng-click="buttonClick()"> change list</button>
  </ion-header-bar>

  <ion-content delegate-handle="scrollHandle" on-scroll="checkPosition()" sticky watch-variable="transactions">
      <div class="list">
          <div class="transaction-list item-container"
               ng-repeat="group in transactions  | groupBy: 'Day' | toArray:true | orderBy:'$key' : true   "
               id="gr_k_{{group.$key}}" on-repeat-done="eventTransactionsRendered">
              <div class="item item-divider">{{::group.$key}}</div>
              <div class="item transaction-item" ng-repeat="transaction in ::group">
                  <a ng-click="open(transaction)" class="navigate-right">
                      {{::transaction.name}}
                  </a>
              </div>
          </div>
      </div>
  </ion-content>
  <script src="https://code.ionicframework.com/1.0.0-rc.1/js/ionic.bundle.min.js"></script>
  <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-filter/0.5.4/angular-filter.min.js"></script>
</body>
</html>

              
            
!

CSS

              
                .item-container {
    margin: 0;
    min-height: 1px;
    overflow: hidden;
    padding: 36px 0 0 0;
    position: relative;
    width: 100%;
}
.list-sticky .item-divider {
    bottom: auto;
    left: 0;
    line-height: 20px;
    min-height: 1px;
    position: absolute;
    right: 0;
    top: 0;
    z-index: 1000;
}

.item-divider.item-hidden {
    visibility: hidden;
}
.item-container.item-animated .item-divider {
    bottom: 0;
    top: auto;
}
              
            
!

JS

              
                angular.module('angular.ionic-sticky-list-dividers', ['ionic','angular.filter'])
.directive("onRepeatDone", function ($timeout) {
    return {
        restriction: 'A',
        link: function ($scope, element, attributes) {
            //console.log("onRepeatDone");
            if ($scope.$last) {
                  $scope.$emit("ng_repeat_done", element);
            };
        }
    }
})
.directive('sticky', function ($ionicScrollDelegate, $timeout,$interval, $compile) {
    var options,
        defaults = {
            classes: {
                animated: 'item-animated',
                container: 'item-wrapper',
                hidden: 'item-hidden',
                stationaryHeader: 'item item-divider'
            },
            selectors: {
                groupContainer: 'item-container',
                groupHeader: 'item-divider',
                stationaryHeader: 'div'
            }
        };


 return {
        restrict: 'A',

        link: function (scope, element, attrs, ctrl) {
            scope.container_el=attrs.delegateHandle;

 			scope.$watch(
                attrs.watchVariable,
                    function handleModelChange( newValue ) {
                        //we run this in a loop so we check to ensure the list is fully rendered
                    	var stop=$interval(function() {
                    		if (scope.list_render_completed===true){
                    			scope.list_render_completed=false;
                    				$timeout(function(){
                    					console.log("updated list");
            						 scope.updateItems();
            						},500);
                    			$interval.cancel(stop);
                    	}	
                    	});
    
                    }
                );
            scope.$on('ng_repeat_done', function (domainElement) {
            scope.list_render_completed=true;
               
            });

            var items = [],
                options = angular.extend(defaults, attrs),
                $element = angular.element(element),
                $fakeHeader = angular.element('<div class="' + options.classes.stationaryHeader + '"/>');



            scope.updateItems = function () {
              //  console.log("updateItems");
               var 
                $groupContainer = angular.element($element[0].getElementsByClassName(options.selectors.groupContainer));

                $element.addClass('list-sticky');

                angular.element($element[0].getElementsByClassName('list')).addClass(options.classes.container);

                $element.prepend($fakeHeader);

                items = [];
                angular.forEach($groupContainer, function (elem, index) {

                    var $tmp_list = $groupContainer.eq(index);

                    var $tmp_listHeight = $tmp_list.prop('offsetHeight'),
                    $tmp_listOffset = $tmp_list[0].getBoundingClientRect().top,
                    $tmp_header = angular.element($tmp_list[0].getElementsByClassName(options.selectors.groupHeader)).eq(0)
                            ;
                    if ($tmp_header.text().indexOf("{{")==-1) //not found
                    items.push({
                        'list': $tmp_list,
                        'header': $tmp_header,
                        'listHeight': $tmp_listHeight,
                        'headerText': $tmp_header.text(),
                        'headerHeight': $tmp_header.prop('offsetHeight'),
                        'listOffset': $tmp_listOffset,
                        'listBottom': $tmp_listHeight +$tmp_listOffset
                        });
                    });

                $fakeHeader.text(items[0].headerText);
            }




            scope.checkPosition = function () {
                var i = 0,
                    topElement, offscreenElement, topElementBottom,
                    currentTop = $ionicScrollDelegate.$getByHandle(scope.container_el).getScrollPosition().top;

                topElement = items[0];
                var delta = items[0].listOffset;
                //while ((items[i].listOffset - currentTop) <= 0) {
                while ((items[i].listOffset - currentTop) <= delta) {
                //while ((items[i].listOffset ) <= 50) {
                    topElement = items[i];
                    topElementBottom = -(topElement.listBottom - currentTop);

                    if (topElementBottom < -topElement.headerHeight) {
                        offscreenElement = topElement;
                    }

                    i++;

                    if (i >= items.length) {
                        i--;
                        break;
                    }
                }

                //$fakeHeader.text(topElement.headerText);
                //delta += 30;
                if (topElementBottom) {
                    if (topElementBottom + delta < 0 && topElementBottom + delta > -topElement.headerHeight) {
                        $fakeHeader.addClass(options.classes.hidden);
                        //angular.element(topElement.list).addClass(options.classes.animated);
                        var listElem = document.getElementById(angular.element(topElement.list).attr("id"));
                        listElem.className = listElem.className.replace(options.classes.animated, ""); // first remove the class name if that already exists
                        listElem.className = listElem.className + " " + options.classes.animated;
                    } else {
                        $fakeHeader.removeClass(options.classes.hidden);
                        if (topElement) {
                            //angular.element(topElement.list).removeClass(options.classes.animated);
                            var listElem = document.getElementById(angular.element(topElement.list).attr("id"));
                            listElem.className = listElem.className.replace(options.classes.animated, "");
                        }
                    }
                    $fakeHeader.text(topElement.headerText);
                }
            }
        }

    }
});

angular.module('stickyDividersDemo', ['ionic','angular.filter','angular.ionic-sticky-list-dividers'])
.controller('DemoController', ['$scope','$filter', function($scope,$filter) {
    $scope.transactions = [
        {"Day":"day1","name":"test"},
        {"Day":"day1","name":"test"},
        {"Day":"day2","name":"test"},
        {"Day":"day2","name":"test"},
        {"Day":"day2","name":"test"},
        {"Day":"day3","name":"test"}
    ];


    $scope.transactions2=[
        {"Day":"day1","name":"test1"},
        {"Day":"day1","name":"test2"},
        {"Day":"day2","name":"test3"},
        {"Day":"day2","name":"test4"},
        {"Day":"day2","name":"test5"},
        {"Day":"day3","name":"test6"}
    ];
    $scope.buttonClick=function(){
        console.log('variable changed');
        $scope.transactions=$scope.transactions2;
        //$scope.$apply();
    };
}]);
              
            
!
999px

Console