md-virtual-repeat does not work without predefined height
codepen: http://codepen.io/uriva/pen/doxdVP
I think md-virtual-repeat-container should have fixed height
Yup. That was the issue. Perhaps a doc clarification on that point?
I referred to official doc's sample codes
+1 Would be nice to figure out a way to allow flexable heights.
It's on my to-do list. It's tricky because any solution comes with performance trade-offs.
+1 This tripped me up for a while, a note on the docs would be cool.
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 - 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 - 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 - 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 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?
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 - 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
+1
+1
+1
+1
As of 1.0-rc4, you can $broadcast $md-resize to induce the repeater to recompute its height.
@kseamon could you explain how to do that ?
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.