调用onCellEdit时DataTable的RowKey为null

RowKey of DataTable is null when calling onCellEdit

我有一个使用 LazyDataModel bean 的 Primefaces 6.0 DataTable。在我改成 lazy 实现后,cell 版就停止工作了。 原因是每当我调用我的 onCellEdit 方法,并尝试通过调用 event.getRowkey() 来获取单击的行内容时,我都会得到一个 null 对象。 根据 Primefaces Documentation,我有一个 rowKey 属性来将元组与 bean 值绑定,但它似乎不起作用。

编辑:我现在可以更新值,但 dataTable 不会重新加载单元格 UiElement。要查看更改,我必须 F5 页面。

这是我的 ata.xhtml 数据表(经过消毒)

<p:tabView id="tab" value="#{setorMB.listaSetor}" var="tabView" 
    activeIndex="#{ataGerencialMB.activeTabIndex}">
    <p:ajax event="tabChange" listener="#{ataGerencialMB.onTabChange}"
            update="tab ,formListagemCapacitacao" />
    <p:tab title="#{tabView.sigla}">
        <p:dataTable    id="dtCapacitacao" 
                value="#{ataGerencialMB.lazyModelAtaGerencial}"
                var="gerencial"  
                lazy="true"
                paginator="true" 
                rows="#{Config.NUM_ROWS}"
                currentPageReportTemplate="#{Config.CURRENT_PAGE_REPORT_TEMPLATE}"
                paginatorTemplate="#{Config.PAGINATOR_TEMPLATE}"
                rowsPerPageTemplate="#{Config.ROWS_PER_PAGE_TEMPLATE}"
                sortBy="#{gerencial.idAta}" 
                sortOrder="ascending"
                reflow="true" 
                editable="true" 
                editMode="cell"
                rowKey="#{gerencial.idAta}">

        <p:ajax event="cellEdit"
                listener="#{ataGerencialMB.onCellEdit}" oncomplete="onCellEdit()"/>

        <p:column>
            <p:rowToggler/>
        </p:column>

        <p:column headerText="Tipo Assunto" >
            <p:cellEditor>
                <f:facet name="output">
                    <h:outputText value="#{gerencial.tipo}" 
                                  rendered="true" />
                </f:facet>

                <f:facet name="input">
                    <p:inputTextarea id="tipo" 
                       value="#{gerencial.tipo}" 
                       style="width:96%" />
                </f:facet>
            </p:cellEditor>
        </p:column>
      </p:dataTable>
    </p:tab>
</p:tabView>

Class 扩展了 LazyDataModel AtaGerencialLazyDataModel

public class AtaGerencialLazyDataModel extends LazyDataModel<AtaGerencialBean> {

AtaGerencialBusiness ataBusiness = new AtaGerencialBusiness();
Map<String, Object> customFilters = new HashMap<String, Object>();
private List<AtaGerencialBean> listaAtaGerencialBean = new ArrayList<AtaGerencialBean>();

public AtaGerencialLazyDataModel(){
    this.setRowCount(ataBusiness.getAtaGerencialTotalCount(null));
}

public AtaGerencialLazyDataModel(SetorBean setor){
    customFilters.put("setor", setor);
    this.setRowCount(ataBusiness.getAtaGerencialTotalCount(customFilters));
}

@Override
public List<AtaGerencialBean> load(int first, int pageSize, String sortField,
        SortOrder sortOrder, Map<String, Object> filters){
    List<AtaGerencialBean> list = ataBusiness.fetchLazyAtaGerencial(first, pageSize, sortField, sortOrder, customFilters);
    this.setRowCount(ataBusiness.getAtaGerencialTotalCount(customFilters));
    setListaAtaGerencialBean(list);
    setWrappedData(list);
    return list;
}

@Override
public AtaGerencialBean getRowData(String rowKey){
    try{
        long id = Long.parseLong(rowKey);
        for (AtaGerencialBean bean : listaAtaGerencialBean) {
            if (bean.getIdAta() == id){
                return bean;
            }

        }

    }catch(Exception e){
        System.out.println(e.getMessage());
    }
    return null;

}

@Override
public Object getRowKey(AtaGerencialBean p) {
    return p.getIdAta();
}

public List<AtaGerencialBean> getListaAtaGerencialBean() {
    return listaAtaGerencialBean;
}

public void setListaAtaGerencialBean(
        List<AtaGerencialBean> listaAtaGerencialBean) {
    this.listaAtaGerencialBean = listaAtaGerencialBean;
}

}

onCellEdit 方法

 @ManagedBean
 @ViewScoped
 public class AtaGerencialMB extends MB<AtaGerencialBean, 
 AtaGerencialBusiness> {

     private LazyDataModel<AtaGerencialBean> lazyModelAtaGerencial;    

     @SuppressWarnings("unused")
     public void onCellEdit(CellEditEvent event) {
      try{
        DataTable controladorTabela = (DataTable) event.getComponent();
        //rowindex is fine, it brings the index of the edited row in the 
 datatable (from 0 onwards)
        Integer rowIndex = event.getRowIndex();
        String rowKey = event.getRowKey();

        //rowKey is always null
        System.out.println("rowKey value:" + rowKey);

        AtaGerencialBean entity = (AtaGerencialBean) controladorTabela.getRowData(event.getRowKey());
        this.setRegistroDefinido(entity);
        super.atualizar();
    }catch(NullPointerException ex){
        System.out.println(ex.getMessage());
    }

  }
}

编辑

我能够通过不使用 rowKey 检索数据并修改 onCellEdit 方法从 Datatable 中的 Datamodel 获取数据来规避问题=].

我不确定这是否是一种 good/bad 做法,或者这是否是您在使用 LazyLoading 时检索行的方式。

此外,根据@Kukeltje 的建议,我现在使用 PRIMEFACES 6.2

修改onCellEdit方法

    @ManagedBean
    @ViewScoped
    public class AtaGerencialMB extends MB<AtaGerencialBean, AtaGerencialBusiness> {

   private LazyDataModel<AtaGerencialBean> lazyModelAtaGerencial;    
    @SuppressWarnings("unused")
        public void onCellEdit(CellEditEvent event) {
            try{
                DataTable controladorTabela = (DataTable) event.getComponent();
                DataModel dm = (DataModel) controladorTabela.getValue();
                AtaGerencialBean entity = (AtaGerencialBean) dm.getRowData();           
                this.setRegistroDefinido(entity);
                super.atualizar();
            }catch(NullPointerException ex){
                System.out.println(ex.getMessage());

            }

        }

    }

我能够通过不使用 rowKey 检索数据并修改 onCellEdit 方法从 Datatable 中的 Datamodel 获取数据来规避问题=].

我不确定这是否是一种 good/bad 做法,或者这是否是您在使用 LazyLoading 时检索行的方式。

此外,根据@Kukeltje 的建议,我现在使用 PRIMEFACES 6.2

修改onCellEdit方法

@SuppressWarnings("unused")
    public void onCellEdit(CellEditEvent event) {
        try{
            DataTable controladorTabela = (DataTable) event.getComponent();
            DataModel dm = (DataModel) controladorTabela.getValue();
            AtaGerencialBean entity = (AtaGerencialBean) dm.getRowData();           
            this.setRegistroDefinido(entity);
            super.atualizar();
        }catch(NullPointerException ex){
            System.out.println(ex.getMessage());

        }

    }

https://github.com/primefaces/primefaces/issues/2688

您可以在 github 上参考此问题。您应该为您的数据表启用选择