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
PaulDysonPaulDyson 

How do I highlight a particular value in a list

Sorry if this is a dumb question ... I'm a beginner with VisualForce and searching hasn't turned anything up.

 

I have a list of objects I want to render but I also want to highlight a particular value as the 'current selection' (i.e. give it a different class to the other elements). This is easy with JSTL: use a foreach tag to iterate over the collection and an if...test tag to check whether the value is the one I want to highlight. Obviously I can use dataList or repeat to iterate over the collection but I can't see any equivalent to if...test. 

 

Any ideas? I can do it with Javascript but that seems very hacky. 

PaulDysonPaulDyson

Okay, managed to answer my own question: use the IF formula to render the required class name within the span tag. E.g:

 

<span class="{!IF(obj.ID == currentID, 'selected', 'unselected')}">{!obj.name}</span>

 

Note to guide authors: the guides are excellent but this kind of use of the formulae isn't obvious from the docs. Or is this really bad style and I should be doing it differently? 

John L.John L.

If using SelectOptions and SelectList as below:

 

<apex:form >
<apex:selectList value="{!attr}" multiselect="false" size="1">
<apex:selectOptions value="{!ulvl}"/>
</apex:selectList>
</apex:form>

 

where attr is the current value of the list of options in selectOptions. ulvl is the List<SelectOptions> containing the possible values, displayed as a drop-down pick-list.