调用onCellEdit

时间:2018-03-06 13:15:46

标签: primefaces jsf-2 datatable lazy-loading

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

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

这是我的ata.xhtml DATATABLE(Sanitized)

<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 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方法来获取DatamodelDatatable的数据来解决问题。

我不确定这是一个好/坏的做法,或者在使用LazyLoading时你应该如何检索该行。

另外,根据@Kukeltje的建议,我现在正在使用Primefaces Documentation

修改了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 个答案:

答案 0 :(得分:0)

我能够通过不使用rowKey检索数据并修改onCellEdit方法来获取DatamodelDatatable的数据来解决问题。

我不确定这是一个好/坏的做法,或者当你使用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());

        }

    }

答案 1 :(得分:0)

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

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