Is it possible to filter a p:datatable when the filterBy attribute is a list of obejtos?

Asked

Viewed 308 times

0

I’m using the P:datatable component of Primefaces and I’m having trouble with the filter. I have a list of Email Sectors and within that list I have another list with the aliases of each email.

Code:

            <h:panelGroup id="tabelaEmails" >
                <div class="tabelaEmails" >
                    <p:dataTable id="tabelaFiltro" var="item" value="#{emailSetorBean.sessionBean.itemPagina.listaEmailSetor}" 
                                 widgetVar="dataWidget" 
                                 emptyMessage="Nenhum registro encontrado com os dados buscados.">

                        <p:column filterBy="#{item.email}" headerText="E-Mail" filterMatchMode="contains"
                                  filterValue="#{emailSetorBean.sessionBean.filtroAlteracao}">
                            <h:outputText value="#{item.email}" />
                        </p:column>
                        <p:column filterBy="#{item.descricao}" headerText="Descrição" filterMatchMode="contains">
                            <h:outputText value="#{item.descricao}" />
                        </p:column>
                        <p:column filterBy="#{item.setor.descricao}" headerText="Setor" filterMatchMode="contains">
                            <h:outputText value="#{item.setor.descricao}" />
                        </p:column>
                        <p:column headerText="Alias" filterBy="#{item.listaAlias}" filterMatchMode="contains">
                            <h:dataTable var="alias" value="#{item.listaAlias}" >
                                <p:column>
                                    <h:outputText value="#{alias.email}"/>
                                </p:column>
                            </h:dataTable>
                        </p:column>                        
                    </p:dataTable>
                </div>
            </h:panelGroup>     

The last column is where the aliases are. I put filterBy="#{item.listAlias}" only that the listAlias is a list of type Email, which in turn has the attributes idGoogle and E-mail. I wanted to filter by e-mail. Is there any way to do this using the component?

Thank you!

2 answers

0


I decided by way of a "gambearra".

In the Email class I reset the toString method. That’s how it was:

    @Override
    public String toString() {
        return this.email;
    }

And then in the filterBy excerpt I called the toString of the list. Gave straight:

                        <p:column headerText="Alias" filterBy="#{item.listaAlias.toString()}" filterMatchMode="contains">
                            <h:dataTable var="alias" value="#{item.listaAlias}" >
                                <p:column>
                                    <h:outputText value="#{alias.email}"/>
                                </p:column>
                            </h:dataTable>
                        </p:column>

0

You for using Datatable - Lazy

https://www.primefaces.org/showcase/ui/data/datatable/lazy.xhtml

@Override
    public List<Car> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String,Object> filters) {
        List<Car> data = new ArrayList<Car>();

        //filter
        for(Car car : datasource) {
            boolean match = true;

            if (filters != null) {
                for (Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
                    try {
                        String filterProperty = it.next();
                        Object filterValue = filters.get(filterProperty);
                        String fieldValue = String.valueOf(car.getClass().getField(filterProperty).get(car));

                        if(filterValue == null || fieldValue.startsWith(filterValue.toString())) {
                            match = true;
                    }
                    else {
                            match = false;
                            break;
                        }
                    } catch(Exception e) {
                        match = false;
                    }
                }
            }

            if(match) {
                data.add(car);
            }
        }

        //sort
        if(sortField != null) {
            Collections.sort(data, new LazySorter(sortField, sortOrder));
        }

        //rowCount
        int dataSize = data.size();
        this.setRowCount(dataSize);

        //paginate
        if(dataSize > pageSize) {
            try {
                return data.subList(first, first + pageSize);
            }
            catch(IndexOutOfBoundsException e) {
                return data.subList(first, first + (dataSize % pageSize));
            }
        }
        else {
            return data;
        }
    }

This way you know which field of the datatable is being filtered and the value, so just insert the filters in the SQL that performs the query.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.