change background color of a row in rich:dataTable

一个人想着一个人 提交于 2019-12-02 11:53:06

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;
}
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!