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

Grid does not render properly when it is hidden (such as in a tab)

Open c0bra opened this issue 13 years ago • 67 comments

I'm trying to use ng-grid in a tab pane that's hidden on initial load.

I've found that the rows do not draw at all and the header/footer draw improperly on data instantiation. The header shows the right number of records and the columns are there in the column menu, but they're off to the left of the grid div.

If I activate the pane and resize the viewable area (either by opening firebug or resizing the browser window), the rows and other content will draw.

I'm not sure if I can demonstrate this in jsFiddle/plunkr, but I could try.

c0bra avatar Dec 12 '12 21:12 c0bra

can you try the latest as of PR #52?

orneryd avatar Dec 13 '12 00:12 orneryd

That works! Thanks very much.

c0bra avatar Dec 13 '12 15:12 c0bra

Ran into this again, except I'm now doing a grid in a bootstrap modal. It happens in both 1.6.0, the current ng-grid.debug.js commit, and that #52 PR.

When opening the modal, it looks like this: one

Upon altering the browser size once (e.g. when opening the firebug panel), it redraws like so: two

And then on the second resize it draws properly. I've tried making an example plunker but I can't get it to break in the same way, so now I'm kind of stuck: http://plnkr.co/edit/DOPDNp

c0bra avatar Jan 03 '13 19:01 c0bra

I was able to get around this in a horribly hacky way by using ui-modal from angular-ui to attach the modal's open state to a boolean value in the scope, and then watch that boolean and manually trigger two browser resize events when the modal gets opened:

$scope.$watch('modalShown', function(n, o){
    if (n == true) {
        window.setTimeout(function(){
            $(window).resize();
            $(window).resize();
        }, 1000);
    }
});

Yes, it takes two resizes, and yes it has to be in a setTimeout in order for the modal/grid to be completely displayed. Not in any way, shape, or form ideal. Hopefully a real fix (either to ng-grid or my code) will arise.

c0bra avatar Jan 03 '13 21:01 c0bra

any fix to this problem? I am also having the same experience here. When using bootstrap tabs, some how I have noticed the generated style by ngGrid is not given correctly which is why it didn't render well.

<style id="ng1357542803118" type="text/css" rel="stylesheet">
.ng1357542803118 .ngCanvas { width: -5px; }
.ng1357542803118 .ngRow { width: -5px; }
.ng1357542803118 .ngCanvas { width: -5px; }
.ng1357542803118 .ngHeaderScroller { width: NaNpx}
.ng1357542803118 .col0 { width: -1px; left: 0px; right: -4px; height: 30px }
.ng1357542803118 .colt0 { width: -1px; }
.ng1357542803118 .col1 { width: -1px; left: -1px; right: -3px; height: 30px }
.ng1357542803118 .colt1 { width: -1px; }
.ng1357542803118 .col2 { width: -3px; left: -2px; right: 0px; height: 30px }
.ng1357542803118 .colt2 { width: -3px; }
</style>

This is how it was regenerated correctly when the browser was slightly resized.

<style id="ng1357542524351" type="text/css" rel="stylesheet">
.ng1357542524351 .ngCanvas { width: 997px; }
.ng1357542524351 .ngRow { width: 997px; }
.ng1357542524351 .ngCanvas { width: 997px; }
.ng1357542524351 .ngHeaderScroller { width: NaNpx}
.ng1357542524351 .col0 { width: 333px; left: 0px; right: 664px; height: 30px }
.ng1357542524351 .colt0 { width: 333px; }
.ng1357542524351 .col1 { width: 333px; left: 333px; right: 331px; height: 30px }
.ng1357542524351 .colt1 { width: 333px; }
.ng1357542524351 .col2 { width: 331px; left: 666px; right: 0px; height: 30px }
.ng1357542524351 .colt2 { width: 331px; }
</style>

raftalks avatar Jan 07 '13 07:01 raftalks

Have you guys tried using the layout plugin?

https://github.com/angular-ui/ng-grid/blob/master/plugins/ng-grid-layout.js

What you need to do is:

http://plnkr.co/edit/7Tc3j2n6uHA73WSQlD4i?p=preview

This will tell the grid to update its layout.

jonricaurte avatar Jan 16 '13 22:01 jonricaurte

Can you try using the latest? I think I may have fixed this issue in latest commit.

jonricaurte avatar Jan 18 '13 21:01 jonricaurte

I tried with the latest version, but still it isn't rendering correctly inside a Tab (although the first tab do work but not in any other hidden tabs that show up on click). If I make the browser slightly stretch its width or height, the table re-renders correctly. Here is a screenshot.

Screenshot at 2013-01-19 11:58:35

raftalks avatar Jan 19 '13 07:01 raftalks

@jonricaurte I tried with latest version, but still it is not rendering completely. See the above attachement.

raftalks avatar Jan 20 '13 06:01 raftalks

For now I would just use the plugin:

https://github.com/angular-ui/ng-grid/blob/master/plugins/ng-grid-layout.js

self.domUtilityService.RebuildGrid(self.scope, self.grid);

That method in the updateGridLayout function is what the $(window).resize() calls, so if you call it when the user switches tabs or the modal window opens, it will do the same thing as if the window.resize event was fired. And here is an example on how to use plugin:

http://plnkr.co/edit/7Tc3j2n6uHA73WSQlD4i?p=preview

jonricaurte avatar Jan 20 '13 23:01 jonricaurte

@raftalks Also could you make a plunker showing the tab issue? The modal one I can't seem to replicate.

jonricaurte avatar Jan 20 '13 23:01 jonricaurte

@jonricaurte I have created the plunkr here http://plnkr.co/edit/7bzBdOZdM1Yc39P2Sbxm?p=preview and while I was making that, I realised it is working without a problem if the .gridStyle defines the width of the table. In my design I wanted to have a flexible grid that will fit to its container, I have set it to auto width and that is the problem.

What is your suggestion to make the grid work with fluid width.

raftalks avatar Jan 21 '13 09:01 raftalks

I think it will be nice to have an event triggered before and after the table is rendered or a callback similar to when selection change, so incase, after the rendering I can call a method to run the layoutPlugin.updateGridLayout(), however I feel this might also end up with a unstoppable loop, as if the same event gets triggered after updateGridLayout method is called.

raftalks avatar Jan 21 '13 09:01 raftalks

I have the same issue. Updating the layout does not happen at the right time when a tab is selected using Bootstrap tabs. The only solution is setting a fixed height and width for the grid.

sephie avatar Jan 22 '13 11:01 sephie

http://plnkr.co/edit/HLmZwCCo2usUNHCcF4vL?p=preview This uses some jQuery to add a click handler to the tab that contains the grid that performs a layout update after showing the tab. All other tabs work as before so it can be used as a workaround for just the tab that hides an ng-grid.

sephie avatar Jan 22 '13 15:01 sephie

@sephie I think the main problem has to do with the grid rendering however your workaround suites as a temporary solution. I wonder if angular-ui/bootstrap tabs have any possibility to run a callback when the tab is clicked. I am not sure, but here is the same plunkr using angular-ui/bootstrap tabs http://plnkr.co/edit/XjvPAhyc22qMBEi7X3cd?p=preview

raftalks avatar Jan 23 '13 07:01 raftalks

@jonricaurte any possibilities to solve this issue, as I think the problem is now narrowed to a point where the width of the table in the style sheet set as auto or 100% while in a hidden container, the rendering fails. http://plnkr.co/edit/XjvPAhyc22qMBEi7X3cd?p=preview

raftalks avatar Jan 26 '13 21:01 raftalks

@raftalks I updated your plunker with the patch I just submitted, so everyone can check it:

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

getuliojr avatar Jan 28 '13 20:01 getuliojr

@getuliojr Thanks, that is working perfectly when the width of the gridStyle class is set to auto, however take note that it is not possible to achieve same result if width is set as 100%.

raftalks avatar Jan 29 '13 06:01 raftalks

@raftalks the same problem. I am submitting a new patch right now that will fix this too.

getuliojr avatar Jan 29 '13 12:01 getuliojr