function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
kmorf_entransformkmorf_entransform 

changing row colors of pageBlockTable

Hi, im trying to make a pageBlockTable where if someone clicks on a certain row on the table, then the color of that row will change. how could i do that?

Best Answer chosen by Admin (Salesforce Developers) 
sfdcfoxsfdcfox

Use the onRowClick JS function to modify the behavior's row click action. Here's a trivial example:

 

<apex:page controller="p1c">
<style>
.selectedDataRow {
    background-color: red;
}
</style>
<script>
function updateRowColor(row) {
    if(row.className.indexOf(' selectedDataRow') < 0) {
        row.className = row.className + ' selectedDataRow'
    } else {
        classes = row.className.split(' ')
        for(var x in classes) {
            if(classes[x] == 'selectedDataRow') {
                classes[x] = classes[0]
                classes.shift()
            }
        }
        row.className = classes.join(' ')
    }
}
</script>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!rows}" var="aRow" onRowClick="updateRowColor(this)">
            <apex:column value="{!aRow.Name}" headerValue="Name"/>
            <apex:column value="{!aRow.Email}" headerValue="Email"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

The controller in this case doesn't particularly matter (it's just a wrapper class and a constructor). The magic here is the onRowClick function; here I make it a toggle switch. The function itself receives the TR (HTML table row element) as the single input, which can be directly modified to adjust that row.

All Answers

sfdcfoxsfdcfox

Use the onRowClick JS function to modify the behavior's row click action. Here's a trivial example:

 

<apex:page controller="p1c">
<style>
.selectedDataRow {
    background-color: red;
}
</style>
<script>
function updateRowColor(row) {
    if(row.className.indexOf(' selectedDataRow') < 0) {
        row.className = row.className + ' selectedDataRow'
    } else {
        classes = row.className.split(' ')
        for(var x in classes) {
            if(classes[x] == 'selectedDataRow') {
                classes[x] = classes[0]
                classes.shift()
            }
        }
        row.className = classes.join(' ')
    }
}
</script>
    <apex:pageBlock >
        <apex:pageBlockTable value="{!rows}" var="aRow" onRowClick="updateRowColor(this)">
            <apex:column value="{!aRow.Name}" headerValue="Name"/>
            <apex:column value="{!aRow.Email}" headerValue="Email"/>
        </apex:pageBlockTable>
    </apex:pageBlock>
</apex:page>

The controller in this case doesn't particularly matter (it's just a wrapper class and a constructor). The magic here is the onRowClick function; here I make it a toggle switch. The function itself receives the TR (HTML table row element) as the single input, which can be directly modified to adjust that row.

This was selected as the best answer
kmorf_entransformkmorf_entransform

ok, works fine, thanks