ui-grid icon indicating copy to clipboard operation
ui-grid copied to clipboard

UI-Grid header height miscalculation leads to header growth

Open keeper3000 opened this issue 12 years ago • 34 comments

The old ng-grid issue has returned in ui-grid.

When in a bootstrap tab ui-grid version rc12 will not render the grid. Instead renders just a vertical scroll bar with no contents. When pulled outside of a tab the grid renders normally.

Using an old trick does not work.: $scope.fixGridInTab = function(){ window.setTimeout(function(){ $(window).resize(); $(window).resize(); }, 1000); };

Additional details:

  • Bootstrap 3.2
  • ui bootstrap 11.2
  • angular 1.3-rc4

keeper3000 avatar Oct 09 '14 12:10 keeper3000

This trick also no longer works::

$scope.reviewGridOptions.$gridServices.DomUtilityService.RebuildGrid($scope.reviewGridOptions.$gridScope, $scope.reviewGridOptions.ngGrid);

keeper3000 avatar Oct 09 '14 12:10 keeper3000

Can you create a plunker that demonstrates this? On Oct 9, 2014 7:09 AM, "keeper3000" [email protected] wrote:

The old ng-grid issue has returned in ui-grid.

When in a bootstrap tab ui-grid version rc12 will not render the grid. Instead renders just a vertical scroll bar with no contents. When pulled outside of a tab the grid renders normally.

Using an old trick does not work.: $scope.fixGridInTab = function(){ window.setTimeout(function(){ $(window).resize(); $(window).resize(); }, 1000); };

Additional details:

  • Bootstrap 3.2
  • ui bootstrap 11.2
  • angular 1.3-rc4

— Reply to this email directly or view it on GitHub https://github.com/angular-ui/ng-grid/issues/1776.

c0bra avatar Oct 09 '14 12:10 c0bra

http://plnkr.co/edit/Sfh44ZZeRshmVrz5hjr7?p=preview

I'm very appreciative of your efforts! Great Job on NG/UI-Grid

keeper3000 avatar Oct 09 '14 13:10 keeper3000

I just noticed if you resize the window it renders the grid.

keeper3000 avatar Oct 09 '14 13:10 keeper3000

I noticed this yesterday. A quick and dirty work around and not really intended as a solution, is watching for when the grid becomes visible and calling grid.handleWindowResize() on the gridApi. With tabs, you can watch when a tab becomes active and do it that way... Also a $timeout to ensure the rendering is performed after the tab has fully displayed. Here's how I watch:

$scope.test = {isActive: false};
$scope.alertGridOptions.onRegisterApi = function (gridApi) {
    $scope.gridApi = gridApi;
};
$scope.$watch('test.isActive', function (active, oldActive) {
    if (active && active !== oldActive && $scope.gridApi) {
        $timeout(function () {
            $scope.gridApi.grid.handleWindowResize();
        });
    }
});
<tabset>
    <tab heading="Empty"></tab>
    <tab heading="Grid" active="test.isActive">
...

Modified your plunkr. http://plnkr.co/edit/Qxr4r6oLGnRPcyg5hpKP?p=preview

Granted this also caused some other wonkiness with the rendering. Like setting the rowHeight on the grid, the row header doesn't obey that value using this trick and is larger/smaller than the rows.

If I had to do a full blown solution, I wouldn't rely on handleWindowResize's for width adjustments and prefer a more fluid css solution to the grid layout. It seems like a lot of the pain could be removed if it was just absolute positioned and used top/bottom/right/left to stretch/anchor things.

mtraynham avatar Oct 09 '14 17:10 mtraynham

You guys rock! Thank you!

keeper3000 avatar Oct 09 '14 20:10 keeper3000

If you give the grid width: 100% with your CSS class it will render. Without that, because the element is hidden initially and unrendered the grid thinks it has 0 width.

c0bra avatar Oct 13 '14 14:10 c0bra

@c0bra Interesting and thanks. That does seem to work on the plunkr @keeper3000 provided . Locally, I'm getting a situation where the grid is extending past it's container slightly. I'll see if I can simulate in a different plunkr. My grid is inside a templated directive, so I wonder if that may have something to do with it...

mtraynham avatar Oct 13 '14 15:10 mtraynham

Found the reason. The parent div has padding. The alignment is correct on the left, but not on the right. Classes from bootstrap, like the grid layouts col-lg-*, typically add padding with percentage widths.

    <tabset class="nav nav-tabs">
    <tab>
        <tab-heading>Tab 1</tab-heading>
        Grid should Render Below
        <div style="width: 100%; padding-left: 15px; padding-right: 15px">
          <div class="gridStyle" style="width:100%" ui-grid="alertGridOptions"></div>
          Move Div above outside of tabs and watch it render.
          Grid should Render Above.  
        </div>
    </tab>
    <tab>
        <tab-heading>Tab 2</tab-heading>
        Test Tab Content
    </tab>
    </tabset>

http://plnkr.co/edit/qrQgVZExhDhpQ0HWBS2C?p=preview

mtraynham avatar Oct 13 '14 15:10 mtraynham

Also calling handleWindowResize causes the window to bug: check this plunk

Scroll your mouse wheel or click the button and watch it grow indefinitely.

http://plnkr.co/edit/APfZasw1hcKe830jxLpM?p=preview

keeper3000 avatar Oct 13 '14 17:10 keeper3000

@mtraynham: I have noticed also the grid consumes the space from padding, I haven't gotten around to looking at the css properly but I figured that is something that could be fixed locally.

@keeper3000: that ongoing grid header growth looks like some sort of off-by-one calculation error, but interesting that it then seems to use the current as the base and add one again on the next cycle.

PaulL1 avatar Oct 13 '14 20:10 PaulL1

@PaulL1 I did some debugging, but here are my findings. If the grid or parent div (inherited) is display: none (all tabs are display: none at initialization), ui.grid does:

  • Initalize grid width - https://github.com/angular-ui/ng-grid/blob/master/src/js/core/directives/ui-grid.js#L197
  • e.offsetWidth == 0 && display: none, so fake display the element - https://github.com/angular-ui/ng-grid/blob/master/src/js/core/services/ui-grid-util.js#L694
  • Fake display the element and execute the callback before removing the fake display - https://github.com/angular-ui/ng-grid/blob/master/src/js/core/services/ui-grid-util.js#L459

That fake display function is the problem. It appends the element to the document body, calculates the width, then removes it. So if your grid (or parent) is display: none, and nested anywhere on the page. It will initially get the width of the entire document (for 100% width).

So if you had a grid in a tab, and that grid took up only the right half of the page. Then on initial render, half of the grid would be off the page to the right.

mtraynham avatar Oct 13 '14 21:10 mtraynham

In my application I do an ng-if on the tabset something like:

<tabset>
  <tab ng-repeat="tab in tablist" id="{{$parent.$parent.$parent.panelName}}.{{tab.view}}" heading="{{(tab.title | translate) + ' (' + ( linkage.tabCounts[tab.entity] || 0 ) + ')'}}" active="tab.active" disabled="tab.disabled">
    <div ng-if="tab.active" class="row" ui-view="{{tab.view}}"></div> 
  </tab>
</tabset>

I did this originally with ng-grid as my tabset has about 10 tabs, and the render time was killing performance. I didn't see the point in rendering a tab until someone clicked on it. That is perhaps why I've never seen this issue - is it something that might work for you?

PaulL1 avatar Oct 13 '14 21:10 PaulL1

It works if you have tab headers, but if you programmatically set tab.active = true the ng-if will fire before the tab switches, still giving an incorrect width.

Probably a better solution that what I originally had. I can just add a watch to that specific tab.active and $timeout set a boolean variable that mimicks the tab.active for the next browser loop after the tab has rendered.

mtraynham avatar Oct 13 '14 21:10 mtraynham

btw...this worked as a temp work around for me until you guys have a perm solution.

.ui-grid-header-cell{ height:27px; max-height:27px; }

keeper3000 avatar Oct 15 '14 20:10 keeper3000

Checked and the header growth is still occurring on current code base.

PaulL1 avatar Jan 14 '15 22:01 PaulL1

yep still happens,

temporary fix still valid .ui-grid-header-cell{ height:27px; max-height:27px; }

dzannotti avatar Jan 18 '15 19:01 dzannotti

dzannotti that trick doesn't work for me. I tried adding the css to ui-grid.css and my custom styles.css. I also added the watch that runs handleWindowResize() function to no avail. Please help.

Siafu avatar Jan 21 '15 17:01 Siafu

@Siafu try to force a $('body').trigger('resize'); after the grid content is loaded, that should fix it. The previous post was regarding the zip on website, for master build credit to @giovannicalo for fixing it. Also you need to trigger that upon tab opening.

dzannotti avatar Jan 21 '15 18:01 dzannotti

The key issue in here was that from @keeper3000 of the grid header growing whenever resize was called. I've updated his plunker to use the latest angular, ui-grid etc, and that seems to have the issue go away: http://plnkr.co/edit/uM50PfGaxQIAR8dEwJTI?p=preview

On that basis, I'm closing the issue.

PaulL1 avatar Jan 23 '15 08:01 PaulL1