material icon indicating copy to clipboard operation
material copied to clipboard

md-virtual-repeat does not work without predefined height

Open uriva opened this issue 11 years ago • 42 comments

codepen: http://codepen.io/uriva/pen/doxdVP

uriva avatar Aug 25 '15 08:08 uriva

I think md-virtual-repeat-container should have fixed height

Pusnow avatar Aug 25 '15 15:08 Pusnow

Yup. That was the issue. Perhaps a doc clarification on that point?

uriva avatar Aug 25 '15 16:08 uriva

I referred to official doc's sample codes

Pusnow avatar Aug 25 '15 17:08 Pusnow

+1 Would be nice to figure out a way to allow flexable heights.

ghost avatar Sep 03 '15 18:09 ghost

It's on my to-do list. It's tricky because any solution comes with performance trade-offs.

kseamon avatar Sep 08 '15 18:09 kseamon

+1 This tripped me up for a while, a note on the docs would be cool.

0xdevalias avatar Sep 25 '15 05:09 0xdevalias

Here's my workaround in the mean time:

this.getListHeight = function() {
  return {height: '' + ($window.innerHeight - 72) + 'px'};
};
$window.addEventListener('resize', onResize);
function onResize() {
  $scope.$digest();
}
$scope.$on('$destroy', function() {
  $window.removeEventListener('resize', onResize);
});

Then in the HTML

<md-virtual-repeat-container id="vertical-container" ng-style="ctrl.getListHeight()">
...
</md-virtual-repeat-container>

colinskow avatar Sep 26 '15 03:09 colinskow

@colinskow - Thanks, it's close to working, it works for me on refresh, but not on regular state-changes (Note that I'm measuring the parent node of the virtual list, not the body like in your example).

A huge +1 for making virtual-repeat flexable.

Madd0g avatar Oct 10 '15 08:10 Madd0g

@Madd0g - I actually refactored it a little bit and it seems to work better. At least on Safari and Chrome it always appears to be the right size.

this.listStyle = {
      height: ($window.innerHeight - 112) + 'px'
    };
    $window.addEventListener('resize', onResize);
    function onResize() {
      self.listStyle.height = ($window.innerHeight - 112) + 'px';
      if(!$scope.$root.$$phase) $scope.$digest();
    }
<md-virtual-repeat-container id="vertical-container" ng-style="ctrl.listStyle">
...
</md-virtual-repeat-container>

colinskow avatar Oct 10 '15 17:10 colinskow

@colinskow - that does work better, but it seems like the internal md-list doesn't get resized to the necessary height, I don't even understand what causes it to be smaller in size, I've combed through the styles tab and don't see anything that can cause it :(

EDIT: it's not a CSS issue, the problem is that the virtual repeater runs before that sizing script and renders a smaller number of items. Maybe there's a way to solve it with a directive and priorities?

EDIT: this is my solution, it calls an internal setSize_ method of the virtual container:

return {
    restrict: 'A',
    require: '^mdVirtualRepeatContainer',
    link: function(scope, element, attributes, mdVirtualRepeatContainer) 
    {
        angular.element(element).css('height', element[0].parentNode.offsetHeight + 'px');

        scope.$watch(function ()
        {
            return element[0].parentNode.offsetHeight;
        }, function(value){
            angular.element(element).css('height', value + 'px');
            mdVirtualRepeatContainer.setSize_(value);
        });
    }
};

Madd0g avatar Oct 10 '15 21:10 Madd0g

@Madd0g if I open and close the Javascript console or developer tools, it will thereafter render an incorrect height. As long as I don't do that, my implementation seems to render correctly.

Do you have a CodePen to demonstrate your issue?

colinskow avatar Oct 11 '15 10:10 colinskow

Here's my CSS that is working perfectly. Note that the repeat items all must be the EXACT specified height without any padding or overflow, otherwise scrolling will be really glitchy.

#vertical-container {
    min-height: 300px;
    width: 100%;
}

.repeated-item {
    border-bottom: 1px dotted #757575;
    box-sizing: border-box;
    height: 50px;
    overflow: hidden;
    margin-right: 16px;
}

The min-height on the container seems to be important. This is later overridden by the actual height from your script. Also, overflow: hidden on the repeat item keeps any items from accidentally getting taller than 50px.

colinskow avatar Oct 11 '15 10:10 colinskow

@colinskow - it's definitely something about my HTML that's causing it (and weirdly it only breaks on ui-router state change, not a page refresh), but the directive I posted solves my problem perfectly, so I'm not worried, it will be enough for me until the virtual repeater is officially flexable.

Thanks for your help

Madd0g avatar Oct 11 '15 12:10 Madd0g

+1

guiporto avatar Oct 28 '15 22:10 guiporto

+1

PezzerDev avatar Oct 28 '15 23:10 PezzerDev

+1

csvan avatar Nov 18 '15 18:11 csvan

+1

jamalx31 avatar Nov 19 '15 14:11 jamalx31

As of 1.0-rc4, you can $broadcast $md-resize to induce the repeater to recompute its height.

kseamon avatar Nov 20 '15 16:11 kseamon

@kseamon could you explain how to do that ?

jamalx31 avatar Nov 22 '15 10:11 jamalx31

On a scope that contains the repeater, call $scope.$broadcast('$md-resize') and the repeater container will attempt to remeasure its height/width. If it started out with the incorrect size, this will hopefully fix it. You might need to put your call into a $timeout. Sorry that this is so awkward, but I haven't yet come up with a great solution to this.

kseamon avatar Nov 23 '15 16:11 kseamon