我正在使用带有Checkbox的p:dataTable selectionMode =“multiple”,但该复选框无法启用选择。有没有人经历过这个?
<p:dataTable id="gridFaturas" value="#{faturaBean.plano.faturas}" var="fatura"
selection="#{faturaBean.faturasSelecionadas}" rowKey="#{fatura.dataVencimento}" selectionMode="multiple"
paginatorPosition="bottom" rows="13" paginator="true" style="margin-bottom:0" paginatorAlwaysVisible="false"
emptyMessage="Nenhuma Fatura Encontrada.">
<f:facet name="header">Faturas</f:facet>
<p:column selectionMode="multiple" style="width:25px;text-align:center" />
答案 0 :(得分:1)
这对我有用(我不知道你的bean模型,但我看到的是文档模型)
<强> 1。模型强>
public class Fatura implements Serializable{
private long number;
private Date dataVencimento;
private String description;
public Fatura() {
}
public Fatura(long number, Date dataVencimento, String description) {
this.number = number;
this.dataVencimento = dataVencimento;
this.description = description;
}
public long getNumber() {
return number;
}
public void setNumber(long number) {
this.number = number;
}
public Date getDataVencimento() {
return dataVencimento;
}
public void setDataVencimento(Date dataVencimento) {
this.dataVencimento = dataVencimento;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@Override
public int hashCode() {
int hash = 3;
hash = 23 * hash + (int) (this.number ^ (this.number >>> 32));
hash = 23 * hash + Objects.hashCode(this.dataVencimento);
hash = 23 * hash + Objects.hashCode(this.description);
return hash;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Fatura other = (Fatura) obj;
if (this.number != other.number) {
return false;
}
if (!Objects.equals(this.dataVencimento, other.dataVencimento)) {
return false;
}
return true;
}
@Override
public String toString() {
return "Fatura{" + "number=" + number + ", dataVencimento=" + dataVencimento + ", description=" + description + '}';
}
}
<强> 2。控制器Bean
@ManagedBean(name="faturaBean", eager = true)
@ViewScoped
public class FaturasBean {
private List<Fatura> faturasSelecionadas;
private List<Fatura> faturas;
public List<Fatura> getFaturasSelecionadas() {
return faturasSelecionadas;
}
public List<Fatura> getFaturas() {
return faturas;
}
public void setFaturas(List<Fatura> faturas) {
this.faturas = faturas;
}
public void setFaturasSelecionadas(List<Fatura> faturasSelecionadas) {
this.faturasSelecionadas = faturasSelecionadas;
}
@PostConstruct
public void init(){
faturas = new ArrayList<>();
faturasSelecionadas = new ArrayList<>();
faturas.add(new Fatura(1, new Date(), "Example"));
faturas.add(new Fatura(2, new Date(), "Do"));
faturas.add(new Fatura(3, new Date(), "Faturas"));
}
}
第3。图强>
<h:form id="table">
<p:dataTable
id="gridFaturas"
value="#{faturaBean.faturas}"
var="fatura"
selection="#{faturaBean.faturasSelecionadas}"
rowKey="#{fatura.number}"
paginatorPosition="bottom"
rows="13"
paginator="true"
style="margin-bottom:0"
paginatorAlwaysVisible="false"
emptyMessage="Nenhuma Fatura Encontrada.">
<p:ajax event="rowSelect" update=":table:modal" oncomplete="PF('info').show();"/>
<f:facet name="header">Faturas</f:facet>
<p:column headerText="Number" width="20%">
#{fatura.number}
</p:column>
<p:column headerText="Description" width="60%">
#{fatura.description}
</p:column>
<p:column width="20%" headerText="Selection" selectionMode="multiple" style="width:25px;text-align:center"></p:column>
</p:dataTable>
<p:commandLink update=":table:count" value="Count" />
<br />
<h:outputText id="count" value="Total selected: ${faturaBean.faturasSelecionadas.size()}" />
<p:dialog id="modal" modal="true" widgetVar="info" closable="true">
<p:panelGrid columns="2">
<p:outputLabel value="Number"/>
<p:outputLabel value="#{faturaBean.faturasSelecionadas[0].number}"/>
<p:outputLabel value="DataVencimiento"/>
<p:outputLabel value="#{faturaBean.faturasSelecionadas[0].dataVencimento}"/>
<p:outputLabel value="Description"/>
<p:outputLabel value="#{faturaBean.faturasSelecionadas[0].description}"/>
</p:panelGrid>
</p:dialog>
</h:form>
请注意:多项选择仅适用于表格的最后一列。行由数字选择,这必须是唯一的。
请检查我的测试用例