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
BeeddiskShahBeeddiskShah 

Using Dependent Picklist in Data Table

Hi,

I wanted to use a dependent picklist in a data table. How to pass a selected value from one picklist into the controller?

 

Once the picklist is selected I want its value to be selected.

Edwin VijayEdwin Vijay

You will have to use the get,set methods...

 

 

<apex:selectList value="{!city}" size="1" id="values">
<apex:actionSupport event="onchange" reRender="newvalue" />
<apex:selectOptions value="{!citynames}"/>
</apex:selectList>

 

 Controller code will be

 

 

public String city{get; set;}

 

 Now the variable city will be holding the value you selected in the Picklist... You can use the variable "City" in your controller for further processing...

 

A detailed example is available here :- Salesforce Unearthed - Dynamically add values to picklist

 

 

BeeddiskShahBeeddiskShah
Hi this method works for one list on the table...I am using a datatable and there will be multiple rows and multiple picklist.
ThomasTTThomasTT

Points are

 

  - You need to tell the controller that which object is selected

  - You need to re-render (at least) the selected row

 

So, I would (did) do like this:

 

 


<apex:pageBlockTable id="table" value="{!data}" var="item">
<apex:column>
<apex:selectList id="stateList" value="{!item.state}" size="1" id="values">
<apex:actionSupport event="onchange" action="{!changeCityList}" reRender="cityList">
<apex:param name="pid" assignTo="{!selectedId}" value="{!item.pid}">
</apex:actionSupport>
<apex:selectOptions value="{!statenames}"/>
</apex:selectList>
</apex:column>
<apex:column>
<apex:selectList id="cityList" value="{!item.city}" size="1" id="values">
<apex:selectOptions value="{!item.citynames}"/>
</apex:selectList>
</apex:column></apex:pageBlockTable>
</apex:pageBlockTable>

 

 

 

public class TheController {
public class MyObject {
public String pid {get; set;}
public String state {get; set;}
public List<String> statenames {get; set;}
public String city {get; set;}
public List<String> citynames {get; set;}
}

public List<MyObject> data {get; set;}
public String selectedId {get; set;}

public PageReference changeCityList(){
if(selectedId == null) return null;
for(MyObject o : data){
if(o.pid != selectedId) continue;
// update o.citynames based on state
return null;
}
return null;
}
 

Be careful with reRender on the controlling actionSupport ("cityList" in the sample in red).

If you want to re-render only THE ROW, you can do that. you just specify the column id because the id is relative, so if you didn't specify the entire id structure, SFDC assume it's about on the same row.

However, if user change 2 rows very quicky enough to change other row before the first call is done, the first change will be overridden because of View State conflict.

If you want to re-render the entire table, user can change only row by row, inconvenient, but safer.

 

I usually use record id for the row identifier and maybe I prepare for a map<id, MyObject> to get the object without loop. You can do it with lineNumber... but if your page has a function to delete a table row, it may screw it up, so I prefere to use recordid (& loop).

 

ThomasTT

Message Edited by ThomasTT on 09-17-2009 10:36 AM
HareHare

please help me in this bellow senario

 

Dynamic dependent pick list using apex :
 i created one Location__c object in that i Created two Pick Lists Country__c and State__c in Country__c pick list
i added India,pakistan valus and in state__c pick list i added Andrapradesh,Madhyapradesh,lohore,Quetta
if i select a value as India from Country__c pick list in State__c only able to see Andrapradesh,Madhyapradesh
if i select a value as pakistan from country_c picklist in state__c only able to see lohore,Quetta by using apex