gpt4 book ai didi

jsf - 如何在Primefaces数据表中放置对象的删除按钮?

转载 作者:行者123 更新时间:2023-12-04 13:16:49 26 4
gpt4 key购买 nike

我有一个包含几行的数据表,有一列,其中有一个带有简单按钮的窗体,该按钮删除了该行中的对象。

所以首先是工作版本:

<h:dataTable value="#{actorTableBackingBean.allActors}" 
var="actor" styleClass="table table-bordered">

<h:column headerText="Actor Name" sortBy="#{actor.firstName}">
<h:outputText value="#{actor.firstName}"/>
</h:column>

<h:column headerText="Actor Detail">
<h:form>
<h:commandButton value="Delete Actor"
styleClass="btn btn-primary"
action="#{actorTableBackingBean.deleteActor(actor.actorId)}"/>
</h:form>
</h:column>
</h:dataTable>

这就是 deleteActor 方法的样子:
public String deleteActor(String id){
removeActorWithId(id);
return "/allActors.xhtml";
}

private void removeActorWithId(String id){
int idk = Integer.parseInt(id);
for(Actor a:allActors){
if(a.getActorId() == idk){
allActors.remove(a);
return;
}
}
}

因此,这完全可以按预期工作。

但是,当我使用 here中所示的Primefaces分页数据表时,删除按钮仅适用于第二种情况下的FIRST ROW,并且仅适用于第一次。
当我单击其他行的“删除”按钮时,什么也没有发生。
可能是什么原因?

对于第二种情况,只需在链接中看到的
标记放在p:dataTable周围,然后将h:dataTable之类的所有内容替换为p:dataTable和p:column等...

最佳答案

我建议您使form标记包含您的数据表。
这有效:

<h:form id="actorsTableForm">
<p:dataTable id="actorsTable" var="item"
value="#{actorsMB.actorList}" selectionMode="single"
rowKey="#{item.description}" paginator="true" rows="10"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
rowsPerPageTemplate="5,10,15" paginatorPosition="bottom">
<p:column headerText="Actor Id" >
<h:outputText value="#{item.id}" />
</p:column>
<p:column headerText="Actor Description">
<h:outputText value="#{item.description}" />
</p:column>
<p:column>
<p:commandButton icon="ui-icon-trash"
title="Delete this actor"
actionListener="#{actorsMB.remove(item)}"
ajax="false" />
</p:column>
</p:dataTable>
</h:form>

以及托管bean中的方法:
public void remove(Actor actor) {
try {
actorService.remove(actor);
actorList = actorService.searchAll();
} catch (Exception e) {
e.printStackTrace();
}
}

希望能帮助到你。

关于jsf - 如何在Primefaces数据表中放置对象的删除按钮?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17495908/

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