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

0

I am using the P: dataTable component of Primefaces and I am having problem with the filter. I have an Email List of 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.listaAlias}" only the Alias list is a mail type list, which in turn has the idGoogle and E-mail attributes. I wanted to filter by e-mail. Is there any way to do this using the component?

Thank you!

    
asked by anonymous 18.04.2018 / 01:44

2 answers

0

I solved it by means of a "gambearra".

In the mail class, 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 from the list. Gave it right:

                        <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>
    
18.04.2018 / 23:26
0

You use the DataTable - Lazy

link

@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 is how you know which field of the dataTable being filtered and the value, so you only have to insert the filters into the SQL query.

    
18.04.2018 / 16:51