简体   繁体   English

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

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

I have list of h:selectBooleanCheckBox in rich:dataTabe. 我在rich:dataTabe中有h:selectBooleanCheckBox的列表。 Also, there is pagination for the datatable. 另外,数据表也有分页。

The problem is when I click the next page number, the selected checkboxes at the first page of the datatable is gone. 问题是,当我单击下一页页码时,数据表首页上的选定复选框消失了。 Though they are selected, clicking the next/previous page make them deselected. 尽管已选择它们,但单击下一页/上一页会使它们被取消选择。

Any idea about the problem? 对这个问题有任何想法吗?

These are the annotations for bean. 这些是bean的注释。

@ManagedBean(name = "bean") 
@ViewScoped

To clarify it, I've attached my facelets and bean code below: 为了澄清这一点,我在下面附加了我的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 code: 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);
        }   
    }

Many Thanks! 非常感谢!

Since your code is unclear and confusing, I'll provide you the minimal example how pagination with select all on current page MIGHT look like. 由于您的代码不清楚且令人困惑,因此,我将向您提供一个最小的示例,以分页显示当前页面MIGHT上的全选。 With no action listeners, just getters and setter. 没有动作侦听器,只有获取器和设置器。 As simple as I could. 尽可能简单。

First XHTML: 第一个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>

Then the bean: 然后是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);
        }
    }
}

Look how and when the data is changing and when it is loaded. 查看如何以及何时更改数据以及何时加载数据。 Check out that there's no unnessecary new action for checkbox of single row. 确认单行复选框没有不必要的新动作。 JSF will take care of that with: value="#{bean.checked[data]}" . JSF将通过以下方式解决此问题: value="#{bean.checked[data]}"

And once again: Your keys in map are objects. 再一次:您在地图中的键是对象。 You have to make sure that equals method is good. 您必须确保equals方法是好的。 In 95% of case the default is not, especially if they are @Entity . 在95%的情况下,默认值不是,特别是如果它们是@Entity Check ie this topic . 检查即本主题

暂无
暂无

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

相关问题 使用h:selectBooleanCheckbox和c:if来获取a的行值 <rich:datatable> -JSF - Using h:selectBooleanCheckbox with c:if to obtain row value of a <rich:datatable> - JSF 运用 <h:selectBooleanCheckbox> 在分页中使用地图 <p:dataTable> 抛出NullPointerException - Using <h:selectBooleanCheckbox> with Map in a paginated <p:dataTable> throws NullPointerException 使用h:selectBooleanCheckbox删除JSF数据表中的多行 - Using h:selectBooleanCheckbox to delete multiple rows in JSF datatable 如何使用JSF的h:selectBooleanCheckbox和h:dataTable来获取选中的复选框而不提交页面内容? - How to use JSF's h:selectBooleanCheckbox with h:dataTable to get selected checkboxes without submitting the page content? 如何在ah:dataTable中的ah:dataTable中映射ah:selectBooleanCheckbox的值? - How to map the value of a h:selectBooleanCheckbox in a h:dataTable within a h:dataTable? 通过使用h:selectbooleanCheckbox链接到Bean中的HashMap,使h:dataTable单元格可编辑 - Make h:dataTable cell editable by using h:selectbooleanCheckbox linked to HashMap in the bean 使用bean的方法结果时的PropertyNotFoundException <h:selectBooleanCheckbox/> - PropertyNotFoundException when using bean's method's result for <h:selectBooleanCheckbox/> rich:dndParam与h:dataTable - rich:dndParam with h:dataTable 如何选择多个行 <h:dataTable> 同 <h:selectBooleanCheckbox> - How to select multiple rows of <h:dataTable> with <h:selectBooleanCheckbox> 当行包含h:commandLink时,Rich:DataTable排序会中断 - Rich:DataTable sorting breaks when row contains h:commandLink
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM