PrimeFaces dataTable: how to catch rows-per-page event?

前端 未结 4 1752
小蘑菇
小蘑菇 2021-01-05 07:03

I cretated a PrimeFaces dataTable:



        
4条回答
  •  生来不讨喜
    2021-01-05 07:43

    You can do that like this:

    The View

    
    
        
            
                #{item.name}
            
        
    
        
    
        
    
        
    
    

    The Bean

    import java.io.Serializable;
    import java.util.ArrayList;
    import java.util.List;
    import java.util.Map;
    import javax.annotation.PostConstruct;
    import javax.faces.bean.ManagedBean;
    import javax.faces.bean.SessionScoped;
    import javax.faces.context.FacesContext;
    
    @ManagedBean
    @SessionScoped
    public class DatatableBean implements Serializable {
    
        private int rows;
    
        private List list;
    
        @PostConstruct
        public void setup() {
            //default rows value
            rows = 2;
    
            list = new ArrayList();
            //ID and Name
            list.add(new SimpleBean(11, "A"));
            list.add(new SimpleBean(22, "B"));
            list.add(new SimpleBean(33, "C"));
        }
    
        public void saveRows(){
            FacesContext context = FacesContext.getCurrentInstance();
            Map map = context.getExternalContext().getRequestParameterMap();
            String rowsStr = (String) map.get("rows");
            rows = Integer.parseInt(rowsStr);
        }
    
        public int getRows() {
            return rows;
        }
    
        public void setRows(int rows) {
            this.rows = rows;
        }
    
        public List getList() {
            return list;
        }
    
        public void setList(List list) {
            this.list = list;
        }
    
    }
    

    The strategy here is to extend the onchange event associated with the html select tags rendered by the paginator. To facilitate that task, I've set the wigetVar in the datatable (dtVar) knowing that it's clientside api gives us both selects through dtVar.paginator.rppSelect.

    Now, to be able to send the value on those selects to the managed bean, we can use a remoteCommand. Using the remoteCommand component we can send javascript parameters to a managedbean. I've named the remoteCommand as persistRows and by calling it I've specified the extra parameters using the pattern required by the component: [{name: 'rows', value: this.value}] ([{name: 'nameOfTheVariable', value: 'valueOfTheVariable'}]).

    Now you can do whatever you want to do with that rows attribute.

提交回复
热议问题