2
votes

I have a data table which has paginator and a filter. When the table is filtered and not at the first page and I try to remove a row, I want to keep both the filter and the current page. So I tried something like this:

  try {
            List<Vector> filteredData = incomeTable.getFilteredValue();
            Map<String, Object> filterValue = incomeTable.getFilters();
            if (filteredData == null) {
                filteredData = lstData;
            }

            int index = filteredData.indexOf(selectedRow);
            lstData.remove(selectedRow);
            filteredData.remove(selectedRow);
            if (filteredData.size() > index) {
                selectedRow = filteredData.get(index);
            } else {
                selectedRow = filteredData.get(index - 1);
            }
            onRowSelect();
            incomeTable.setFilteredValue(filteredData);
            incomeTable.setFilters(filterValue);
            incomeTable.setFirst(getFirstRecordShow(filteredData));
        } catch (Exception e) {
            reportException(e);
        }

After this function is processed, I update the table on the client side:

update="@([id$=incomeTable])"

I was able to keep the current page, list of filtered data and display the selected row correctly. But the filter I used on the header rows was cleared. I already tried

incomeTable.setFilters(filterValue); 

to set the value again, but it's still not working.

Does anyone know how to keep both filter and the current page in this case?

My PrimeFaces version is 5.3.

1
@Jasper de Vries : you didn't read all of my requiredment in this case? i want to keep the current page, so both clearFilters or filter aren't acceptable because they will move paginator back to first page.mameo
OK. I improved your question. I'll have a look later.Jasper de Vries
Why not just refrain from updating the datatable and just use an ajax call to remove the row server side and use some simple jquery on the on complete of the ajax call to remove it client-side? No need to filter or whatever. Only small downside is that the page now contains 'rows-1' rows.Kukeltje
@Kukeltje : how to remove row on client side ? PF('incomeTable') doesn't have any remove function aside from removeSelection. Also if i try to remove it from view by deleting whole <tr/> element, my table's index column won't get update so there will be a missing index right?mameo
Does the 'missing' index cause a problem?Kukeltje

1 Answers

2
votes

After some testing, the result turn out to be rather simple.

Just like toggle column visible while using paging (using boolean[]), create a String[] which will hold all the filter value

private String[] colVisible = new String[] {"", "", "", "", ...};

Add filterValue attribute to every column:

<p:column headerText="Header text"
    filterBy="#{item[1]}" filterMatchMode="contains"
    visible="#{bean.colVisible[1]}"
    filterValue="#{bean.filterValue[1]}">
    <h:outputText value="#{item[1]}" />
</p:column>

This way the filter value will be keep even after update. Then when i need to clear filter, i just need to reset all of them back to blank.