代码之家  ›  专栏  ›  技术社区  ›  Anika

调用onCellEdit时,DataTable的RowKey为null

  •  2
  • Anika  · 技术社区  · 6 年前

    我有一个Primefaces 6.0 DataTable 使用 LazyDataModel 豆在我更改为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>
    

    类,该类扩展LazyDataModel AtageRecialazyDataModel

    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());
        }
    
      }
    }
    

    编辑

    通过不使用 行键 检索数据并修改 onCellEdit公司 方法从 Datamodel 内部 Datatable .

    我不确定这是否是一种好的/坏的做法,或者在使用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());
    
                }
    
            }
    
        }
    
    2 回复  |  直到 6 年前
        1
  •  0
  •   Anika    6 年前

    通过不使用 rowKey 检索数据并修改 onCellEdit 方法从 Datamodel 内部 Datatable .

    我不确定这是否是一种好的/坏的做法,或者在使用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());
    
            }
    
        }
    
        2
  •  0
  •   jackycflau    5 年前

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

    您可以在github上参考此问题。您应该为datatable启用选择