How to display a dependent list box disabled if no child data exist
- by frank.nimphius
A requirement on OTN was to disable the dependent list box of a model driven list of value configuration whenever the list is empty. To disable the dependent list, the af:selectOneChoice component needs to be refreshed with every value change of the parent list, which however already is the case as the list boxes are already dependent. When you create model driven list of values as choice lists in an ADF Faces page, two ADF list bindings are implicitly created in the PageDef file of the page that hosts the input form. At runtime, a list binding is an instance of FacesCtrlListBinding, which exposes getItems() as a method to access a list of available child data (java.util.List). Using Expression Language, the list is accessible with #{bindings.list_attribute_name.items} To dynamically set the disabled property on the dependent af:selectOneChoice component, however, you need a managed bean that exposes the following two methods //empty – but required – setter method public void setIsEmpty(boolean isEmpty) {} //the method that returns true/false when the list is empty or //has values public boolean isIsEmpty() { FacesContext fctx = FacesContext.getCurrentInstance(); ELContext elctx = fctx.getELContext(); ExpressionFactory exprFactory = fctx.getApplication().getExpressionFactory(); ValueExpression vexpr = exprFactory.createValueExpression(elctx, "#{bindings.EmployeeId.items}", Object.class); List employeesList = (List) vexpr.getValue(elctx); return employeesList.isEmpty()? true : false; } If referenced from the dependent choice list, as shown below, the list is disabled whenever it contains no list data <! -- master list --> <af:selectOneChoice value="#{bindings.DepartmentId.inputValue}" label="#{bindings.DepartmentId.label}" required="#{bindings.DepartmentId.hints.mandatory}" shortDesc="#{bindings.DepartmentId.hints.tooltip}" id="soc1" autoSubmit="true"> <f:selectItems value="#{bindings.DepartmentId.items}" id="si1"/> </af:selectOneChoice> <! -- dependent list --> <af:selectOneChoice value="#{bindings.EmployeeId.inputValue}" label="#{bindings.EmployeeId.label}" required="#{bindings.EmployeeId.hints.mandatory}" shortDesc="#{bindings.EmployeeId.hints.tooltip}" id="soc2" disabled="#{lovTestbean.isEmpty}" partialTriggers="soc1"> <f:selectItems value="#{bindings.EmployeeId.items}" id="si2"/> </af:selectOneChoice>