how to highlight a row using ng-mouseenter/leave in a rowTemplate?
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.
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.
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!
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;
}
.ui-grid-row:nth-child(odd):hover .ui-grid-cell{background:red} .ui-grid-row:nth-child(even):hover .ui-grid-cell{background:red}
Wow! @tonny008 you nail it !!!
Thank you!
@tonny008 answer is working.
I will go with this option instead of replacing a lot of rowTemplate code.
Finally I found it. @tonny008 Thank you very much!
i have done it, this: .ngCanvas > div:hover { background: #ddd; }
this work me. well
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}
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 I'm experiencing the same issue, have you found a workaround for this?
Same problem as @alpeshmail and @lbntly, did somebody find a solution?
@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.
Thanks @lbntly Works fine. Plunkr for reference: http://plnkr.co/edit/bMLVkcXPCC2TMieUg6LB?p=preview
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
@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