angular-ui-grid: how to highlight row on mouseover?
Asked Answered
L

4

11

I want to highlight an entire row of angular-ui-grid when the mouse is on top of it.

I have tried to modify the rowTemplate by adding ng-mouseover and ng-mouseleave callbacks which set the background-color.

Here is my rowTemplate--I expect the first three lines to change the entire row color to red. But only the cell currently under the mouse gets changed.

<div
    ng-mouseover="rowStyle={'background-color': 'red'}; grid.appScope.onRowHover(this);"
    ng-mouseleave="rowStyle={}"
    ng-style="rowStyle"
    ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid"
    ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'"
    class="ui-grid-cell"
    ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }"
    role="{{col.isRowHeader ? 'rowheader' : 'gridcell'}}"
    ui-grid-cell>
</div>

How do I highlight an entire row on mouseover events?

Another approach that I tried: using some callback function do this--like the appScope.onRowHover in the example. Here, I have the scope of the row being hovered on. How do I go about styling that row within the onRowHover function?

Thanks!

Link to Plunkr. I expect mousing over a grid cell to turn the entire row red.

Lowbred answered 26/11, 2015 at 17:32 Comment(0)
R
11

Here, is it. I made a change in the row template. http://plnkr.co/edit/gKqt8JEo2FukS3URRLJ5?p=preview

RowTemplate:

<div ng-mouseover="rowStyle={'background-color': 'red'}; grid.appScope.onRowHover(this);" ng-mouseleave="rowStyle={}">
    <div  ng-style="rowStyle" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'"
    class="ui-grid-cell" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" role="{{col.isRowHeader ? 'rowheader' : 'gridcell'}}" ui-grid-cell>
    </div>
</div>
Ragen answered 27/11, 2015 at 6:20 Comment(2)
I'd have used ng-class instead of ng-style, but the logic would be the same.Grandaunt
Thank you! My understanding is that the outer div encompasses the whole row while the inner div gets expanded to each cell in the row. Is that correct?Lowbred
A
35

(angular-ui-grid#3.0.7) Easiest way is to use CSS hover styles, just add the following to your CSS styles for the page and you are all set:

.ui-grid-row:hover .ui-grid-cell {
  background-color: beige;
}

With ui-grid 3.1.1 the above styles didn't work. I had to do this:

.ui-grid-row:nth-child(odd):hover .ui-grid-cell{background:red}
.ui-grid-row:nth-child(even):hover .ui-grid-cell{background:red}

Solution borrowed from here

Apus answered 13/12, 2015 at 1:52 Comment(4)
The solutions offered by the other answers were buggy, but this one was simple and worked perfectly. So easy. ThanksSplatter
Simplifying things even further, if you don't need the highlight to change because the row is even or odd, then you can just do this .ui-grid-row:hover .ui-grid-cell{background:red}. It works all the sameArundel
For working with ngGrid, you can use .ngRow:nth-child(odd):hover .ngCell{background: red} .ngRow:nth-child(even):hover .ngCell{background: red}Toro
works great. anyone find a solution that works with pinned columns though?Enthusiasm
R
11

Here, is it. I made a change in the row template. http://plnkr.co/edit/gKqt8JEo2FukS3URRLJ5?p=preview

RowTemplate:

<div ng-mouseover="rowStyle={'background-color': 'red'}; grid.appScope.onRowHover(this);" ng-mouseleave="rowStyle={}">
    <div  ng-style="rowStyle" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'"
    class="ui-grid-cell" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" role="{{col.isRowHeader ? 'rowheader' : 'gridcell'}}" ui-grid-cell>
    </div>
</div>
Ragen answered 27/11, 2015 at 6:20 Comment(2)
I'd have used ng-class instead of ng-style, but the logic would be the same.Grandaunt
Thank you! My understanding is that the outer div encompasses the whole row while the inner div gets expanded to each cell in the row. Is that correct?Lowbred
T
1

There is a small problem with the solution of SaiGiridhar: the selection highlighting stops working since another wrapper is added on top of the ng-repeat div.

A small fix for it would be:

<div ng-mouseover="rowStyle={'background-color': (rowRenderIndex%2 == 1) ? '#76BED6' : '#BFDAE3'};" ng-mouseleave="rowStyle={}" ng-style="selectedStyle={'background-color': (row.isSelected) ? '#379DBF' : ''}">
    <div ng-style="(!row.isSelected) ? rowStyle : selectedStyle" ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid" ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'"
        class="ui-grid-cell" ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }" role="{{col.isRowHeader ? 'rowheader' : 'gridcell'}}" ui-grid-cell>
    </div>
</div>

I've just copy-pasted my code. It adds alternate row styling for highlighted rows and also fixes the selected row highlighting.

Hope it helps someone.

Tanana answered 8/2, 2016 at 12:6 Comment(0)
B
0

This is harder if you have selection enabled. Use this.

//js

   $templateCache.put('ui-grid/ui-grid-row',
            `
            <div 
            ng-mouseenter="row.isHov = true" 
            ng-mouseleave="row.isHov = false" 
            ng-init="row.isHov = false"
            ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid"
            ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'"
            class="ui-grid-cell"
            ng-class="{ 'on': row.isHov, 'ui-grid-row-header-cell': col.isRowHeader }"
            role="{{col.isRowHeader ? 'rowheader' : 'gridcell'}}"
            ui-grid-cell>
          </div>
            `
        );

//scss

.ui-grid-cell {


&.on {
    .ui-grid-cell-contents {
        background: red;
    }
}

}

Brabazon answered 25/4, 2018 at 18:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.