primefaces树表中的复选框选择不起作用

时间:2016-06-13 07:48:11

标签: java jsf primefaces

我使用primefaces创建一个多标签页。

如果用户点击动态添加标签的添加按钮,则页面可能包含添加按钮。

在该动态标签中,我使用复选框选项附上树表。

在此页面中,每个值都存储在bean列表中,这是tabview的值。

问题是:

我得到了" selectionNode"第一个选项卡中的值,但无法在其他选项卡中获取值。在日志中,其他选项卡中的选择节点值为空。我尝试了改变会话和视图范围的技术。但失败了。

我附上了以下所有代码。

请帮助我。

的index.xhtml

<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:p="http://primefaces.org/ui"
                xmlns:ui="http://java.sun.com/jsf/facelets" 
                xmlns:c="http://java.sun.com/jsp/jstl/core">

    <h:head>
        <title>
            Dynamic View in List
        </title>
    </h:head>
    <h:body>
        <f:event listener="#{DynamicTab.initializePageAttributes}" type="preRenderComponent"/>
        <h:form id="dynamicFormId">
            <p:panel id="mainPanelId" widgetVar="mainPanelId">
                <p:commandButton id="addButtonId" icon="ui-icon-circle-plus" action="#{DynamicTab.addButtonAction}"
                                 update="mainPanelId" rendered="#{DynamicTab.addButtonRendered}"/>
                <p:growl id="msgs" showDetail="true" escape="false" />

                <p:tabView id="tabViewId" activeIndex="#{DynamicTab.activeIndex}" value="#{DynamicTab.sampleList}" var="useCase"
                           dynamic="true" >
                    <p:ajax event="tabChange" listener="#{DynamicTab.onTabChange}" update="@([id$=mainPanelId])"/>
                    <p:ajax event="tabClose" listener="#{DynamicTab.onTabClose}" update="@([id$=mainPanelId])"/>

                    <p:tab  id="tabId" closable="true" title="#{useCase.sstId}">

                        <p:panel id="sampleId">

                            <h:outputText value="SST Id: "/>  <h:outputText value="#{useCase.sstLabel}"/>

                            <p:commandButton id="uploadButtonId" icon="ui-icon-circle-arrow-n" actionListener="#{DynamicTab.uploadButtonAction}"
                                             update="@([id$=mainPanelId])"/>

                            <p:treeTable value="#{useCase.rootNode}"   
                                         var="document" selectionMode="checkbox" selection="#{useCase.selectedNodes}" >

                                <f:facet name="header">
                                    Tab View
                                </f:facet>

                                <p:column headerText="Name">
                                    <h:outputText value="#{document.name}" />
                                </p:column>

                                <p:column headerText="Size">
                                    <h:outputText value="#{document.size}" />
                                </p:column>

                                <p:column headerText="Type">
                                    <h:outputText value="#{document.type}" />
                                </p:column>

                            </p:treeTable>


                        </p:panel>
                    </p:tab>
                </p:tabView>
            </p:panel>
        </h:form>
    </h:body>
</ui:composition>

点击添加/上传按钮操作我使用下面的Java代码:

DynamicTab.java

public class DynamicTab
{
    private HtmlForm initForm;
    private int count = 0;
    private Logger logger = Logger.getLogger(getClass());
    private List<DynamicBean> sampleList = new ArrayList<DynamicBean>();
    private Short activeIndex;
    private boolean addButtonRendered;
    private TreeNode root;

    public void initializePageAttributes()
    {
        logger.info("Enter inside initializepage");
        count = 0;
        if (sampleList != null && !sampleList.isEmpty())
        {
            sampleList.clear();
        }
        addButtonRendered = true;
    }

    private void addValues()
    {
        formTree();
        DynamicBean dynamic = new DynamicBean();
        dynamic.setSstId("SST" + count);
        dynamic.setSstLabel("SST0" + count);
        dynamic.setTabIndex(String.valueOf(count));
        dynamic.setActiveIndex(activeIndex);
        dynamic.setRootNode(root);
        dynamic.setSelectedNodes(null);
        sampleList.add(dynamic);
    }

    private void formTree()
    {
        root = new CheckboxTreeNode(new Document("Files", "-", "Folder"), null);

        TreeNode documents = new CheckboxTreeNode(new Document("Documents", "-", "Folder"), root);
        TreeNode pictures = new CheckboxTreeNode(new Document("Pictures", "-", "Folder"), root);
        TreeNode movies = new CheckboxTreeNode(new Document("Movies", "-", "Folder"), root);

        TreeNode work = new CheckboxTreeNode(new Document("Work", "-", "Folder"), documents);
        TreeNode primefaces = new CheckboxTreeNode(new Document("PrimeFaces", "-", "Folder"), documents);

        //Documents
        TreeNode expenses = new CheckboxTreeNode("document", new Document("Expenses.doc", "30 KB", "Word Document"), work);
        TreeNode resume = new CheckboxTreeNode("document", new Document("Resume.doc", "10 KB", "Word Document"), work);
        TreeNode refdoc = new CheckboxTreeNode("document", new Document("RefDoc.pages", "40 KB", "Pages Document"), primefaces);

        //Pictures
        TreeNode barca = new CheckboxTreeNode("picture", new Document("barcelona.jpg", "30 KB", "JPEG Image"), pictures);
        TreeNode primelogo = new CheckboxTreeNode("picture", new Document("logo.jpg", "45 KB", "JPEG Image"), pictures);
        TreeNode optimus = new CheckboxTreeNode("picture", new Document("optimusprime.png", "96 KB", "PNG Image"), pictures);

        //Movies
        TreeNode pacino = new CheckboxTreeNode(new Document("Al Pacino", "-", "Folder"), movies);
        TreeNode deniro = new CheckboxTreeNode(new Document("Robert De Niro", "-", "Folder"), movies);

        TreeNode scarface = new CheckboxTreeNode("mp3", new Document("Scarface", "15 GB", "Movie File"), pacino);
        TreeNode carlitosWay = new CheckboxTreeNode("mp3", new Document("Carlitos' Way", "24 GB", "Movie File"), pacino);

        TreeNode goodfellas = new CheckboxTreeNode("mp3", new Document("Goodfellas", "23 GB", "Movie File"), deniro);
        TreeNode untouchables = new CheckboxTreeNode("mp3", new Document("Untouchables", "17 GB", "Movie File"), deniro);

    }

    public String addButtonAction()
    {
        addButtonRendered = sampleList.size() < 4;
        count = count + 1;
        addValues();
        return null;
    }

    public void onTabChange(TabChangeEvent event)
    {
        FacesMessage msg = new FacesMessage("Tab Changed", "Active Tab: " + event.getTab().getTitle());
        FacesContext.getCurrentInstance().addMessage(null, msg);
    }

    public void onTabClose(TabCloseEvent event)
    {
        FacesMessage msg = new FacesMessage("Tab Closed", "Closed tab: " + event.getTab().getTitle());
        FacesContext.getCurrentInstance().addMessage(null, msg);
        TabView tabView = (TabView) event.getComponent();
        addButtonRendered = true;
        List<DynamicBean> tempBeanList = new ArrayList<DynamicBean>();
        tempBeanList.addAll(sampleList);
        for (DynamicBean dynamicBean : tempBeanList)
        {
            if (dynamicBean.getSstId().equalsIgnoreCase(event.getTab().getTitle()))
            {
                sampleList.remove(dynamicBean);
            }
        }

    }

    public String uploadButtonAction()
    {
        DynamicBean dynamic = sampleList.get(activeIndex);
        if (dynamic != null)
        {
            if (dynamic.getSelectedNodes() != null)
            {
                for (TreeNode subTree : dynamic.getSelectedNodes())
                {
                    Document doc = (Document) subTree.getData();
                    logger.info(" Name: " + doc.getName());
                    logger.info(" Type:  " + doc.getType());
                    logger.info(" Size:  " + doc.getSize());
                }
            }
            else
            {
                logger.info("The Selected node is null");
            }
        }
        else
        {
            logger.info("Dynamic is null");
        }
        return null;
    }

    public void displaySelectedMultiple(TreeNode[] nodes)
    {
        if (nodes != null && nodes.length > 0)
        {
            StringBuilder builder = new StringBuilder();

            for (TreeNode node : nodes)
            {
                builder.append(node.getData().toString());
                builder.append("<br />");
            }

            FacesMessage message = new FacesMessage(FacesMessage.SEVERITY_INFO, "Selected", builder.toString());
            FacesContext.getCurrentInstance().addMessage(null, message);
        }
    }

    /**
     * @return the initForm
     */
    public HtmlForm getInitForm()
    {
        return initForm;
    }

    /**
     * @param initForm the initForm to set
     */
    public void setInitForm(HtmlForm initForm)
    {
        this.initForm = initForm;
    }

    /**
     * @return the sampleList
     */
    public List<DynamicBean> getSampleList()
    {
        return sampleList;
    }

    /**
     * @param sampleList the sampleList to set
     */
    public void setSampleList(List<DynamicBean> sampleList)
    {
        this.sampleList = sampleList;
    }

    /**
     * @return the activeIndex
     */
    public Short getActiveIndex()
    {
        return activeIndex;
    }

    /**
     * @param activeIndex the activeIndex to set
     */
    public void setActiveIndex(Short activeIndex)
    {
        this.activeIndex = activeIndex;
    }

    /**
     * @return the addButtonRendered
     */
    public boolean isAddButtonRendered()
    {
        return addButtonRendered;
    }

    /**
     * @param addButtonRendered the addButtonRendered to set
     */
    public void setAddButtonRendered(boolean addButtonRendered)
    {
        this.addButtonRendered = addButtonRendered;
    }
}

Bean类动态Bean包含以下变量及其getter / setter:

DynamicBean.Java

public class DynamicBean
{
    private String sstId;
    private String sstLabel;
    private String tabIndex;
    private String pagePath;
    private Short activeIndex;
    private TreeNode rootNode;
    private TreeNode[] selectedNodes;
}

树表Bean类:

Document.Java

    public class Document implements Serializable, Comparable<Document>
    {

    private String name;
    private String size;
    private String type;

    public Document(String name, String size, String type)
    {
        this.name = name;
        this.size = size;
        this.type = type;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public String getSize()
    {
        return size;
    }

    public void setSize(String size)
    {
        this.size = size;
    }

    public String getType()
    {
        return type;
    }

    public void setType(String type)
    {
        this.type = type;
    }

    //Eclipse Generated hashCode and equals
    @Override
    public int hashCode()
    {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((name == null) ? 0 : name.hashCode());
        result = prime * result + ((size == null) ? 0 : size.hashCode());
        result = prime * result + ((type == null) ? 0 : type.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj)
    {
        if (this == obj)
        {
            return true;
        }
        if (obj == null)
        {
            return false;
        }
        if (getClass() != obj.getClass())
        {
            return false;
        }
        Document other = (Document) obj;
        if (name == null)
        {
            if (other.name != null)
            {
                return false;
            }
        }
        else if (!name.equals(other.name))
        {
            return false;
        }
        if (size == null)
        {
            if (other.size != null)
            {
                return false;
            }
        }
        else if (!size.equals(other.size))
        {
            return false;
        }
        if (type == null)
        {
            if (other.type != null)
            {
                return false;
            }
        }
        else if (!type.equals(other.type))
        {
            return false;
        }
        return true;
    }

    @Override
    public String toString()
    {
        return name;
    }

    public int compareTo(Document document)
    {
        return this.getName().compareTo(document.getName());
    }
}

0 个答案:

没有答案