Quantcast
Channel: Ashish Awasthi's Blog
Viewing all articles
Browse latest Browse all 165

Add and delete values in POJO based selectOneListbox/selectOneChoice in ADF

$
0
0
Previously i have posted about populating selectOneChoice programmatically using POJO
Programmatically populate values in a af:selectOneChoice component in ADF

In same way we can populate values in selectOneListBox as both ADF Faces components are used for single selection and share same structure


Here i have used a List to populate values in selectOneListBox (For details read complete article in above link)
And to get selected value from af:selectOneListBox/selectOneChoice in bean- Used a String variable , created it's accessors

//List DataStrucutre to populate values in selectOneListBox
List<SelectItem> customList =new ArrayList<SelectItem>();

publicvoidsetCustomList(List<SelectItem> customList){
this.customList= customList;
}

public List<SelectItem>getCustomList(){
return customList;
}



//String variable to hold selectOneListBox value
private String selectedVal;

publicvoidsetSelectedVal(String selectedVal){
this.selectedVal= selectedVal;
}

public String getSelectedVal(){
return selectedVal;
}

And see here how both List and String variable are bound to af:selectOneListBox

<af:selectOneListboxid="sol1"contentStyle="width:150px;"size="10"value="#{viewScope.SelectOneListBoxBean.selectedVal}">
<f:selectItemsvalue="#{viewScope.SelectOneListBoxBean.customList}"id="si1"var="variable"/>
</af:selectOneListbox>

Now I have dropped an inputText and two buttons on page to add and delete values from selectOneListBox. Created component binding for inputText to get Value in managed bean


<af:panelGroupLayoutid="pgl1"layout="vertical">
<af:inputTextlabel="Enter value to add in Listbox"id="it1"
binding="#{viewScope.SelectOneListBoxBean.itBind}"/>
<af:buttontext="Add Record"id="b1"
actionListener="#{viewScope.SelectOneListBoxBean.addValueInList}"/>
<af:spacerwidth="0"height="10"id="s1"/>
<af:panelGroupLayoutid="pgl2"layout="horizontal">
<af:selectOneListboxid="sol1"contentStyle="width:150px;"size="10"
value="#{viewScope.SelectOneListBoxBean.selectedVal}"
partialTriggers="b1 b2">
<f:selectItemsvalue="#{viewScope.SelectOneListBoxBean.customList}"id="si1"/>
</af:selectOneListbox>
<af:buttontext="Delete"id="b2"
actionListener="#{viewScope.SelectOneListBoxBean.deleteSelectedValue}"/>
</af:panelGroupLayout>
</af:panelGroupLayout>


Now see code for add and delete buttons, Add button actionListener get value from inputText using component binding and add it to List  and Delete button finds and deletes selected value from List


/**Method to add Record in List
* @param actionEvent
*/
publicvoidaddValueInList(ActionEvent actionEvent){
//Get value from inputText using component binding
if(itBind.getValue()!=null){
//Add value in List
customList.add(new SelectItem(itBind.getValue().toString()));
}
}

/**Method that check for selected value in List
* @param items
* @param value
* @return
*/
public SelectItem getItem(List<SelectItem> items, String value){
for(SelectItem si : items){
System.out.println(si.getValue());
if(si.getValue().toString().equalsIgnoreCase(value)){
return si;
}
}
returnnull;

}

/**Method to delete selected record from List
* @param actionEvent
*/
publicvoiddeleteSelectedValue(ActionEvent actionEvent){
if(selectedVal !=null){
//Find and delete selected item from List
customList.remove(getItem(customList, selectedVal));
}
}

All Done :) Run and check application
Enter a value in inputText and click on Add button , It is added in ListBox


 Add 3 values and select second value to delete


 Click on delete button


Sample ADF Application - Download
Cheers :) Happy Learning

Viewing all articles
Browse latest Browse all 165

Trending Articles