0

I'm using primefaces lazy data table in a jsf page like below:

<h:form id="searchForm">
    <h:panelGroup id="searchFields">
        <h:outputText value="searchField"/>
        <p:inputMask
                id="txtSearchField"
                dir="LTR"
                mask="9?999999999999"
                maxlength="12"
                value="#{myBackBean.searchField}" />

        <h:commandButton
                id="searchButton"
                value="search"
                action="{myBackBean.searchAction}" />
    </h:panelGroup>
    <p:dataTable
            id="resultTable"
            var="res"
            value="#{res.listOfRecords}"
            paginator="true" paginatorPosition="bottom" rows="10"
            rowsPerPageTemplate="5,10,15"
            lazy="true">

        <p:column headerText="recordId" sortBy="#{res.recordId}">
            <h:outputText value="#{res.recordId}" />
        </p:column>

    </p:dataTable>
</h:form>

As you can see, there is a search panel above the dataTable which contains parameters that user can narrow the result by filling them. There exists a corresponding attribute for each search field in the backing bean like below.

@Component
@Scope("request")
public class myBackBean {

    private String searchField;
    private LazyDataModel<RecordsDto> listOfRecords;

    @Autowired
    MyService myService;

    public String getSearchField() {
        return searchField;
    }

    public void setSearchField(String searchField) {
        this.searchField = searchField;
    }

    public LazyDataModel<OperationsDto> getListOfRecords() {
        return listOfRecords;
    }

    public void setListOfRecords(LazyDataModel<RecordsDto> listOfRecords) {
        this.listOfRecords = listOfRecords;
    }

    public void searchAction() {
        // doing the search and filling the list
        listOfRecords = ...;
        return listOfRecords;
    }
}

when I press the "searchButton" the back bean gets updated and it contains the "searchField" value which the user has inputted. But when I try to sort the grid with the sort arrow on the "recordId" column, my request goes to "getListOfRecords()" method of the back bean, but the "searchField" value is null.

How can I instruct the data table sort actions to update the search fields as well in the back bean?

Kukeltje
  • 12,223
  • 4
  • 24
  • 47
Mostafa
  • 119
  • 1
  • 13
  • Possible duplicate of [How to choose the right bean scope?](https://stackoverflow.com/questions/7031885/how-to-choose-the-right-bean-scope) – Kukeltje Jul 30 '17 at 19:26

1 Answers1

0

Please try with session scope if it is feasible in your case so that search field value will be available.

  • It's not feasible to use session scope, because lazy data table in JSF calls the getter method of the list ("getListOfRecords" method in my case) on sort actions. And according to JSF specification, getter methods get called multiple times. So In that case I can not put my search logic inside my getter method. – Mostafa Jul 31 '17 at 15:14
  • How about setting the value of "searchField" to session scope when search function is invoked ? So that when sort function get's invoked "searchField" value can be obtained from session and it wouldn't be null any more. – Anil Kumar Athuluri Jul 31 '17 at 15:22
  • You mean putting the value of the "searchField" into the session object myself? that seems to be an ugly solution, isn't it? – Mostafa Aug 06 '17 at 05:02