4

So I got my Visualforce Page to display a table with columns (e.g. Version, Date, Type, etc.) as well as values from a dataset for each corresponding column of the next rows.

I'm trying to integrate Custom Settings in the form of selected fields, to be able to determine which columns show up on the Visualforce Page. I've created a Select Fields custom setting in the form of checkboxes for each column name.

Here's my markup without the custom settings integration:

<head>
    <apex:includescript value="//code.jquery.com/jquery-1.11.1.min.js" / >
    <apex:includescript value="//cdn.datatables.net/1.10.4/js/jquery.dataTables.min.js" />
    <apex:stylesheet value="//cdn.datatables.net/1.10.4/css/jquery.dataTables.css" />
    <script>
        j$ = jQuery.noConflict();
        j$(document).ready( function () {
            var Timeline = j$('[id$="Timeline"]').DataTable({

            });
        });
    </script>
</head>
<body>
    <table id="Timeline" class="display">

        <thead>
            <tr>
                <th>Subscription Version</th>
                <th>Start Date</th>
                <th>End Date</th>
                <th>Amendment</th>
                <th>Amendment Date</th>
                <th>Amendment Type</th>
                <th>Amendment Description</th>
                <th>Charge Name</th>
                <th>Charge Price</th>
            </tr>
        </thead>

        <tbody>
            <apex:repeat value="{!subscriptionInfo}" var="cs"> 
                <tr>
                    <td>{!cs.version}</td>
                    <td>{!cs.startDate}</td>
                    <td>{!cs.endDate}</td>
                    <td>{!cs.amendment}</td>
                    <td>{!cs.effectiveDate}</td>
                    <td>{!cs.amdType}</td>
                    <td>{!cs.description}</td>
                    <td>{!cs.chargeName}</td>
                    <td>{!cs.price}</td>
                </tr>
            </apex:repeat>
        </tbody>

    </table>
</body>
5

Use a Field Set instead. It was designed with this scenario in mind.

Working with Field Sets

You can use dynamic bindings to display field sets on your Visualforce pages. A field set is a grouping of fields. For example, you could have a field set that contains fields describing a user's first name, middle name, last name, and business title. If the page is added to a managed package, administrators can add, remove, or reorder fields in a field set to modify the fields presented on the Visualforce page without modifying any code. Field sets are available for Visualforce pages on API version 21.0 or above. You can have up to 50 field sets referenced on a single page.

Working with Field Sets Using Visualforce

Field sets can be directly referenced in Visualforce by combining the $ObjectType global variable with the keyword FieldSets. For example, if your Contact object has a field set called properNames that displays three fields, your Visualforce page can reference the field data through the following iteration:

<apex:page standardController="Contact">
    <apex:repeat value="{!$ObjectType.Contact.FieldSets.properNames}" var="f"> 
        <apex:outputText value="{!Contact[f]}" /><br/>
    </apex:repeat>
</apex:page>

In your case, you would need to use it once for the headers, and again for the data.

<thead>
    <apex:repeat value="{!$ObjectType.MyObject__c.FieldSets.MyFieldSet}" var="field">
        <th>{!field.label}</th>
    </apex:repeat>
</thead>
<tbody>
    <apex:repeat value="{!subscriptionInfo}" var="record">
        <tr>
            <apex:repeat value="{!$ObjectType.MyObject__c.FieldSets.MyFieldSet}" var="field">
                <td>{!record[field]}</td>
            </apex:repeat>
        </tr>
    </apex:repeat>
</tbody>
1

You can access custom setting in Visualforce page through global variable $Setup.

Include your columns in apex:outputPanel and use rendered attribute to display the columns.

<apex:outputPanel id="helpWorkstationHeight" rendered="{!$Setup.App_Prefs__c.Show_Help_Content__c}"> 
<th>Subscription Version</th> 
</apex:outputPanel>

Link : https://developer.salesforce.com/docs/atlas.en-us.pages.meta/pages/pages_variables_global_setup.htm

  • 2
    Worth noting this approach only works for a Hierarchy Custom Setting. – Adrian Larson Jul 27 '16 at 14:10

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.