This post is about a specific requirement that is to hide some values from adf based select one choice from UI
Previously I have posted about disabling some items from select one choice and this post uses same steps to hide values from lov
Blog about Oracle ADF, JDeveloper, PL/SQL, Java, JavaScript, jQuery and Other Web Technologies
//HashMap and it's accessors private HashMap<String, String> valMap = new HashMap<String, String>(); public void setValMap(HashMap valMap) { this.valMap = valMap; } public HashMap getValMap() { return valMap; } // Object array to store key values of HashMap public Object[] getKeySet() { return getValMap().keySet().toArray(); }
<af:panelFormLayout id="pfl1"> <af:inputText label="Key" id="it1" contentStyle="width:100px;" binding="#{viewScope.ShowHashMapAsTable.keyTextBind}"/> <af:inputText label="Value" id="it2" contentStyle="width:100px;" binding="#{viewScope.ShowHashMapAsTable.valueTextBind}"/> <af:button text="Add" id="b1" actionListener="#{viewScope.ShowHashMapAsTable.addValuesToMap}"/> </af:panelFormLayout>
//Component Binding of inputText to get values private RichInputText keyTextBind; private RichInputText valueTextBind; public void setKeyTextBind(RichInputText keyTextBind) { this.keyTextBind = keyTextBind; } public RichInputText getKeyTextBind() { return keyTextBind; } public void setValueTextBind(RichInputText valueTextBind) { this.valueTextBind = valueTextBind; } public RichInputText getValueTextBind() { return valueTextBind; } /**Add new value to HashMap * @param actionEvent */ public void addValuesToMap(ActionEvent actionEvent) { if (keyTextBind.getValue() != null && valueTextBind.getValue() != null) { valMap.put(keyTextBind.getValue().toString(), valueTextBind.getValue().toString()); } }
#{cVal[row]}
<af:table var="row" rowBandingInterval="0" id="t1" value="#{viewScope.ShowHashMapAsTable.keySet}" partialTriggers="::b1"> <af:iterator id="i1" value="#{viewScope.ShowHashMapAsTable.valMap}" var="cVal"> <af:column sortable="false" headerText="Key" id="c2"> <af:outputText value="#{row}" id="ot4" inlineStyle="font-weight:bold;color:darkblue;"/> </af:column> <af:column sortable="false" headerText="Value" id="c1"> <af:outputText value="#{cVal[row]}" id="ot3"/> </af:column> </af:iterator> </af:table>
//List to show records in selectMany components List<SelectItem> allValuesList; public void setAllValuesList(List<SelectItem> allValuesList) { this.allValuesList = allValuesList; } public List<SelectItem> getAllValuesList() { return allValuesList; }
//List to show records in selectMany components List<SelectItem> allValuesList; public void setAllValuesList(List<SelectItem> allValuesList) { this.allValuesList = allValuesList; } public List<SelectItem> getAllValuesList() { if (allValuesList == null) { allValuesList = new ArrayList<SelectItem>(); allValuesList.add(new SelectItem(1, "India")); allValuesList.add(new SelectItem(2, "Australia")); allValuesList.add(new SelectItem(3, "America")); allValuesList.add(new SelectItem(4, "United Kingdom")); } return allValuesList; }
//List to show selected values in selectMany Component List selectedValues; public void setSelectedValues(List selectedValues) { this.selectedValues = selectedValues; } public List getSelectedValues() { if (selectedValues == null) { selectedValues = new ArrayList(); selectedValues.add(1); selectedValues.add(3); System.out.println("List is-" + selectedValues); } return selectedValues; }
<af:selectManyCheckbox id="smc1" value="#{viewScope.ProgSelectManyComp.selectedValues}"> <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si1"/> </af:selectManyCheckbox>
<af:selectManyChoice label="Label 1" id="smc2" value="#{viewScope.ProgSelectManyComp.selectedValues}" simple="true"> <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si2"/> </af:selectManyChoice>
<af:selectManyListbox label="Label 1" id="sml1" value="#{viewScope.ProgSelectManyComp.selectedValues}" simple="true"> <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si3"/> </af:selectManyListbox>
<af:selectManyShuttle label="Label 1" id="sos1" simple="true" value="#{viewScope.ProgSelectManyComp.selectedValues}" contentStyle="width:50px;"> <f:selectItems value="#{viewScope.ProgSelectManyComp.allValuesList}" id="si4"/> </af:selectManyShuttle>
/**ValueChangeListener for SelectManyChoice (Executes Javascript to replace 'All' text) * @param vce */ public void selectManyVCE(ValueChangeEvent vce) { //String to store all selected Departments Name String displayVal = ""; //Get BindingContainer of current page BindingContext bctx = BindingContext.getCurrent(); BindingContainer bindings = bctx.getCurrentBindingsEntry(); //Get Iterator of SelectManyChoice DCIteratorBinding iter = (DCIteratorBinding) bindings.get("Departments1Iterator"); if (vce.getNewValue() != null) { //Get all selected values in an Object array Object[] selectedVals = (Object[]) vce.getNewValue(); //Iterate over array to get all selected DepartmentId for (int i = 0; i < selectedVals.length; i++) { Integer val = (Integer) selectedVals[i]; //Create Key using DepartmentId to use furhter Key key = new Key(new Object[] { val }); //Get ViewObject row using Key vlaue Row row = iter.getViewObject().getRow(key); // Get DepartmentName from row and add it to String if (displayVal != "") { displayVal = displayVal.concat(", ").concat(row.getAttribute("DepartmentName").toString()); } else { displayVal = displayVal.concat(row.getAttribute("DepartmentName").toString()); } } //Write JavaScript code to change text of selectManyChoice as a StingBuilder Object StringBuilder jsString = new StringBuilder(); //First Step-get clientID of component jsString.append("var elementId = '" + vce.getComponent().getClientId()); //Second- add ::content to access it's value jsString.append("::content';"); //Third- Check that current value is 'All' or not jsString.append("\n if (document.getElementById(elementId).value == 'All') {"); //Forth- if yes then assign Department Name's string as value of selectManyChoice jsString.append("\n document.getElementById(elementId).value ='" + displayVal + "' \n};"); System.out.println("JS File-" + jsString); //Call this JavaScript code using this Helper method writeJavaScriptToClient(jsString.toString()); } }
/**Helper Method to call Javascript * @param javascriptCode */ public static void writeJavaScriptToClient(String javascriptCode) { FacesContext facesCtx = FacesContext.getCurrentInstance(); ExtendedRenderKitService service = Service.getRenderKitService(facesCtx, ExtendedRenderKitService.class); service.addScript(facesCtx, javascriptCode); }
import javax.faces.context.FacesContext; import javax.faces.event.ValueChangeEvent; import oracle.adf.model.BindingContext; import oracle.adf.model.binding.DCIteratorBinding; import oracle.binding.BindingContainer; import oracle.jbo.Key; import oracle.jbo.Row; import org.apache.myfaces.trinidad.render.ExtendedRenderKitService; import org.apache.myfaces.trinidad.util.Service;
<af:selectManyShuttle value="#{bindings.Departments1.inputValue}" label="#{bindings.Departments1.label}" id="sms1" size="15" binding="#{pageFlowScope.ShuttleSelectBean.shuttleBind}" partialTriggers="b1"> <f:selectItems value="#{bindings.Departments1.items}" id="si1"/> <f:validator binding="#{bindings.Departments1.validator}"/> </af:selectManyShuttle>
/**Method to set selected values in af:selectManyShuttle * @param actionEvent */ public void selectValuesShuttle(ActionEvent actionEvent) { DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator"); ViewObject vo = iter.getViewObject(); RowSetIterator rsi = vo.createRowSetIterator(null); ArrayList listVal = new ArrayList(20); while (rsi.hasNext()) { Row nextRow = rsi.next(); if (nextRow.getAttribute("DepartmentId") != null) { listVal.add(nextRow.getAttribute("DepartmentId")); } } shuttleBind.resetValue();// Reset Shuttle Component System.out.println("Setting values -" + listVal); shuttleBind.setValue(listVal.toArray()); // Setting selected values AdfFacesContext.getCurrentInstance().addPartialTarget(shuttleBind); }
List pageLoad = new ArrayList(20); public void setPageLoad(List pageLoad) { this.pageLoad = pageLoad; } public List getPageLoad() { DCIteratorBinding iter = (DCIteratorBinding) getBindingsCont().get("Departments1Iterator"); ViewObject vo = iter.getViewObject(); RowSetIterator rsi = vo.createRowSetIterator(null); while (rsi.hasNext()) { Row nextRow = rsi.next(); if (nextRow.getAttribute("DepartmentId") != null) { pageLoad.add(nextRow.getAttribute("DepartmentId")); } } return pageLoad; }