0

I'm newbie on JSF and I'm working with primefaces-2.2.RC2. I have a commandButton and when I click I want that it fill a dataTable that use lazyLoading (my work is based on primefaces showcase). When I click on commandButton, the data is not loaded.

Here piece of my code:

<p:commandButton value="Search"
              action="#{tableBean.search()}"
              update="carList"
                                     />
<p:dataTable id="carList" 
             var="car" 
             value="#{tableBean.lazyDataModel}" 
             paginator="true" 
             rows="10"
             dynamic="true" 
             lazy="true" 
             paginatorPosition="bottom"
             >

Ande on my bean:

@ViewScoped
public class TableBean {
    LazyDataModel<Car> lazyDataModel;
    public TableBean() {
        lazyDataModel = new LazyDataModel<Car>() {
            @Override
            public List<Car> load(int i, int i1, String string, boolean bln, Map<String, String> map) {
                return new ArrayList<Car>();
            }
        };
    }

    public LazyDataModel<Car> getLazyDataModel() {
         return lazyDataModel;
    }

    public void search() {
        lazyDataModel = new LazyDataModel<Car>() {
            @Override
            public List<Car> load(int first, int pageSize, String sortField,boolean sortOrder, Map<String, String> filters) {
                return fetchLazyData(first, pageSize);
            }

            @Override
            public void setRowIndex(int rowIndex) {                
                setPageSize(10);
                if (rowIndex == -1 || getPageSize() == 0) {
                   super.setRowIndex(-1);
                } else {
                   super.setRowIndex(rowIndex % getPageSize());
                }
            }

            public List<Car> fetchLazyData(int first, int pageSize) {
                 System.out.println("Loading the lazy car data between " + first + " and " + (first + pageSize));
            List<Car> lazyCars = new ArrayList<Car>();

                 for (int i = 0; i < pageSize; i++) {
                     int offset = i + first;
                     lazyCars.add(new Car("Model_" + offset, (int) (Math.random() * 60 + 1960), "Brand_" + offset, "Color_" + offset));
                 }
                 return lazyCars;
            }
       };
       lazyDataModel.setRowCount(10000);
    }
}

update: I descovered that I have to bind my DataTable and on my bean I have to call loadLazyData() function.

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Why are you working with an old beta version? PrimeFaces 2.2 was already finalized more than 2 years ago. – BalusC Oct 28 '13 at 16:13
  • It's a non-functional requirement of project I will work. Apparently they use a custom-made Tomcat that uses this version of primefaces. – João Lucas Scharf Oct 28 '13 at 16:17
  • Are you using `@ManagedBean` annotation? or is writen in your faces-config file (jsf1.2 old style/school)? – danRod Oct 28 '13 at 22:18
  • I`m using @ManagedBen annotation. – João Lucas Scharf Oct 29 '13 at 11:29
  • 1
    Don't put answer in question. Just post the answer as a real answer. Don't yell "RESOLVED" in question title. Just mark the answer accepted and the question will appear differently in listings and search. – BalusC Oct 30 '13 at 13:29

1 Answers1

1

I descovered that I have to bind my DataTable with my bean and on my bean I have to call loadLazyData() function to inform to table to load it.

  • You don't need binding. See https://stackoverflow.com/questions/11918834/primefaces-datatable-lazy-loading-and-commandbutton-per-row – Jasper de Vries Feb 02 '18 at 12:00