gpt4 book ai didi

jsf - 从 p :datatable via a checkbox and sending it to bean 中选择多个值

转载 作者:行者123 更新时间:2023-12-04 04:39:55 25 4
gpt4 key购买 nike

我正在关注此链接 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 并且这两个值应该在数据库中更新。就像我应该得到 inservice 的值这两个 bean 的字段在我的 bean 中是真的。你能让我知道我缺少什么吗。谢谢。

最佳答案

你基本上是在混合 发布 使用 请求资料获取 一。您的 <p:commandButton />action方法只需要获取已存储在 bean 上的内容并将其发送到您的 Controller 层。

除此之外,其他错误是Primefaces manages the check selection列独立,而您手动注入(inject) <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>

关于jsf - 从 p :datatable via a checkbox and sending it to bean 中选择多个值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19055249/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com