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

how to highlight a row using ng-mouseenter/leave in a rowTemplate?

Open jmcmichael opened this issue 12 years ago • 16 comments

Thanks for this fabulous datagrid, I'm enjoying working with it and so far it looks like it has all the features we'll need (and more).

I'm working on a simple grid with listing items that will change the application's state when a row is clicked. I've figured out how to call a function when a row is clicked using ng-click and getExternalScopes to call a function that changes the state, but I'm having trouble showing a highlight effect on the entire row using ng-mouseenter/leave and ng-class. It works to some extent, but only highlights the cell, not the row.

My rowTemplate currently looks like:

$scope.gridOptions = {
rowTemplate: "<div ng-repeat=\"(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name\" \
ng-mouseleave=\"hover=false\" \
ng-mouseenter=\"hover=true\" \
ng-click=\"getExternalScopes().rowClick(row)\" \
class=\"ui-grid-cell\" \
ng-class=\"{ 'ui-grid-row-header-cell': col.isRowHeader, 'rowHover': hover }\" \
ui-grid-cell></div>"
}

I'm currently unsure of how to toggle a class on the actual row and not a cell.

jmcmichael avatar Nov 17 '14 23:11 jmcmichael

If you use the selection feature, and turn off the rowSelectionHeader, then what it will do is to highlight the selected row for you. That may be all you need.

If you need something more custom, then take a look at the code for selection to see how it's doing the highlighting, that may give you some tips.

PaulL1 avatar Nov 17 '14 23:11 PaulL1

Close, I'm looking to highlight the row on a hover event, not a click event. The click will replace the ui-view containing the ui-grid with a detail view of the record clicked by the user.

EDIT: I'll review the ui-grid-selection code to see how it does the highlighting and get back to you. Thanks!

jmcmichael avatar Nov 17 '14 23:11 jmcmichael

OK, after a bit of digging through the ui.grid.selection service this turned out to be straightforward:

rowTemplate:

<div ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.colDef.name"
     ng-mouseenter="getExternalScopes().rowHover(row)"
     ng-mouseleave="getExternalScopes().rowHover(row)"
     ng-click="getExternalScopes().rowClick(row)"
     class="ui-grid-cell"
     ng-class="{ 'ui-grid-row-header-cell': col.isRowHeader }"
     ui-grid-cell>
</div>

And then rowHover is just:

      rowHover: function(row) {
        row.isSelected ? row.isSelected = false : row.isSelected = true;
      }

jmcmichael avatar Nov 18 '14 19:11 jmcmichael

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

tonny008 avatar Jun 05 '15 04:06 tonny008

Wow! @tonny008 you nail it !!!

Thank you!

Goodwin-Git avatar Sep 23 '15 05:09 Goodwin-Git

@tonny008 answer is working.

I will go with this option instead of replacing a lot of rowTemplate code.

jazlopez avatar Oct 02 '15 18:10 jazlopez

Finally I found it. @tonny008 Thank you very much!

Noel-bk avatar Oct 16 '15 08:10 Noel-bk

i have done it, this: .ngCanvas > div:hover { background: #ddd; }

this work me. well

Cdvalencia avatar Jan 11 '16 16:01 Cdvalencia

It works for me using css only .ui-grid-row:nth-child(even):hover .ui-grid-cell{background:#F5F5F5} .ui-grid-row:nth-child(odd):hover .ui-grid-cell{background:#F5F5F5}

nkantkumar avatar Feb 28 '16 05:02 nkantkumar

Css is making a sense for row hover color on mouse enter and leave. hover color is working fine for regular columns, when we do the ping on left column using ui-grid-pinning directives then it didn't work as expected, hover color comes for either non-pinging/ping section but not for both together.

alpeshmail avatar Jun 06 '16 20:06 alpeshmail

@alpeshmail I'm experiencing the same issue, have you found a workaround for this?

leahjlou avatar Jul 08 '16 20:07 leahjlou

Same problem as @alpeshmail and @lbntly, did somebody find a solution?

gjkamstra avatar Jul 18 '16 08:07 gjkamstra

@gjkamstra Yeah, I ended up setting a custom rowTemplate in gridOptions. This is my template:

<div
    ng-repeat="(colRenderIndex, col) in colContainer.renderedColumns track by col.uid"
    ng-mouseover="grid.appScope.hoveredIndex = rowRenderIndex"
    ng-mouseleave="grid.appScope.hoveredIndex = null"
    ui-grid-one-bind-id-grid="rowRenderIndex + '-' + col.uid + '-cell'"
    class="ui-grid-cell"
    ng-class="{'ui-grid-row-header-cell': col.isRowHeader, 'your-hover-class': grid.appScope.hoveredIndex === rowRenderIndex}"
    role="{{col.isRowHeader ? 'rowheader' : 'gridcell'}}"
    ui-grid-cell>
</div>

Then in css make .your-hover-class have whatever styling you want. (In my case it was a highlight color for the background color.)

It's not ideal, but it doesn't seem to affect performance in any notable way.

leahjlou avatar Jul 18 '16 18:07 leahjlou

Thanks @lbntly Works fine. Plunkr for reference: http://plnkr.co/edit/bMLVkcXPCC2TMieUg6LB?p=preview

gjkamstra avatar Jul 20 '16 13:07 gjkamstra

Hi, i am using ag-grid, onkeypress i need to change the background color but the row is already selected. is any event to find that event

viswa588 avatar May 01 '17 03:05 viswa588

@nkantkumar Your solution is working great for the grids without the selection checkbox, but with selection checkbox, it does not.

Reason is that the checkbox column is wrapped in a completely different wrapper

saurajit avatar Feb 08 '18 13:02 saurajit