代码之家  ›  专栏  ›  技术社区  ›  nosnhoj

带有dataTable的上下文菜单和目标

  •  0
  • nosnhoj  · 技术社区  · 10 年前

    我正在用primefaces5.0进行jsf,下面是我的场景:

    我有一个页面包含一个带有如下上下文菜单的dataTable: enter image description here 当我单击上下文菜单时,我想弹出另一个“窗口”(而不是“选项卡”),其中包含有关所选数据的信息,例如字符串“data1”。

    然而,无论我使用 action url 中的参数 p:menuitem .

    当我使用 行动 参数 anotherPage 显示但在原始窗口中,而打开的新窗口为空: enter image description here

    如果我改变了 action="/anotherPage" 进入 url="/anotherPage.xhtml" , 另一页 显示在新窗口中,但没有所选数据的信息:(请注意,标题已更改为“另一页”)
    enter image description here

    以下是我所做的:

    面板:

     <h:form>
            <p:contextMenu for="dataTable">
                <p:menuitem value="clickMe" icon="ui-icon-gear" 
                            onclick="window.open('', 'Popup', config = 'scrollbars=yes,status=no,toolbar=no,location=no,menubar=no,width=1300,height=370').focus();" 
                            target="Popup" actionListener="#{mainBean.showPopup()}" action="/anotherPage"/>
            </p:contextMenu>
            <p:dataTable id="dataTable" value="#{mainBean.dataList}" var="data" rowIndexVar="index" emptyMessage="Loading..." 
                         selectionMode="single" selection="#{mainBean.selectedStr}" rowKey="#{data}">
                <p:column headerText="data">
                    <p:outputLabel value="#{data}"/>
                </p:column>
            </p:dataTable>    
        </h:form>
    

    BackingBean:

    private List<String> dataList=new ArrayList<>();
    private String selectedStr="";
    
    public void showPopup(){
        Map<String, Object> reqMap=FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
        reqMap.put("param", selectedStr);
    }
    

    另一页.xhtml:

    <p:outputLabel value="This is the selected String:"/>
    <p:outputLabel value="#{anotherPageBean.txt}"/>
    

    另一个PageBean:

    private String txt;    
    
    @PostConstruct
    public void init(){
        Map<String, Object> reqMap=FacesContext.getCurrentInstance().getExternalContext().getRequestMap();
        txt=(String) reqMap.get("param");
    }
    

    非常感谢你。


    使现代化

    我做了很多调查,发现了一些东西:

    1. Target 未为呈现菜单项的属性 行动 ,这是一个 known issue 并且不会被修复。
    2. 菜单项支持 f:params 根据传递参数 optimus.prime .
    3. 当我使用 网址 ,在请求映射中找不到参数,这可能意味着这是一个全新的请求,所以我找不到在reqMap中放置的内容。
    4. 也许我可以将参数放在会话映射中而不是请求映射中,因为我在3中提到了这一点。
    5. 也不 行动 也没有 actionListener 如果我使用 网址 .
    1 回复  |  直到 10 年前
        1
  •  0
  •   nosnhoj    10 年前

    根据我在 使现代化 我找到了一个解决方法。
    请注意,我认为这只是一种变通方法,希望有一些正常的方法来解决它。

    未为操作呈现菜单项的目标属性 我只能使用 url . 所以我尝试使用 f:params 传递参数 网址 但仍然徒劳无功,因为我在 使现代化 我认为。

    之后,我尝试通过backing bean将参数放入会话映射中,并使用 action 调用该方法。然而,它仍然不起作用,因为 行动 如果我使用 网址 .

    根据这个想法,我调用了 menuitem .
    我将事件添加到 dataTable 并在那里调用方法:

    <p:dataTable id="dataTable" value="#{mainBean.dataList}" var="data" rowIndexVar="index" emptyMessage="Loading..." 
                         selectionMode="single" selection="#{mainBean.selectedStr}" rowKey="#{data}">
            <p:ajax event="contextMenu" listener="#{mainBean.setParam()}"/>
            <p:column headerText="data">
                <p:outputLabel value="#{data}"/>
            </p:column>
    </p:dataTable>    
    

    在《背豆》中:

     public void setParam(){
        HttpSession session=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        session.setAttribute("param", selectedStr);
    }
    

    所以我可以通过会话获取信息 anotherPageBean 如:

     @PostConstruct
    public void init(){
        HttpSession session=(HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
        txt=(String) session.getAttribute("param");
        session.removeAttribute("param");
    }
    

    请注意,获取信息后应删除该属性。
    结果是:
    enter image description here 只针对那些有同样问题的人。