2

Is there a way to view certain columns based on a condition. For example, I want columns to be rendered based on the data type of an object.

What I mean is like this:

<apex:column rendered="test.data_type == Select List">
</apex:column>

Also, I want multiple columns to be generated depending on how many specific data types are in that object. If it has two items with string data type, it should render two columns in the output with string input fields. P.S. I have no experience with javascript, if that is part of your answer. Thank you.

To elaborate, what I want to do is that the user would be able to choose an item from a drop down menu and then they can press an add button. This will cause a a table with 2 columns and one or more rows to generated. Each row would have a name field and an input or output. The items are created in a different setup page and the page I am talking about will generate line items based on my item setup. So for different items there could be a different amount of rows and they should also be able to add multiple items so they are stackable.

  • data_type - it is picklist on Object? Or you mean diffrent types of objects? – Artur Kępczyński Feb 18 '14 at 16:30
  • its suppose to be a field for my object. – ths Feb 19 '14 at 17:22
5

Below is some Visualforce that makes use of rendered on apex:column to change the markup for various named columns. So the answer to your question is yes.

<apex:pageBlockTable var="item" value="{!tasks}">
    <apex:column headerValue="Action" styleClass="actionColumn">
        <apex:outputLink value="/{!item.Id}/e" title="Edit Task" styleClass="actionLink">Edit</apex:outputLink>
    </apex:column>
    <apex:repeat var="f" value="{!fields}">
        <apex:column rendered="{! f != 'OwnerId' && f != 'Description' && f != 'Subject'}" value="{!item.task[f]}"/>
        <apex:column rendered="{! f == 'Subject'}" style="white-space: nowrap">
            <apex:facet name="header">Subject</apex:facet>
            <apex:outputLink value="/{!item.task.Id}">{!item.title}</apex:outputLink>
        </apex:column>
        <apex:column rendered="{! f == 'Description'}">
            <apex:facet name="header">Comments</apex:facet>
            <apex:outputText value="{!item.task[f]}"/>
        </apex:column>
        <apex:column rendered="{! f == 'OwnerId'}" headerValue="Assigned To" value="{!item.task.OwnerId}"/>
    </apex:repeat>
</apex:pageBlockTable>

A bit more explanation... Two lists (provided by the controller) are involved. The tasks list is a list of SObjects where each SObject is to be displayed as a row of the table. The fields list is a list of the SObject field names to display where each field is displayed as a column of the table. So it is the job of the controller using describe information or other configuration to build the lists and it is the job of the page to render each column using the appropriate markup.

If you want your logic to be driven by the type of each SObject field then that can be obtained using describe calls (DescribeFieldResult.getType()) and also made available to the page.

I have not done this using input fields so there may be some gotchas there.

| improve this answer | |
  • Could you elaborate on what this line does exactly? Such as the syntax. rendered="{! f != 'OwnerId' && f != 'Description' && f != 'Subject'}". I am a beginner so I am not sure exactly what is going on. Also please look at my edited question if it helps, I added some more details. – ths Feb 19 '14 at 17:21
  • @ths the best way to learn about Visualforce syntax is to work through the various tutorials. The addition to your question suggests you are working on something non-trivial and that will be extra hard if you have no experience of the tools you are aiming to use. – Keith C Feb 19 '14 at 18:44
  • thank you for your help. I will be going through the tutorials and now I believe I understand everything better as well. – ths Feb 19 '14 at 19:47
0

Not entirely sure if I understood your question, but you can use the standard apex:inputField and apex:outputField components to dynamically display correct input fields based on the field type.

An HTML input element for a value that corresponds to a field on a Salesforce object. The component respects the attributes of the associated field, including whether the field is required or unique, and the user interface widget to display to get input from the user. For example, if the specified component is a date field, a calendar input widget is displayed. When used in an , tags always display with their corresponding output label.

inputfield documentation - outputfield documentation

| improve this answer | |

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.