2013-12-12 79 views
0

考慮以下代碼,它會顯示一個包含一列和一個過濾器的表以顯示活動/非活動記錄。 如何根據過濾器中選定的值禁用菜單項「無效」?換句話說,如果過濾器值是「無效」,則應禁用「禁用」菜單。PrimeFaces - 動態菜單項

  <p:contextMenu for="ratingScaleTable" id="RsContextMenuId"> 
       <p:menuitem value="Edit" update=":templateForm:tabView, :templateForm:dirtyFlag" icon="ui-icon-search" action="#{ratingScaleBK.edit}" /> 
       <p:menuitem id="inactivate" value="Inactivate" icon="ui-icon-close" action="#{ratingScaleBK.inactivate}" disabled="#{ratingScaleBK.selectedRatingScale.active==0}" /> 
       <p:menuitem value="Activate" update=":templateForm:tabView" icon="ui-icon-close" action="#{ratingScaleBK.activate}"/> 
       <p:menuitem value="View Archive" update=":templateForm:tabView" icon="ui-icon-close"/> 
      </p:contextMenu>       

      <p:dataTable id="ratingScaleTable" widgetVar="tableWidget" 
       value="#{ratingScaleBK.ratingScaleList}" var="item1" 

       selectionMode="single" 
       selection="#{ratingScaleBK.selectedRatingScale}" 
       rowKey="#{item1.name}" 
       rendered="#{not empty ratingScaleBK.ratingScaleList}" 
       filteredValue="#{ratingScaleBK.filteredRatingscale}"> 

       <p:ajax event="rowSelect" process="ratingScaleTable" listener="#{ratingScaleBK.edit}" update=":templateForm:tabView, :templateForm:dirtyFlag, :templateForm:tabView:RsContextMenuId " /> 


       <p:column id="activeCol" filterBy="#{item1.active}" 
        filterOptions="#{ratingScaleBK.activeOptions}" 
        filterMatchMode="exact" width="30"> 
        <h:outputText value="#{item1.active}" /> 
       </p:column> 
      </p:dataTable> 

眼下這個代碼不工作,文本菜單從不更新rowSelect(菜單項「無效」始終啓用)。我猜想在特定行上顯示菜單的右鍵單擊事件並不會真正觸發rowSelect事件,即使該行被突出顯示。 這樣做的正確方法是什麼?

回答

0

文本菜單事件試試:

<p:contextMenu for="ratingScaleTable" id="RsContextMenuId" widgetVar="ctxMenu"> 
    ... 
</p:contextMenu> 

<p:dataTable ... > 
    ... 
    <p:ajax event="contextMenu" process="ratingScaleTable" listener="#{ratingScaleBK.edit}" update=":templateForm:tabView, :templateForm:dirtyFlag, :templateForm:tabView:RsContextMenuId" oncomplete="PF('ctxMenu').show();" /> 
    ... 
</p:dataTable> 
2

我結束了使用從PF forum此解決方案:

<p:contextMenu for="ratingScaleTable" id="RsContextMenuId" widgetVar="ctxMenu" beforeShow="return true;"> 

<p:dataTable id="ratingScaleTable" widgetVar="tableWidget" 
... 
    <p:ajax event="rowSelect" process="ratingScaleTable" listener="#{ratingScaleBK.edit}" update=":templateForm:tabView, :templateForm:dirtyFlag, :templateForm:tabView:RsContextMenuId" /> 
    <p:ajax event="contextMenu" update=":templateForm:tabView:RsContextMenuId" oncomplete="ctxMenu.show(currentEvent);"/> 

和關聯的JavaScript:

<script type="text/javascript"> 
var currentEvent; 
    $(document).ready(function() { 
    PrimeFaces.widget.ContextMenu.prototype.show = function(e) { 
    //hide other contextmenus if any 
    $(document.body).children('.ui-contextmenu:visible').hide(); 

    if(e) { 
     currentEvent = e; 
    } 

    var win = $(window), 
    left = e.pageX, 
    top = e.pageY, 
    width = this.jq.outerWidth(), 
    height = this.jq.outerHeight(); 

    //collision detection for window boundaries 
    if((left + width) > (win.width())+ win.scrollLeft()) { 
     left = left - width; 
    } 
    if((top + height) > (win.height() + win.scrollTop())) { 
     top = top - height; 
    } 

    if(this.cfg.beforeShow) { 
     this.cfg.beforeShow.call(this); 
    } 

    this.jq.css({ 
     'left': left, 
     'top': top, 
     'z-index': ++PrimeFaces.zindex 
    }).show(); 

    e.preventDefault(); 
    }; 
}); 
</script> 
+0

我曾嘗試這個解決方案,但我得到以下錯誤:treeMenu沒有定義。但它被定義爲上下文菜單的widgetVar的名稱。任何想法? – Zardo