Search code examples
javaoracle-adf

ADF checkboxes in table, selected row missing timing


I have a table where the first column is supposed to be a checkbox so that you can select a bunch of rows and for now, just add it to a list stored in the pageflowscope. The problem is that clicking on the checkboxes sometimes has the selected row lagging behind and adds the wrong row. Ex. I click the checkbox in the first row, then while it processes, I uncheck it then click another checkbox, it'll add the first row still because it thinks the selected row is still the first row.

Table:

 <af:table value="#{bindings.documents.collectionModel}" var="row" rows="#{bindings.documents.rangeSize}"
                  emptyText="#{bindings.documents.viewable ? 'No data to display.' : 'Access Denied.'}"
                  rowBandingInterval="0" selectedRowKeys="#{bindings.documents.collectionModel.selectedRow}"
                  selectionListener="#{bindings.documents.collectionModel.makeCurrent}" rowSelection="single"
                  fetchSize="#{bindings.documents.rangeSize}" filterModel="#{bindings.documentsQuery.queryDescriptor}"
                  filterVisible="true" queryListener="#{bindings.documentsQuery.processQuery}" varStatus="vs" id="t1"
                  autoHeightRows="0" styleClass="AFStretchWidth" scrollPolicy="page">
            <af:column id="c41" align="center" width="50">
                <?audit suppress oracle.adf.faces.tablecolneedsheaders?>
                <af:selectBooleanCheckbox id="sbc1" valueChangeListener="#{CopyQueueBean.toggleCheckbox}"
                                          autoSubmit="true">
                    <?audit suppress oracle.adf.faces.compnotlabelled?>
                </af:selectBooleanCheckbox>
            </af:column>

            <af:column sortProperty="#{bindings.documents.hints.id.name}" filterable="true" sortable="true"
                       headerText="#{labels.ID}" id="c1" align="center">
                <af:outputText value="#{row.id}" shortDesc="#{bindings.documents.hints.id.tooltip}" id="ot1"/>
            </af:column>
            <af:column sortProperty="#{bindings.documents.hints.fileName.name}" filterable="true" sortable="true"
                       headerText="#{labels.TITLE}" id="c4" align="center" width="400">
                <af:panelGroupLayout id="pgl2" styleClass="AFStretchWidth" inlineStyle="float:left; text-align:left;">
                        <af:spacer width="20px" id="leftIconSpacer" />
                        <af:image source="#{row.icon}" id="i6"
                                  inlineStyle="width:40.0px;"/>
                        <af:spacer width="40px" id="iconSpacer" />
                        <af:outputText value="#{row.fileName}" id="fot2"/>
                    </af:panelGroupLayout>
            </af:column>

My Bean method:

public void toggleCheckbox(ValueChangeEvent valueChangeEvent) {
    // Add event code here...
    Boolean checked = (Boolean) valueChangeEvent.getNewValue();
    //get list
    ADFContext adfCtx = ADFContext.getCurrent();
    Map params = adfCtx.getPageFlowScope();
    List<Row> list = (List<Row>) params.get("docQueue");
    if (list == null) {
        list = new ArrayList<CopyQueuePojo>();
    }

    DCBindingContainer bindings = (DCBindingContainer) BindingContext.getCurrent().getCurrentBindingsEntry();
    DCIteratorBinding documents = bindings.findIteratorBinding("documentsIterator");
    int index = documents.getCurrentRowIndexInRange();

    Row[] rows = documents.getAllRowsInRange();
    Row line = rows[index];

    if (checked) {
        //add to queue
        if (index >=  0) {                
            //check to make sure there are no duplicates
            boolean exists = false;
            for(int i=0; i<list.size(); i++) {
                if(list.get(i).getId() == ((String) line.getAttribute("id"))) {
                    exists = true; 
                    break;
                }
            }
            if (!exists) {                    
                list.add(line);
            }
        }
    } else {
        //remove from queue
        for(int i=0; i<list.size(); i++) {
            if(list.get(i).getId() == ((String) line.getAttribute("id"))) {
                list.remove(i);
            }
        }
    }
    params.put("docQueue", list);
}

JDEVELOPER: 12.2.1.2.0

Should I be using a different way to set the selected row? or should I not be using valuechangelistener? or is it something else?


Solution

  • The best way to add a selectBooleanCheckbox to a table based on a ViewObject is to add a boolean transient value to your viewObject. Doing so will allow you to have a different boolean value saved for each row and to easily access it in the same ways as the other column values.

    To do so :

    1) Add a isSelected Boolean transient attribute to your ViewObject:

    <ViewAttribute
    Name="isSelected"
    IsSelected="false"
    IsPersistent="false"
    PrecisionRule="true"
    Type="java.lang.Boolean"
    ColumnType="NUMBER"
    AliasName="VIEW_ATTR"
    SQLType="BIT"
    Passivate="true">
    <DesignTime>
      <Attr Name="_diagramName" Value=" "/>
    </DesignTime>
    <Properties>
      <SchemaBasedProperties>
        <CONTROLTYPE
          Value="check_box"/>
        <DISPLAYWIDTH
          Value="10"/>
        <DISPLAYHEIGHT
          Value="10"/>
      </SchemaBasedProperties>
    </Properties>
    

    2) Drag and drop your ViewObject as a table to your view, you should have the following column added :

    <af:column sortable="false" id="c1" width="20">
        <af:selectBooleanCheckbox value="#{row.bindings.isSelected.inputValue}" label="#{row.bindings.isSelected.label}" id="sbc1"/>
    </af:column>
    

    3) In your Bean actions, easily get your boolean value :

        Boolean val = (Boolean )JSFUtils.resolveExpression("#{row.bindings.isSelected.inputValue}");