Preselect checkbox in SelectManyCheckbox Component in ADF

Hi All,

In this post I am going to show you how you can preselect the checkbox in SelectManyCheckbox component in ADF.

Suppose you have created SelectManyCheckbox in your page as following:

<?xml version='1.0' encoding='UTF-8'?>
<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page" version="2.1"
          xmlns:f="http://java.sun.com/jsf/core"
          xmlns:af="http://xmlns.oracle.com/adf/faces/rich">
  <jsp:directive.page contentType="text/html;charset=UTF-8"/>
  <f:view>
    <af:document id="d1">
      <af:messages id="m1"/>
      <af:form id="f1">
                     <af:selectManyCheckbox id="smc1"
                               value="#{backingBeanScope.SelectManyCheckboxBean.selectedCheckbox}"
                               autoSubmit="true" label="List of Items">
          <f:selectItems value="#{backingBeanScope.SelectManyCheckboxBean.checkboxList}"
                         id="si1"/>             
        </af:selectManyCheckbox>                         
      </af:form>
    </af:document>
  </f:view>
</jsp:root>

In your bean write down the following lines of code :

package com.kunal.test.view;

import java.util.ArrayList;
import java.util.List;

import javax.faces.model.SelectItem;


public class SelectManyCheckboxBean {
    public SelectManyCheckboxBean() {
        super();
    }
    List<SelectItem> checkboxList;

    public void setCheckboxList(List<SelectItem> checkboxList) {
        this.checkboxList = checkboxList;
    }

    public List<SelectItem> getCheckboxList() {
        if (checkboxList == null) {
            checkboxList = new ArrayList<SelectItem>();
            checkboxList.add(new SelectItem(1, "Tea"));
            checkboxList.add(new SelectItem(2, "Coffee"));
            checkboxList.add(new SelectItem(3, "Juice"));
            checkboxList.add(new SelectItem(4, "Shake"));
        }
        return checkboxList;
    }

    List selectedCheckbox;

    public void setSelectedCheckbox(List selectedCheckbox) {
        this.selectedCheckbox = selectedCheckbox;
    }

    public List getSelectedCheckbox() {
        if(null==selectedCheckbox){
            selectedCheckbox=new ArrayList();
            selectedCheckbox.add(3);
            selectedCheckbox.add(4);
        }
        return selectedCheckbox;
    }

}

 

Now when you run your page you can see the following result on your page.


Happy Coding :)

Comments

Popular posts from this blog

Setting up the environment for Angular2 and Hello World Example in Angular2

Showing number of rows or row count on top and bottom of table in ADF.

Build a Simple ReactJS application using react-cli