Valla
Valla

Reputation: 2442

selecting multiple values from p:datatable via a checkbox and sending it to bean

I am following this link http://balusc.blogspot.com/2006/06/using-datatables.html for selecting multiple checkboxes and retrieving the value in the bean:

My jsf code is below:

<p:dataTable value="#{deviceController.devicesModel}"
            var="item" widgetVar="deviceTable"
            selection="#{deviceController.selectedDevices}"
            rowKey='#{item}'>
    <p:column  sortBy="#{item.manufacturerSerialNum}" filterBy="#{item.manufacturerSerialNum}">
        <f:facet name="header">
            <h:outputText value="Manufacturer Serial No"/>
        </f:facet>
        <h:outputText value="#{item.manufacturerSerialNum}" />  
    </p:column>             
    <p:column selectionMode="multiple">
        <f:facet name="header">
            <h:outputText value="#{bundle.ListLaptopTitle_inService}"/>
        </f:facet>
        <h:selectBooleanCheckbox value="#{item.isInService}"/>
    </p:column>
</p:dataTable> 

<p:commandButton action="#{deviceController.getSelectedItems}" 
        value="Submit Request" style="margin-top: 20px"/>

My bean code is:

public String getSelectedItems(){
        selectedDevicesList = new ArrayList<Device>();
        List<Device> dev=createDeviceList();
        for (Device item :dev ) {
            if (item.getIsInService()) {
                selectedDevicesList.add(item);
                item.setIsInService(false); // Reset.
            }
    }
        return "selected";
    }

What I am not understanding is where are we passing the values of selected rows from the table. Initially, if I have 4 rows and all the check boxes are unchecked now when i check two rows then it should go to the bean and these two values should be updated in database. Like I should get the values of the inservice field for these two beans as true in my bean.Could you let me know what I am missing.Thanks.

Upvotes: 2

Views: 17210

Answers (1)

Aritz
Aritz

Reputation: 31649

You're basically mixing the POST request stuff with the GET one. Your <p:commandButton />'s action method needs only to acquire what is already stored on the bean and send it to your controller layer.

Appart from that, other error is that Primefaces manages the check selection column by its own, while you're manually injecting a <h:selectBooleanCheckbox /> to perfom that.

Replacing your last column by <p:column selectionMode="multiple" style="width:2%" /> will do the work of storing your selection in #{deviceController.selectedDevices}.

Just check this simple SSCCE which provides you the required functionality:

Managed Bean

@ManagedBean
@ViewScoped
public final class TestManagedBean implements Serializable {

    public class Test {
        private int id;
        private String description;
        private boolean selected;

        public Test(int id, String desc) {
            this.id = id;
            this.description = desc;
        }

        public String getDescription() {
            return description;
        }

        public int getId() {
            return id;
        }

        public boolean isSelected() {
            return selected;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void setId(int id) {
            this.id = id;
        }

        public void setSelected(boolean selected) {
            this.selected = selected;
        }

        @Override
        public String toString() {
            return "Test [description=" + description + ", selected="
                    + selected + "]";
        }
    }
    private List<Test> list;

    private List<Test> selectedValues;

    public TestManagedBean() {
    }

    public void actionSave() {
        //Perform DB stuff with selectedValues
        System.out.println("Table saved");
    }

    public List<Test> getList() {
        return list;
    }

    public List<Test> getSelectedValues() {
        return selectedValues;
    }

    @PostConstruct
    public void init() {
        list = Arrays.asList(new Test(1, "Tanzania"), new Test(2, "India"));
    }
}

xhtml page

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Test page</title>
</h:head>
<h:body>
    <h:form>
        <p:dataTable value="#{testManagedBean.list}"
            selection="#{testManagedBean.selectedValues}" var="value"
            rowKey="#{value.id}">
            <p:column>
                #{value.description}
            </p:column>
            <p:column selectionMode="multiple" style="width:2%" />
        </p:dataTable>
        <h:commandButton value="send" action="#{testManagedBean.actionSave}" />
    </h:form>
</h:body>
</html>

Upvotes: 3

Related Questions