h:在rich中选择了selectBooleanCheckbox:使用分页时,dataTable会丢失

时间:2016-08-31 08:32:08

标签: jsf datatable richfaces

我在rich:dataTabe中有h:selectBooleanCheckBox列表。此外,还有数据表的分页。

问题是,当我单击下一页编号时,数据表第一页上的选中复选框消失了。虽然已选中它们,但单击下一页/上一页会取消选中它们。

对这个问题有什么看法?

这些是bean的注释。

@ManagedBean(name = "bean") 
@ViewScoped

为了澄清它,我在下面附上了facelets和bean代码:

<rich:dataTable  value="#{bean.ssTable}" var="data" iterationStatusVar="it" id="myDataTable">     
    ...
    <rich:column id="includeInWHMapping" >

        <f:facet name="header">
            <h:selectBooleanCheckbox value="#{bean.selectAll}" valueChangeListener="#{bean.selectAllCheckBox}">
                <f:ajax render="myDataTable" />
            </h:selectBooleanCheckbox>  
        </f:facet>      

       <h:selectBooleanCheckbox id="selectedForWHProcess" value="#{bean.checked[data]}">        
            <f:ajax actionListener="#{bean.selectAllRows}" />
       </h:selectBooleanCheckbox>   

    </rich:column>
    ...
</rich:dataTable>

Bean代码:

    private Map<StandardStructure, Boolean> checked = new HashMap<StandardStructure, Boolean>();
    private boolean selectAll;

    /* Controller */
    public MyController() {
        super(new DataSetParameters()); 

        logger.info("StandardStructureController created.");

        Column rowid_col =new Column("rowid", "rowid",  "No.", FilterTypes.NUMERIC, true, true, "");
        Column fileid_col =new Column("fileid", "fileid",   "File ID", FilterTypes.STRING, true, true, "");
        Column releasetag_col =new Column("releasetag", "releasetag",   "Releasetag ID", FilterTypes.STRING, true, true, "");
        Column applicationid_col =new Column("applicationid", "applicationid",  "Application ID", FilterTypes.STRING, true, true, "");
        Column filename_col =new Column("filename", "filename", "Filename", FilterTypes.STRING, true, true, "ASC");
        Column includeInWHMapping_col =new Column("includeInWHMapping", "includeInWHMapping",   "Include in WH Mapping?", FilterTypes.NONE, true, true, "");

        columns.put("fileid", fileid_col);
        columns.put("releasetag", releasetag_col);
        columns.put("applicationid", applicationid_col);
        columns.put("filename", filename_col);
        columns.put("includeInWHMapping", includeInWHMapping_col);

        initialize();
        setOrderField("importDate");
        setOrder("DESC");   
        dataSetParameters.setColumns(columns);
        loadTable();
    }

    /** getter/setter.. */
    public boolean isSelectAll() {
        return selectAll;
    }
    public void setSelectAll(boolean selectAll) {
        this.selectAll = selectAll;
    }

    public Map<StandardStructure, Boolean> getChecked() {
        return checked;
    }

    public void setChecked(Map<StandardStructure, Boolean> checked) {
        this.checked = checked;
    }
    /** Load ssTable */
    private void loadTable() {
        try{
            ssTable = new StandardStructureDao(dataSetParameters).getAllStandardStructure();
        }catch (Exception ex){
            System.out.println("Exception in loading table:"+ex);
        }
    }

    /** Get ssTable */
    public Collection<StandardStructure> getSsTable(){  
        return ssTable.getDto();
    }

    /** Pagination */
    public void doPaginationChange(ActionEvent event) {         
        super.doPaginationChange(event);
        loadTable();

        /* trying to set the value of list of checkboxes after loading the table */ 
        Iterator<StandardStructure> keys = checked.keySet().iterator(); 
        while(keys.hasNext()){
            StandardStructure ss = keys.next();
            if(checked.get(ss)){ /* getting checked boxes */
                /* Got stuck here. */ 
               /* How do we just set the true (boolean) value only 
                for list of checkboxes though they are in Map?*/
                System.out.println("Row id: " + ss.getRowid() + " Checked : " + checked.get(ss));       
            }
        }
    }

    /** Select all the list of checkbox in datatable */
    public void selectAllCheckBox(){
        for(StandardStructure item : ssTable.getDto()){
            if(!selectAll)
                checked.put(item, true);
            else
                checked.put(item, false);
        }
    }

    /** Select row of data in datatable */
    public void selectAllRows(ValueChangeEvent e) {
        boolean newSelectAll = (Boolean) e.getNewValue();
        Iterator<StandardStructure> keys = checked.keySet().iterator();
        logger.info("Rows selected..." + newSelectAll);
        while(keys.hasNext()) {
            StandardStructure ss = keys.next();
            checked.put(ss, newSelectAll);
            System.out.println("File::"+ss.getRowid()+":"+newSelectAll);
        }   
    }

非常感谢!

1 个答案:

答案 0 :(得分:0)

由于您的代码不清楚且令人困惑,我将为您提供最简单的示例,即如何在当前页面上选择all的分页。没有动作听众,只有getter和setter。尽可能简单。

首先是XHTML:

<h:form>
    <rich:dataTable value="#{bean.valuesOnPage}" var="data" id="dataTable" rows="20">
        <rich:column>
            <f:facet name="header">
                <h:selectBooleanCheckbox value="#{bean.selectAll}">
                    <f:ajax render="dataTable" />
                </h:selectBooleanCheckbox>
            </f:facet>
            <h:selectBooleanCheckbox value="#{bean.checked[data]}">
                <f:ajax />
            </h:selectBooleanCheckbox>
        </rich:column>

        <!-- other columns -->
    </rich:dataTable>
</h:form>

然后是豆子:

@ManagedBean(name = "bean")
@ViewScoped
public class MyBean {

    // when the view is first loaded this is empty
    // until someone will click one of checkboxes
    private Map<Object, Boolean> checked = new HashMap<Object, Boolean>();
    private boolean selectAll;

    private List<Object> valuesOnPage;
    private int currentPage = -1;

    MyBean() {
        setCurrentPage(1);
    }

    // no setter
    public Map<Object, Boolean> getChecked() {
        return checked;
    }

    public int getCurrentPage() {
        return currentPage;
    }

    public boolean getSelectAll() {
        return selectAll;
    }

    // no setter
    public List<Object> getValuesOnPage() {
        return valuesOnPage;
    }

    private void loadTable() {
        try {
            // gets data from data base
            valuesOnPage = getData(currentPage);
        } catch (Exception ex) {
            System.out.println("Exception in loading table:" + ex);
        }
    }

    public void setCurrentPage(int currentPage) {
        if (this.currentPage != currentPage) {
            this.currentPage = currentPage;
            loadTable();
            // we don't need it selected, especially if it
            // was a paged we've never been on
            selectAll = false;
        }
    }

    public void setSelectAll(boolean selectAll) {
        this.selectAll = selectAll;
        for (Object o : valuesOnPage) {
            checked.put(o, selectAll);
        }
    }
}

了解数据如何以及何时更改以及何时加载。检查单行复选框没有任何不必要的新操作。 JSF将通过以下方式处理:value="#{bean.checked[data]}"

再次说明:地图中的键是对象。你必须确保equals方法是好的。在95%的情况下,默认值不是,特别是如果它们是@Entity。检查this topic