I'm trying to make a directive that I can make a virtual scroll, as the user scrolls the table, the table deletes the "old" views and adds "new" views, like repeating a collection, but I failed, I think I did not understand the math behind it, maybe Does anyone help me?
this is my directive code:
BaseModule.directive('myScroll', function() { return { restrict:"A", scope:{ rows:"=", headers:"=" }, link: function(scope,el) { var scrollTop = 0; var scrollLeft = 0; angular.element(el).on('scroll',function(){ scrollTop = $(el).scrollTop(); scrollLeft = $(el).scrollLeft(); $(el).parent().find(".header").scrollLeft(scrollLeft); var height = $(el).height(); var numberOfRows = height/23; var initialRow = scrollTop/23; var html = ""; for(i=0; i<numberOfRows;i++){ var row = scope.rows[i+initialRow]; html = html + addRow(row,i+initialRow); } $(el).find('.tbody-scroll').append(html); }); scope.$watch('rows',function(rows){ var height = $(el).height(); var numberOfRows = height/23; var initialRow = scrollTop/23; var html = ""; for(i=0; i<numberOfRows;i++){ var row = rows[i+initialRow]; html = html + addRow(row,i+initialRow); } $(el).find('.tbody-scroll').append(html); }); var addRow = function(row,index){ var html = ""; var pos = 0; var totalWidth = 0; angular.forEach(row,function(col){ var width = scope.headers[pos].width; totalWidth = totalWidth + width; html = html + "<span style='width:"+width+"px'>"+col.value+"</span>"; pos++; }); html = "<div class='row' style='top:"+index*23+"px;width:"+totalWidth+"px;'>"+html; html = html + "</div>"; return html; }; } }; }); <div class="mTable"> <div class="header" ng-style="headerWidth(headers())"> <span ng-repeat="header in headers()" ng-style="widthStyle(header)"> {{::header.label}} </span> </div> <div class="tbody-container" my-scroll headers="headers()" rows="rows()"> <div class="tbody-scroll" ng-style="scrollHeight(rows(),headers())"></div> </div> </div>
source share