5

I am using a BlockTable and colorize its colums depending on object values.

<apex:variable value="" var="column_1_color"/>

<apex:pageBlockTable value="{!objectList}" var="object">

    <apex:column>
        <apex:variable var="column_1_color"
            value="{!IF(object.Name="test","green","red")}"/>
        Name:
    </apex:column>

    <apex:column style="background-color:{!column_1_color}">
        {!object.Name}
    </apex:column>

</apex:pageBlockTable>

|Name: | xxx |<--red
|Name: | test |<--green

The var needs to render for each row,
but what if I need the var value before creating first |Name: | column?
Is there any tag that keeps me from using an empty column?

Like this: (does not work)

<apex:variable value="" var="column_1_color"/>

<apex:pageBlockTable value="{!objectList}" var="object">

    <apex:variable var="column_1_color"
        value="{!IF(object.Name="test","green","red")}"/>

    <apex:column style="background-color:{!column_1_color}">
        Name:
    </apex:column>

    <apex:column style="background-color:{!column_1_color}">
        {!object.Name}
    </apex:column>

</apex:pageBlockTable>
  • What do you mean "what if I need the var value before creating first |Name: | column?" I see that the Name: is a repeat of the objectList. – Rao Jun 5 '13 at 16:52
5

Your <apex:variable> doesn't need a <apex:column> for itself. It can be immediately after <apex:repeat> and it should work just fine.

In fact you can ditch it completely and directly use your condition (makes sense if it'd be used only once. If you need the conditional formatting in more than 1 place - leave it in variable).

<apex:column style="background-color:{!IF(object.Name="test","green","red")}">
    {!object.Name}
</apex:column>

Not sure if that answers your question...

  • yes <apex:variable> is not rendered or not working in <apex:pageblocktable> – D-Horse Jun 5 '13 at 18:09
  • That is how it should work, but it doesn't. not even an <apex:variable var="column_1_color" value="red"/> immediately after blockTable works.. it is not rendering this var outside a column – Basti Jun 6 '13 at 6:51
  • @Basti It should work, i've tested it too and got colirized columns. – Sergej Utko Jun 6 '13 at 7:10
  • 1
    That's because pageBlockTable "thinks" in columns. repeat would "think" in rows if you're willing to craft the html yourself. Try the second way or you can also rearrange styling a bit: <apex:column><div style="background-color:{!IF(object.Name="test","green","red")}">{!object.Name}</div></apex:column> – eyescream Jun 6 '13 at 7:11
  • good to know!! But I would prefer an empty column, than styling a whole new table. and using divs in TDs just leads to more problems. – Basti Jun 6 '13 at 8:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.