i have a row in rich:datatable, which has a link in one of its column. Onclick of this click i need to change the background color of the selected row. how can i achieve this?
You can do this with the following code:
<a4j:form id="myfrm">
<rich:dataTable id="myTbl" value="#{myBean.tblData}" var="tblData">
<rich:column>
<f:facet name="header">Col1</f:facet>
<h:outputText value="#{tblData}" />
</rich:column>
<rich:column>
<f:facet name="header">Col2</f:facet>
<h:outputText value="#{tblData}" />
</rich:column>
<a4j:support event="onRowClick" oncomplete="highlightSingleRow(this)"/>
</rich:dataTable>
</a4j:form>
Javascript:
jQuery.noConflict();
function highlightSingleRow(col) {
jQuery(col).parent().parent().find('tr').removeClass('highlight-row');
jQuery(col).parent().addClass('highlight-row');
}
CSS:
.highlight-row {
background-color: cyan;
}
The above example would highlight the row when it is clicked.
To do it on a link you could do something like:
<rich:dataTable id="myTbl" value="#{myBean.tblData}" var="tblData">
<rich:column>
<f:facet name="header">Col1</f:facet>
<h:outputLink onclick="highlightSingleRow(this)" value="#">
<h:outputText value="link" />
</h:outputLink>
</rich:column>
<rich:column>
<f:facet name="header">Col2</f:facet>
<h:outputText value="#{tblData}" />
</rich:column>
</rich:dataTable>
and then change your javascript to:
jQuery.noConflict();
function highlightSingleRow(lnk) {
jQuery(lnk).parent().parent().parent().find('tr').removeClass('highlight-row');
jQuery(lnk).parent().parent().addClass('highlight-row');
}
Add on your link the onclick method :
<rich:column>
<h:outputLink onclick="changeBackground(this)" value="#">
<h:outputText value="link" />
</h:outputLink>
</rich:column>
The Script (Using jQuery) to find the tr of the cell and apply style :
<script>
function changeBackground(element){
jQuery(element).parents('tr:first').addClass('backgroundRed');
}
</script>
and the css for example
.backgroundRed {
color: #555658;
background-color: red;
}
来源:https://stackoverflow.com/questions/7173262/change-background-color-of-a-row-in-richdatatable