繁体   English   中英

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

[英]h:selectBooleanCheckbox being selected in rich:dataTable is lost when using Pagination

我在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);
        }   
    }

非常感谢!

由于您的代码不清楚且令人困惑,因此,我将向您提供一个最小的示例,以分页显示当前页面MIGHT上的全选。 没有动作侦听器,只有获取器和设置器。 尽可能简单。

第一个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>

然后是bean:

@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 检查即本主题

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM