Width issue fixed
http://stackoverflow.com/questions/15523959/ng-grid-does-not-take-100-width-on-page-load-but-shows-fine-on-resize
It works for me. (ng-grid 2.0.5)
From this:
var ngEventProvider = function (grid, $scope, domUtilityService, $timeout) {
var self = this;
// The init method gets called during the ng-grid directive execution.
self.colToMove = undefined;
self.groupToMove = undefined;
self.assignEvents = function() {
// Here we set the onmousedown event handler to the header container.
To this:
var ngEventProvider = function (grid, $scope, domUtilityService, $timeout) {
setInterval(function() {
domUtilityService.RebuildGrid($scope,grid);
}, 30);
var self = this;
// The init method gets called during the ng-grid directive execution.
self.colToMove = undefined;
self.groupToMove = undefined;
self.assignEvents = function() {
// Here we set the onmousedown event handler to the header container.
If there is any reason for not using this patch, please let me know.
This is not what I want. Actually when browser shrink to some low width (say mobile screen), grid structure should be changed as per priority defined in column. In mobile screen, column with low priority should be not visible. For reference Gmail mobile app. It shows Subject in the top line, and label, from and time in second line.
I see.
Anyway, that solution overloads too much the CPU. A similar patch, but much more efficient would be:
From this:
// we have to set this to false in case we want to autogenerate columns with no initial data.
grid.lateBoundColumns = false;
$scope.columns = [];
grid.config.columnDefs = a;
grid.buildColumns();
grid.configureColumnWidths();
grid.eventProvider.assignEvents();
domUtilityService.RebuildGrid($scope, grid);
}, true);
To this:
// we have to set this to false in case we want to autogenerate columns with no initial data.
grid.lateBoundColumns = false;
$scope.columns = [];
grid.config.columnDefs = a;
grid.buildColumns();
grid.configureColumnWidths();
grid.eventProvider.assignEvents();
setTimeout(function() {domUtilityService.RebuildGrid($scope,grid);}, 1);
}, true);
Fixing the underlying issue would be better than using setTimeout. That's the JS equivalent of !important. Just throwing up your hands at trying to solve the actual problem and (in this case) wasting processor time to rebuild a grid that was just built.
@roblarsen What would you suggest for throttling resize events? Only thing I can find is to use timeouts like so:
http://stackoverflow.com/questions/5489946/jquery-how-to-wait-for-the-end-or-resize-event-and-only-then-perform-an-ac
Can you give a plunker showing this issue or something I can test locally? Thanks.
@jonricaurte I wasn't really looking at throttling resize events as much as saying that it would be better to look for a solution that didn't involve immediately rebuilding the grid with an instant setTimeout()
@roblarsen Ah, I see. Can you try the latest build of 2.0.7 to see if it fixes it? I actually removed both the grid.configureColumnWidths() and domUtilityService.RebuildGrid($scope, grid) since now the watch for columns is calling the rebuild grid method. We might have to use a setTimeout, but it wont actually be rebuilding the grid twice as it was. It will only build it once.
The simplest way to make the table responsive is to calculate the percentage you need per column (only one necessary columns). Then when declaring the width, for example if I were to use 10%
instead of declaring
width: 250, "Which ends up being 250px"
try using width: '20%',
Worked for me.
percentage works nicely, there is still the issue though that small screens don't need to see all the data so it would be great for the future if some columns could be hidden for smaller screens
It looks like the original poster is looking for "responsive design" smarts built into ng grid. The concept is "write once, display anywhere." The problem is that, in viewports with less horizontal space (anything that is more portrait than landscape such as a phone), the columns either shrink so small that they become unusable, or the table is fixed in width so that the table becomes the only part of the page that doesn't fit within the visible area of the screen (breaking responsiveness). The responsive (modern, mobile-friendly) approach that will work is one that stacks the columns as the width of the viewport shrinks (based on css media queries... twitter bootstrap hides a lot of this work from the developer). Here is a simple example that is more along the lines of what's needed http://css-tricks.com/examples/ResponsiveTables/responsive.php ... a better name for this is probably "stackable grid", or "responsive grid" ... and it shouldn't have any CPU strain. It's just "if phone then render columns stacked" ...
The setTimeout solution isn't working for me and the setInterval solution is consuming a lot of resources. Does anyone have a better solution?