【问题标题】:selecting multiple values from p:datatable via a checkbox and sending it to bean通过复选框从 p:datatable 中选择多个值并将其发送到 bean
【发布时间】:2013-09-27 16:01:20
【问题描述】:

我正在关注此链接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 字段的值一样。你能告诉我我缺少什么吗。谢谢。

【问题讨论】:

  • 看起来您走在正确的道路上。当您单击复选框时,item.isInService 将被切换。当您单击命令按钮时,您正在构建一个selectedDevicesList,您可以使用它来更新您的数据库。您应该使用selectedDevicesList 来创建和执行您的数据库更新语句。
  • 我还没有完全阅读您的问题/问题,但是您在那里链接的文章针对的是 JSF 1.x 数据表。在 PrimeFaces 中,您无需自己将&lt;h:selectBooleanCheckbox&gt; 放入&lt;p:column selectionMode&gt;

标签: jsf primefaces datatable


【解决方案1】:

您基本上是在将 POST 请求内容与 GET 请求内容混合在一起。您的&lt;p:commandButton /&gt;action 方法只需要获取已存储在bean 上的内容并将其发送到您的控制器层。

除此之外,其他错误是 Primefaces manages the check selection 列本身,而您手动注入 &lt;h:selectBooleanCheckbox /&gt; 以执行该操作。

将您的最后一列替换为&lt;p:column selectionMode="multiple" style="width:2%" /&gt; 将完成将您的选择存储在#{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>

【讨论】:

  • 你能看看这篇文章,让我知道它是如何实现的。谢谢你的帮助。
  • 当您选择复选框本身时,值不会更改,但在您发送表单时。这是 JSF 生命周期的一部分,特别是模型更新部分。之后,使用其属性中的值,执行action 方法。
猜你喜欢
  • 2018-04-27
  • 2014-07-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-04
  • 2021-09-22
  • 1970-01-01
  • 2011-09-23
相关资源
最近更新 更多