How to delete multiple rows in datatable

0

I'm trying to delete multiple rows in my data table at once and get nullPointerException. I am sending the code

Bean

@Named
@ViewScoped
public class PesquisaFamiliasBean implements Serializable {
private static final long serialVersionUID = 1L;

@Inject
private Familias familias;

@Inject
private Empresas empresas;

private List<Familia> listaDeFamilias = new ArrayList<>();

private Familia familiaSelecionada;

private List<Familia> familiasSelecionadas;

public List<Familia> getListaDeFamilias() {
    return listaDeFamilias;
}

public FamiliaLazyList getLazyFamilias() {
    return lazyFamilias;
}

public void setListaDeFamilias(List<Familia> listaDeFamilias) {
    this.listaDeFamilias = listaDeFamilias;
}

public void setLazyFamilias(FamiliaLazyList lazyFamilias) {
    this.lazyFamilias = lazyFamilias;
}

private List<Empresa> empresasRaizes = new ArrayList<>();

public List<Empresa> getEmpresasRaizes() {
    return empresasRaizes;
}

@Inject
private CadastroFamiliaService cadastroFamiliaService;

private Familia novaFamilia = new Familia();

public Familia getNovaFamilia() {
    return novaFamilia;
}

public void setNovaFamilia(Familia novaFamilia) {
    this.novaFamilia = novaFamilia;
}

private FamiliaLazyList lazyFamilias;

public List<Familia> getFamilias() {
    return listaDeFamilias;
}

public void inicializar() {

    if (FacesUtil.isNotPostback()) {
        lazyFamilias = new FamiliaLazyList(familias);
        empresasRaizes = empresas.todasEmpresas();

    }

}

public void doCreateFamlia() {
    cadastroFamiliaService.salvar(novaFamilia);
}

public void doUpdateFamilia(ActionEvent actionEvent) {
    cadastroFamiliaService.salvar(familiaSelecionada);
}

/**
 *
 * @param actionEvent
 */
public void doDeleteFamilias(ActionEvent actionEvent) {

    for (Familia fami : familiasSelecionadas) {
        System.out.println(fami.getDescricao());
    }
    familias.removerFamilias(familiasSelecionadas);
}

public void excluir() {
    familias.remover(familiaSelecionada);
    this.listaDeFamilias.remove(familiaSelecionada);
    FacesUtil.addInfoMessage("Familia " + familiaSelecionada.getDescricao() + " excluída com sucesso.");
}

public Familia getFamiliaSelecionada() {
    return familiaSelecionada;
}

public void setFamiliaSelecionada(Familia familiaSelecionada) {
    this.familiaSelecionada = familiaSelecionada;
}

public List<Familia> getFamiliasSelecionadas() {
    return familiasSelecionadas;
}

public void setFamiliasSelecionadas(List<Familia> familiasSelecionadas) {
    this.familiasSelecionadas = familiasSelecionadas;
}

public FamiliaLazyList getLazyModel() {
    return lazyFamilias;
}

}

Repository where deletion is done:

@Transactional
public void removerFamilias(Familia[] familias) {

    for (Familia familia : familias) {

        try {
            familia = porId(familia.getCodigo());
            manager.remove(familia);
            manager.flush();
        } catch (PersistenceException e) {
            throw new NegocioException("Familia não pode ser excluída.");
        }
    }
}

datatable:

<p:dataTable id="familiasTable"
    value="#{pesquisaFamiliasBean.lazyModel}" var="familia"
    style="margin-top: 20px;" selection="#{pessaFamiliasBean.familiasSelecionadas}"
    emptyMessage="Nenhuma Familia Encontrada"
    paginatorPosition="bottom" 
    rows="20"
    paginator="true"
    paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
                currentPageReportTemplate="(#{msg.pagina} {currentPage} #{msg.de} {totalPages}) - (#{msg.registro} {startRecord} #{msg.a} {endRecord}) - Total ({totalRecords} #{msg.registros})"
                paginatorAlwaysVisible="true" rowsPerPageTemplate="10, 20, 30"
                lazy="true">

    <f:facet name="header">
      #{msg.lista_linhas}
    </f:facet>
    <p:column selectionMode="multiple" style="width:18px" />
    <p:column 
    sortBy="#{familia.descricao}">
    <f:facet name="header">
    <h:outputText value="Descrição da Familia" />
    </f:facet>
    <p:commandLink value="#{familia.descricao}"
                        update=":userDetailForm:display" oncomplete="userDialog.show()"
                        title="View">
                        <f:setPropertyActionListener value="#{familia}"
                            target="#{pesquisaFamiliasBean.familiaSelecionada}" />
    </p:commandLink>

    </p:column>
        <p:column headerText="Descrição Empresa">

            <h:outputText value="#{familia.empresa.nome}" />

    </p:column>

    <f:facet name="footer">
                    <p:commandButton value="New User"
                        oncomplete="newUserDialog.show()" icon="ui-icon-star"
                        title="Criar Nova Familia" />
                    <p:commandButton value="Deletar Familias" process="@this"
                        actionListener="#{pesquisaFamiliasBean.doDeleteFamilias}"
                        update="familiasTable" icon="ui-icon-trash" />
    </f:facet>
</p:dataTable>
    
asked by anonymous 29.05.2017 / 17:58

1 answer

0

Add the rowKey property in the dataTable by passing the var id. Ex: rowKey="# {family.id}"

    
02.06.2017 / 16:12