h: selectboolean复选框在rich:dataTable中被选中,在使用分页时丢失



我在rich: datatable中有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);
        }   
    }

多谢!

由于您的代码不清楚且令人困惑,因此我将提供一个最小的示例,说明在当前页面上使用select 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]}" .

来处理这个问题。

再次强调:map中的键是对象。你必须确保equals方法是正确的。在95%的情况下,默认值不是,特别是如果它们是@Entity

相关内容

  • 没有找到相关文章

最新更新