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

Fit row height to content feature

Open magiccrafter opened this issue 11 years ago • 17 comments

It would be good if we are provided with such "dynamic row height" feature. Base on the search I see that many people need this and what is a grid/table without the option to show the complete data in a single row. Based on Issue #157 I realize that it would affect the performance a little bit, but that could be just another reason for having this as a separate feature. That way the decision would be resigned to the ng-grid consumer (developer) to use it or not. Thanks for the good work, the ng-grid is very good.

magiccrafter avatar Feb 11 '15 12:02 magiccrafter

Currently I am using the ng-grid all over the project I work on. I would like to be able to show the data and fill the cell than having some tooltip over partials ;) Tried the tooltips and it is ugly. I've started digging into the code to see if I can create some 'workaround feature' to satisfy my needs. @PaulL1 I would appreciate any guidance if you have any idea how I can achieve that.

magiccrafter avatar Feb 11 '15 15:02 magiccrafter

I have considered this feature a few times. I think it can't be done as a feature, it would need to have some impacts in the core code base.

To make it work, my thought was:

  1. Instead of just fitting to the data, require the user to give a fixed height for each individual row. The grid cannot calculate the scroll bar properly if the row heights are indeterminate
  2. Against each gridRow, keep a running total of "height in pixels up to and including this row". This needs to be recalculated any time the data changes or the displayed rows or row order changes
  3. Modify the virtualisation code to take this information into account when calculating the scroll bar length and current position

I don't think it's impossible, but it's probably not a priority for us when we're already trying to deal with a number of scrolling bugs.

PaulL1 avatar Feb 11 '15 19:02 PaulL1

Thanks for the fast response and the hints. I will try to modify it and if succeed will share it. If not than the victory would be for the ng-grid and I will have to search for an alternative. Thanks and keep the good work.

magiccrafter avatar Feb 12 '15 08:02 magiccrafter

@PaulL1 we're already doing this for the columns so we can essentially just copy that code over and re-use it for the rows, but flip it from X-axis to Y.

c0bra avatar Feb 27 '15 15:02 c0bra

Hi Guys,

I have created a plunker for the dynamic row height which expands the wrapped contents: http://plnkr.co/edit/OwgEfru0QyFaU1XJFezv?p=preview

Do check it out. It is to configure the rowHeight based on the highest content in that cellTemplate. Do feedback if there is better implementation. Thanks.

shenlong avatar Aug 25 '15 07:08 shenlong

@shenlong Tried your example on plunker. When I shrink the size of the company column, the height of the row doesn't increase as more rows of text are needed. I tested this on my Mac running Firefox ESR v31.8 which is equivalent to Firefox v39.

anilnatha avatar Aug 25 '15 15:08 anilnatha

Oh. For that, we need the event to trigger the directive. Any idea is there a column resize event?

shenlong avatar Aug 25 '15 15:08 shenlong

Hi everybody,

To fit row height on content feature, first i changed the CSS consequently (heigth: auto, display: table-row). it works.

To coordinate left, body and right containers, i add a function on "rowsRendered" event.

The function get all rows rendered and in loop get the row offsetHeight in each containers, determines which is the largest and apply to all.

It works but the scroll not. So, I tried to edit gridRow height but it is worst.

I don't understand well viewport concept, so could you help me ?

Here the code: function alignContainers ( gridContainer , grid) {

var rows = angular.element(gridContainer + ' .ui-grid .ui-grid-render-container-body .ui-grid-row');

var pinnedRowsLeft = angular.element(gridContainer + ' .ui-grid .ui-grid-pinned-container-left .ui-grid-row');

var gridHasRightContainer = grid.hasRightContainer();

if (gridHasRightContainer) { var pinnedRowsRight = angular.element(gridContainer + ' .ui-grid .ui-grid-pinned-container-right .ui-grid-row'); }

        // get count columns pinned on left, except columns system (tree,selection)

        var columnsPinnedOnLeft = (grid.renderContainers.left.renderedColumns.length > grid.rowHeaderColumns.length);

        for(var r = 0; r < rows.length; r++) {
            // Remove height CSS property to get new height if container resized 

            var elementBody = angular.element(rows[r]).children('div');
            elementBody.css('height', '');
            var elementLeft = angular.element(pinnedRowsLeft[r]).children('div');
            elementLeft.css('height', '');
            if (gridHasRightContainer) {
                var elementRight = angular.element(pinnedRowsRight[r]).children('div');
                elementRight.css('height', '');
            }

            // GET Height when set in auto for each container
            // BODY CONTAINER
            var rowHeight = rows[r].offsetHeight;
            // LEFT CONTAINER
            var pinnedRowLeftHeight = 0;
            if (columnsPinnedOnLeft) {
                pinnedRowLeftHeight = pinnedRowsLeft[r].offsetHeight;
            }
            // RIGHT CONTAINER
            var pinnedRowRightHeight = 0;
            if (gridHasRightContainer) {
                pinnedRowRightHeight = pinnedRowsRight[r].offsetHeight;
            }
            // LARGEST
            var largest = Math.max(rowHeight, pinnedRowLeftHeight, pinnedRowRightHeight);

            // Apply new row height in each container
            elementBody.css('height', largest);
            elementLeft.css('height', largest);
            if (gridHasRightContainer) {
                elementRight.css('height', largest);
            }

            // Apply new height in gridRow definition
            grid.renderContainers.body.renderedRows[r].height = largest;
        }
    };// END alignContainers()

I note that rowStyle() function is based on grid.options.rowHeight, and the calculated margin-top seems to be false...;

jibap avatar Nov 17 '15 17:11 jibap

For me it works [ui-grid-row] { display: table-row; }

.ui-grid-cell { float: none; display: table-cell; }

and in gridoptions set rowHeight: 'auto'.

alangumer avatar Jan 22 '16 16:01 alangumer

@alangumer : I have no problem for adapt row height to content, my style (almost yours) works :

.ui-grid-row{
    height: auto!important;

    > div {
        display: table-row;
    }

    .ui-grid-cell {
        display: table-cell
        float: none
        vertical-align: middle
        height: auto!important
    }
}

I benefit to tell you that gridoptions set rowHeight: 'auto'. is not working because it generate this in navigator : height: 'autopx'

The remaining issue is how to align rows between containers (left, body, right) ?

My function alignContainers works but the scroll not ! To solve the scrolling issue, for now, i forced pagination and thresholdVirtualization to 50. But i expect to get a real solution from ui-grid team !?

jibap avatar Feb 01 '16 11:02 jibap

@jibap Plunker please? I'm interested in this topic as well. I would certainly be willing to consider the scroll bar as a 'record position' when in 'dynamic height' mode.

izep avatar Feb 04 '16 22:02 izep

Hey, thank you for your help, here the plunker : http://plnkr.co/edit/JwbEmPhJq2LInfUNncdi?p=preview The issue is visible when you scroll at the end of the list (stop scrolling in the middle to get resize many times) , a white space (variable height) is shown

jibap avatar Feb 10 '16 10:02 jibap

#3239 I was able to fix the scroll issue. Could you please try if it works for you?

Thanks, Piyush

piyushparamane avatar Mar 22 '16 14:03 piyushparamane

I edited my plunker, because i made a mistake :

The new largest height value of row is not set on good array (visibleRowCache), i changed for renderedRowsarray.

// Apply new height in gridRow definition (used by scroll) bodyContainer.renderedRows[r].height = largest;

And i edit the container like this :

bodyContainer.canvasHeightShouldUpdate = true

else getVerticalScrollLength will not take new values. (thanks @piyushparamane)

Now, it has more sense but the issue is biggest, the viewport is bad... loot at the plunker !

jibap avatar Mar 24 '16 14:03 jibap

@jibap Thanks for your code. I improved for ajust the final scroll. see: http://plnkr.co/edit/htq0LhJ7k22sr0oXA2Vb?p=preview

I change the code bellow: bodyContainer.renderedRows[r].height = largest;

ghost avatar Jul 19 '16 20:07 ghost

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar May 23 '18 05:05 stale[bot]

This issue has been automatically closed because it has not had recent activity. If you believe that this is still an issue in the latest version, feel free to re-open it. Thank you for your contributions.

stale[bot] avatar Jun 22 '18 06:06 stale[bot]