简体   繁体   中英

Value of <h:selectBooleanCheckbox> inside <p:dataTable> remains false on submit

I have a primefaces datatable and inside the primefaces datatable, I have a column, which contains the . The issue is,I have set the default value for the as false. When I click/check the , its still retrieving the value as false. I tried it multiple times but not sure why its returning false. Please find the sample code below.

  <p:dataTable  id="review-table" var="item" value="#{demandBean.filterVOList}">
  <p:column id="SelectallID" style="text-align: left; width:40px;" rendered="#{demandBean.screeRenderVo.selectAllRenderer}">
 <f:facet name="header" >
  <h:outputText id="selectId"  value="#{demandBean.dmdScreenLabelVO.selectAll}" />
                    <div></div>
<h:selectBooleanCheckbox id="checkbox1" value="Select All" onclick="checkAll(this)"/>
</f:facet>
<h:selectBooleanCheckbox id="checkbox2"  value="#{item.selected}"/>
</p:column>

Im getting the value as false, when I check the and click on the save button. I have written an Action listerner, below is the code corresponding to the actionListener

public void saveData(ActionEvent event)
{
    System.out.println("Entering the Save :");
    selected = isSelected();
    System.out.println("value of Selected"+selected);
}

I have tried debugging the code as well, but not sure why the value for is getting displayed as false. Please Assist. Thanks in Advance

You seem to be binding the value of all checkboxes in the column to the one and same bean property. This way the value will ultimately end up to be the one of the last row in the column.

This is not how it's supposed to be used.

You basically need to bind the value of the checkbox to the property of the currently iterated row object (the one behind the var attribute of the datatable).

<p:dataTable value="#{bean.items}" var="item">
    <p:column>
        <p:selectBooleanCheckbox value="#{item.selected}" />

Alternatively, you could use the <p:column selectionMode="multiple" /> to use builtin multiple selection support of the PrimeFaces datatable ( see also the showcase example ).

<p:dataTable value="#{bean.items}" var="item" rowKey="#{item.id}" selection="#{bean.selectedItems}">
    <p:column selectionMode="multiple" />

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM