通过复选框从p:datatable中选择多个值并将其发送到bean

时间:2013-09-27 16:01:20

标签: jsf primefaces datatable

我正在关注此链接http://balusc.blogspot.com/2006/06/using-datatables.html,以选择多个复选框并检索bean中的值:

我的jsf代码如下:

<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"/>

我的bean代码是:

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";
    }

我不理解的是我们从表中传递所选行的值的位置。最初,如果我有4行并且当我检查两行时所有复选框都未选中,那么它应该转到bean,这两个值应该在数据库中更新。就像我应该在这个bean中得到这两个bean的inservice字段的值为true。你能让我知道我错过了什么。谢谢。

1 个答案:

答案 0 :(得分:2)

您基本上将 POST 请求内容与 GET 混合使用。您的<p:commandButton /> action方法只需要获取已存储在bean上的内容并将其发送到您的控制器层。

来自那里的公寓,其他错误是Primefaces manages the check selection列本身,而你手动注入一个<h:selectBooleanCheckbox />来执行该操作。

<p:column selectionMode="multiple" style="width:2%" />替换上一栏将完成将您的选择存储在#{deviceController.selectedDevices}中的工作。

只需查看这个简单的SSCCE即可获得所需的功能:

托管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页面

<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>