2013-10-20 49 views
0

我有這樣的代碼,它顯示我有10行的表,排序完美的作品,但我有一個問題,當我點擊下一頁或最後一頁 - 什麼都沒有發生。每頁行數也不起作用:(也許我的JFS bean應該是@ManagedBean而不是@Named,或者範圍可能不正確?LazyDataModel與分頁,點擊下一頁或最後一頁沒有任何效果

我使用的是:netbeans 7.3.1(glassFish 4 embeded)+ primeFaces 3.5

customers_list.xhtml

<h:form id="form"> 
    <p:dataTable var="cust" value="#{CustomersList.customers}" paginator="true" rows="10" 
          paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}" 
          rowsPerPageTemplate="5,10,15" selectionMode="single" selection="#{CustomersList.selectedCustomer}" id="customerTable" lazy="true"> 

    <p:ajax event="rowSelect" update=":form:display" oncomplete="customerDialog.show()" /> 

    <p:column headerText="Id" sortBy="#{cust.id}"> 
     <h:outputText value="#{cust.id}" /> 
    </p:column> 

    //rest of view... 

</h:form> 

CustomersList.java

package pl.; 

import pl..utils.LazyCustomerDataModel; 
import javax.inject.Named; 
import javax.enterprise.context.RequestScoped; 
import javax.inject.Inject; 
import org.primefaces.model.LazyDataModel; 
import pl..entity.Customer; 

/** 
* 
* @author 
*/ 
@Named(value = "CustomersList") 
@RequestScoped 
public class CustomersList { 

@Inject 
private CustomerSessionBean customerBean; 
private Customer selectedCustomer; 

public CustomersList() { 
} 

public LazyDataModel<Customer> getCustomers() { 
    return new LazyCustomerDataModel(customerBean); 
} 

public Customer getSelectedCustomer() { 
    return selectedCustomer; 
} 

public void setSelectedCustomer(Customer selectedCustomer) { 
    this.selectedCustomer = selectedCustomer; 
} 
} 

LazyCustomerDataModel.java

package pl..utils; 

import java.util.ArrayList; 
import java.util.Collections; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import org.primefaces.model.LazyDataModel; 
import org.primefaces.model.SortOrder; 
import pl..CustomerSessionBean; 
import pl..entity.Customer; 

/** 
* 
* @author 
*/ 
public class LazyCustomerDataModel extends LazyDataModel<Customer> { 

private CustomerSessionBean customerBean; 
private List<Customer> datasource; 

public LazyCustomerDataModel(CustomerSessionBean customerBean) { 
    this.customerBean = customerBean; 
} 

@Override 
public void setRowIndex(int rowIndex) { 
    /* 
    * The following is in ancestor (LazyDataModel): 
    * this.rowIndex = rowIndex == -1 ? rowIndex : (rowIndex % pageSize); 
    */ 
    if (rowIndex == -1 || getPageSize() == 0) { 
     super.setRowIndex(-1); 
    } else { 
     super.setRowIndex(rowIndex % getPageSize()); 
    } 
} 

@Override 
public Customer getRowData(String rowKey) { 
    for (Customer customer : datasource) { 
     if (customer.getName().equals(rowKey)) { 
      return customer; 
     } 
    } 

    return null; 
} 

@Override 
public Object getRowKey(Customer customer) { 
    return customer.getName(); 
} 

@Override 
public List<Customer> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, String> filters) { 

    this.datasource = customerBean.getAllCustomers(first, pageSize); 

    List<Customer> data = new ArrayList<>(); 

    //filter 
    for (Customer customer : datasource) { 
     boolean match = true; 

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

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

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

    //sort 
    if (sortField != null) { 
     Collections.sort(data, new CustomerLazySorter(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; 
    } 
} 
} 
+2

好吧,我都做到了:setRowCount(INT大小)方法必須LazyCustomerDataModel對象(外load()方法) – Grampi

回答

1

我也面臨同樣的問題與延遲加載,請參閱this Link

我發現,對於列表大小總數已設置第一次。

this.datasource = customerBean.getAllCustomers(first, pageSize); 

if(getRowCount()<=0){ 
    setRowCount(customerBean.getCustomersCount()); 
} 

和customerBean.getCustomersCount()獲得總計數在數據庫中的客戶記錄數。

-3

你應該使用@SessionScoped代替@RequestScoped

+0

是啊,他應該使用上調用''@SessionScoped爲他所有的'豆'。好主意! – alexander

+0

'@ SessionScoped'不正確。 '@ ViewScoped'是正確的。另見http://stackoverflow.com/q/7031885 – BalusC

相關問題