2012-05-02 63 views
0

我有一個JSF h:datatable使用數據行:如何在JSF頁面中單擊行時打開新頁面?

<h:dataTable id="dataTable" value="#{SessionsController.dataList}" binding="#{table}" var="item"> 
        <!-- Check box --> 
        <h:column> 
         <f:facet name="header"> 
          <h:outputText value="Select" /> 
         </f:facet> 
         <h:selectBooleanCheckbox onclick="highlight(this)" value="#{SessionsController.selectedIds[dataItem.id]}" /> 
        </h:column> 
        <h:column> 
         <f:facet name="header"> 
          <h:commandLink value="№" actionListener="#{SessionsController.sort}"> 
           <f:attribute name="№" value="№" /> 
          </h:commandLink> 
         </f:facet> 
         <h:outputText value="#{table.rowIndex + SessionsController.firstRow + 1}" /> 
        </h:column> 
        <h:column> 
         <f:facet name="header"> 
          <h:commandLink value="Account Session ID" actionListener="#{SessionsController.sort}"> 
           <f:attribute name="sortField" value="Account Session ID" /> 
          </h:commandLink> 
         </f:facet> 
         <h:outputText value="#{item.aSessionID}" /> 
        </h:column> 
        <h:column> 
         <f:facet name="header"> 
          <h:commandLink value="User ID" actionListener="#{SessionsController.sort}"> 
           <f:attribute name="sortField" value="User ID" /> 
          </h:commandLink> 
         </f:facet> 
         <h:outputText value="#{item.userID}" /> 
        </h:column> 
        <h:column> 
         <f:facet name="header"> 
          <h:commandLink value="Activity Start Time" actionListener="#{SessionsController.sort}"> 
           <f:attribute name="sortField" value="Activity Start Time" /> 
          </h:commandLink> 
         </f:facet> 
         <h:outputText value="#{item.activityStart}" /> 
        </h:column> 
        <h:column> 
         <f:facet name="header"> 
          <h:commandLink value="Activity End Time" actionListener="#{SessionsController.sort}"> 
           <f:attribute name="sortField" value="Activity End Time" /> 
          </h:commandLink> 
         </f:facet> 
         <h:outputText value="#{item.activityEnd}" /> 
        </h:column> 
        <h:column> 
         <f:facet name="header"> 
          <h:commandLink value="Activity" actionListener="#{SessionsController.sort}"> 
           <f:attribute name="sortField" value="Activity" /> 
          </h:commandLink> 
         </f:facet> 
         <h:outputText value="#{item.activity}" /> 
        </h:column> 
       </h:dataTable> 

我想給當他點擊一行打開一個新頁面的用戶的能力。 我發現JavaScript代碼是有用的:

$("table#dataTable tbody tr").click(function() { 
      window.location = $(this).find("a:first").attr("href"); 
     }); 

的問題是,我需要它會通過JavaScript使用點擊行何時打開新頁面,並通過數據的JSF標籤。在這個例子中,href用於導航到新窗口。我需要JSF標籤,可以用於相同的目的。有沒有合適的JSF標籤?

回答

1

你可以只使用純HTML <a>在JSF。

<a href="page.xhtml">link</a> 

或者<h:link>

<h:link value="link" outcome="page" /> 

無關的具體問題,爲你的jQuery選擇,不要忘記考慮到JSF預先將客戶端ID與ID的父命名容器組件。檢查生成的HTML輸出是否確定。

+0

是否有替代方法來打開新頁面?例如,當我點擊一行來從託管bean調用Java方法時,可能會發生這種情況。 Java方法將返回例如「123acb」。我將在faces-config.xml中將其配置爲導航規則。 –

+0

並非沒有很好的JavaScript負載。例如PrimeFaces的''更容易使用。另請參閱http://www.primefaces.org/showcase/ui/datatableRowSelectionInstant.jsf – BalusC

+0

好吧,我更喜歡使用純JSF。你能想出任何可能的方法來實現我的目標嗎? –