,

Adding an apex:selectList

The page shown above presents an interface to most of the fields in the old version of the Position__c object. You need to add a new field to take care of the new Position_Type__c field in the object.

For all of the previous fields, you were able to simply use the inputField component within a pageBlockSection. This combination allowed your Visualforce page to simply use all the defaults associated with the field definition—yet another example of the metadata driven quality of the entire Force Platform environment.

This default will use a lookup field for the Position_Type__c object, but you want to add a little more functionality. As you saw in the definition of the Position_Type__c object, a user should not be able to see all position types for a position – just those associated with the department of the position. This functionality is very similar to that presented with a dependent picklist, but, as you will see later, can be expanded with additional capabilities.

The new field will be a selectList that is displayed as a picklist on the page, but the relationship to the Position_Type__c field is not as direct. Although you want to save the result of the user’s selection into that lookup field, you populate the selectList with come of the code in the controller extension you created.

Because of this goal, you must use a slightly different set of tags for this field.

1.
Bring up the new Visualforce page in your Force Platform environment, by adding /apex/VisualforceExtension to the base URL of your Force Platform org, explained above as http://c.instance_name.visual.force.com.

2.
Open the Page Editor for your new page.

3.
Add the highlighted code to your Visualforce page. The code is shown below, along with the code that precedes and follows it in the page.

<apex:pageblocksection columns="1" title="Department">
  <apex:inputField
    value="{!Position__c.Department__c}"/>
</apex:pageblockSection>
<apex:pageblockSection id="dependentPositionType"
								columns="1">
								<apex:pageBlockSectionItem >
								<apex:outputLabel value="Position Type" for="pt"/>
								<apex:panelGrid columns="2">
								<apex:outputText
								value="{!Position__c.Position_Type__c}"
								rendered="false"/>
								<apex:selectList id="pt"
								value="{!positionTypeID}" size="1" >
								<apex:selectOptions
								value="{!PositionTypepptions}"/>
								</apex:selectList>
								</apex:panelGrid>
								</apex:pageBlockSectionItem>
								</apex:pageBlockSection>
<apex:pageBlockSection title="Position Details">

You have added a new pageBlockSection to enclose the new selectList item, a requirement since you want to use a partial page refresh to refresh the values in this picklist when the value for the Department field changes.

The first new thing to see in the code for this new item is the use of the pageBlockSectionItem tags. Since your new picklist is not bound to a particular field in a Force Platform object, you need to use these tags to force the Visualforce page to use the same formatting for the field listed within the tags. The pageBlockSectionItem tags use the outputLabel as the label of the field and the next Visualforce component as the input field, in this case the selectList component. The outputLabel is further associated with the selectList by the use of the id tag on the selectList and the for tag that points to that ID in the outputLabel tag.

The selectList component presents to users as a picklist. The size attribute indicates the number of entries initially for the picklist. With a size="1" attribute, the picklist is displayed as an entry field with a drop down list, like a standard picklist.

You can see that the selectList is bound to the positionTypeID variable in the controller extension field through the familiar value attribute. As you saw inn the previous section, the variable is used to hold the value for the lookup field Position_Type__c, as well as a value that you will be using later in this chapter.

The panelGrid component is here for a specific purpose, but one that is not yet apparent. You can see that the component has a columns="2" attribute, which allows two components to be placed in a single column pageBlockSection. Right now there is only one component, but you will see the use of the grid later in this chapter.

Why the hidden column?

You may have noticed that there is an outputText column bound to the Position_Type__c field, but hidden. You need this column as having a column bound to a field in a Visualforce page is the only way to include the column in the SOQL query for a standard controller. Since you will want to insert a value into this column, having it referenced by field, but not shown, is the most efficient way to make sure the column is available to receive data.


You want to populate the values for this picklist with the values retrieved with the get method of the postionTypeOptions variable. The values in the list are populated with the selectOptions tag. The code behind that get method limits the values placed in the picklist through the selection condition in the SOQL statement.

4.
Save the new version of your Visualforce page.

If you have modified the code properly, your new page should look like the figure below.

Figure 192. The next phase of your Visualforce extension page


Your new page does include a picklist for the Position Type field, but the values for that picklist are not being dynamically repopulated. Your next step is to add this key feature to your page.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.138.123.208