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

Need to make ExpandableRowHeight dynamic

Open flea19 opened this issue 11 years ago • 22 comments

Need way to dynamically set height on the ExapndableRow. This is needed for a Feature in the Open Source tool OSEE.

flea19 avatar Jan 14 '15 19:01 flea19

This has been discussed a number of times, and there are some practical limitations on the ability to do that. There is nobody currently working on it, however we are more than happy to accept a pull request adding this feature: https://github.com/angular-ui/ng-grid/blob/master/FIRST_TIMER.md and https://github.com/angular-ui/ng-grid/blob/master/CONTRIBUTING.md

PaulL1 avatar Jan 14 '15 19:01 PaulL1

I have been trying to work around this for a while. I am going to go ahead and start working on this enhancement.

kevin-sage avatar Jan 16 '15 22:01 kevin-sage

I think we probably should be clear exactly what we're doing. I've seen a few people asking for this in different ways, and I think @jpuri is of the opinion that it's not possible. So I'd get a few eyes on it before you start coding.

I think there are a few things to consider in here:

  1. The grid doesn't really deal with dynamic row heights. The scroll height calculates based on an assumption about the row height. However, the expandable does some extra tricks, because the height of expandable rows isn't automatic. I suspect that it works based on visibleRows * rowHeight + expandedRows * expandedRowHeight
  2. If all the expanded rows must have the same height, then it gives some grief when the expanded row has only one item in the subgrid, or has 100 items. Reasonably they might each be different.
  3. I think the request is that we be able to somehow show this, particularly if the subgrid has more rows than fits in the expandable height. Since you need to know the expandable height up front, and you may not know how many rows there are in an expanded row until you click on it, you can get into a situation where you have more rows in your subgrid than can be displayed
  4. I think the most logical answer is to permit scrolling in the subgrid. If we permit instead dynamic subgrid heights, then calculation of scroll height will require us to sum the height of each subgrid, including subgrids that aren't currently displayed
  5. I've sort of thought of another way, in which we store against each row the "height up to here". We'd recalculate all the "height up to here"s whenever someone changes a height - changes the height of a row, expands a subgrid, sorts or filters the grid etc. But we wouldn't have to calculate it on every scroll. I'm not sure how much work this would turn into, but @swalters has been refactoring this scrolling code recently so it may be easier than it used to be.

PaulL1 avatar Jan 16 '15 22:01 PaulL1

I am flexible on the approach, and my first approach was to see if I could it based on the number of rows that the expandable grid would take (so rather than saying the expanded grid is 140px, you could say it is 3 rows). That way the expanded grid would be based on the same calculations as the rest of the grid (i.e. if it is expanded, add that many rows to the scroll height. Let me take a look into it, and see where I can get to, then before I commit too much time to it, we can circle back on it.

kevin-sage avatar Jan 16 '15 22:01 kevin-sage

Maybe I am missing something here, but it looks like, with the exception of cellnav (which currently does not handle expandable grids anyway), everything should handle having variable sized subgrids. The issue is that the grid sizes and draws the row prior to having a tie-in for loading data.

My suggestion is to move the rowExpandedStateChanged to occur prior to setting the row height, then instead of just using grid.options.expandableRowHeight, we have the ability to override that value based on a row level variable (that could get set during the rowExpandedStateChanged event).

If we don't want to change where this event occurs, my second suggestion would be to add a new event that occurs prior to setting the row height, and allows the same functionality described above.

Let me know what everyone's thoughts are, and for now I am going to go forward with my first idea to see if I can prove it out.

kevin-sage avatar Jan 18 '15 17:01 kevin-sage

Hi @kevin-sage , I also have some time, I can also contribute in the task you are doing.

jpuri avatar Jan 19 '15 11:01 jpuri

Thanks @jpuri , I think I have the change mostly done, however, I am running into an issue with the height of the expanded row being stuck at 150. Tonight I should get more time to work on this and if I cannot resolve that, I might need your help on it.

kevin-sage avatar Jan 19 '15 16:01 kevin-sage

Thanks for looking into this so promptly. I was hoping to get the ability to make the row I've just clicked for expansion to be dynamic and to also be the only one that's expanded. I figure that functionality might be more helpful for the user since they'll only see one expanded/focused row, and easier to implement for you guys. The scrolling algorithm wouldn't have to worry about multiple rows being a dynamic height but rather just one.

I created the bug stating that I was able to modify the height of the expanding row but not the height of the table. If that issue was resolved it seems that I could implement the version of Dynamic Expandable Rows quite easily.

flea19 avatar Jan 19 '15 21:01 flea19

I tried to just change the timing of the event, however, since it is async, the code to set the height runs prior to the event firing. Next option, put in a method, like onRegisterApi to load the data first, then finish expanding the row. Again, this seems to work unless you are loading the data with ajax and getting into another async situation. At this point, I am thinking maybe the best option would be expose a property that would allow the height to be changed directly at anytime (not sure how difficult that will be and I am concerned it might cause some unexpected behaviors with scrolling and other grid functionality).

kevin-sage avatar Jan 20 '15 01:01 kevin-sage

If you're having problems with timing, one option is to shift to a promise architecture.

I did this to some extent in the rowEdit feature. So if you're dependent on the data load completing (and I assume that expandable explicitly calls a "get data" method to get the data anyway), then you can just require the user to return a promise from the get data method (either directly as a result of calling them, or just require them to call a setPromise callback, which is what I did in rowEdit).

You then wait on that promise, then execute the sizing code.

PaulL1 avatar Jan 20 '15 02:01 PaulL1

Great idea @PaulL1, I will take a look at what you did for rowEdit and will implement something very similar.

kevin-sage avatar Jan 20 '15 15:01 kevin-sage

@kevin-sage hey looking for same thing, was wondering how progress is going?

702webdev avatar Jan 23 '15 22:01 702webdev

Hi @kevin-sage, any update on this? This would be a great feature to have.

priceld avatar Feb 19 '15 12:02 priceld

I am using this from stackoverflow in the interim.

    onRegisterApi: function(gridApi) {
        gridApi.expandable.on.rowExpandedStateChanged($scope, function(row) {
            $scope.gridOptions.expandableRowHeight = 30 + row.entity.subGridOptions.data.length * 30;
        });
    }

ruhley avatar Feb 26 '15 01:02 ruhley

Hi, I've managed to solve this (in version 3.0). My grid now has dynamic row heights for, for each row according to it's own data. I've changed the templates a bit, as well as the expandable feature. I've forked the project and implemented my changes. If you would like to have a look and decide if you could use this - https://github.com/yccteam/ng-grid. Here's a short explanation of what I did:

  1. In the expandable temlates, change the grid.options.expandableRowHeight to row.expandableHeight. Now for each row, you can set an expandedableHeight, which will be used in the following steps to make the changes.
  2. I've added a new event: rowExpandedBeforeStateChange to the feature. I hook up to this event every time a row is expanded and I do the dynamic change.
  3. Then, in the function: toggleRowExpansion I did as follows:
  • Triggered the event rowExpandedBeforeStateChange at the head of the function.
  • Asked if there is a row.expandableHeight (e.g. angular.isUndefined(row.expandableHeight)) - if it does not exist, it takes the default grid.options.expandableRowHeight.
  • If an expansion was evoked (e.g. row.IsExpanded is true), I've replaced grid.options.expandableRowHeight with row.expandableHeight.

This worked wonders for me.

NOTE: I did not test it fully, in regards to design etc. I did a lot more changes, but saw this feature is very much sought after, so I publish it here. My CSS files and scenerios might vastly differ than yours...

YonatanKra avatar Apr 01 '15 14:04 YonatanKra

Unfortunately, the various solutions did not work out, and we were having issues with other features so we abandoned ui grid.

Sent from Outlook

From: Andrey Bottoni

Sent: Tuesday, March 8, 8:29 AM

Subject: Re: [ui-grid] Need to make ExpandableRowHeight dynamic (#2549)

To: angular-ui/ui-grid

Cc: kevin-sage

@YonatanKra Hey man. I was trying to access your code on https://github.com/yccteam/ng-grid but no luck (Page not found). Do you have the code you have used to solve this problem somewhere (or an example of it). I have been stuck for a while trying to make this to work but no luck so far. Thanks

—

Reply to this email directly or view it on GitHub.

kevin-sage avatar Mar 08 '16 15:03 kevin-sage

I was able to make the expandable row height dynamic and it is expnading as per the rows that are in the child grid. I am using the external pagination option for the expandable and parent grid as well. May be I could be wrong on this topic or the bubg reported. but using the expandedRowHeight I was able to set the dynamic height for the expandable grid.

I set the expandedRowHeight like this and it works for my requirement . This is set once the event is called to set that data as per the pagination

`gridApi.pagination.on.paginationChanged($scope, function (newPage, pageSize) {
                            subgridpaginationOptions.pageNumber = newPage;
                            subgridpaginationOptions.pageSize = pageSize;
                            prepareData()
                        });`
  if (angular.isDefined($scope.childRow.entity.subGridOptions.data)) {
                rows = $scope.childRow.entity.subGridOptions.data.length;
            }

            if (rows != undefined) {
                $scope.childRow.expandedRowHeight = 90 + ((rows) * 30);
            }

rahulmranjith avatar May 11 '16 05:05 rahulmranjith

Hello everybody. I just finished worked solution for this issue. Nothing dificult.

  1. Need to change expandableRowTemplate html file like: <div ui-grid="row.entity.subGridOptions" ng-style="{height: ((row.entity.Details.length+1) * 30 + 'px')}"></div>
  2. add handler to your grid controller like: ` onRegisterApi: function (gridApi) { $scope.gridApi = gridApi;
              gridApi.expandable.on.rowExpandedStateChanged($scope, function (row) {
                  var rowLength = row.entity.Details.length + 1;
                  $timeout(function () {
                      row.expandedRowHeight = rowLength * 30;
                  }, 150);

if You want - you can also resize main grid dynamically

                  if (row.isExpanded) {
                      $scope.rowLength+=rowLength;
                  } else {
                      $scope.rowLength-=rowLength;
                  }
                  reSize($scope.rowLength);
              });
          }

$scope.rowLength = 0;

Do not forget load your data... and set $scope.rowLength to your data length

    function reSize(rows, gridClassName) {
        var newHeight =(rows*30)+90;
        angular.element(document.getElementsByClassName((gridClassName || 'grid'))[0]).css('height', newHeight + 'px');
    }

` Thats it. Woks perfect for Me.

Olegarius avatar Jul 27 '16 11:07 Olegarius

@YonatanKra could you please share your code? The page is not accessible.

BPoorey avatar Apr 26 '17 10:04 BPoorey

listen on rowExpandedBeforeStateChanged event successfully fix the problem

monhz avatar May 04 '17 07:05 monhz