2013-08-29 57 views
2

我有一個DataTable數據錶行選擇不工作

<p:dataTable id="db" 
    value="#{notificationBox.notificationsList}" 
    var="notificationForm" 
    rows="15" 
    emptyMessage="${msgs.getMessage('table.empty.message')}" 
    paginator="true" 
    paginatorPosition="bottom" 
    rowKey="#{notificationForm}" 
    selection="#{notificationBox.notification}" 
    paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} (${notificationBox.notificationsList.size()} ${msgs.getMessage('table.records')})" 
    selectionMode="single" 
    tableStyle="height:430px"> 

    //Rest of the code 

這裏如果我選擇的任何行,我去爲一個Ajax調用(相同primefaces即時行選擇的例子)。 Ajax位於Datatable中。

<p:ajax event="rowSelect" listener="#{notificationBox.onRowSelect}" 
    oncomplete="carDialog.show();" /> 

我支持bean類 -

private List<NotificationForm> notificationsList; 
public NotificationForm notification; 
public void onRowSelect(SelectEvent event) { 
    LOGGER.info("Here. +"+notification); 
} 

//Setter and Getters. 

的問題是,如果我選擇任何行, 「通知」 即將爲空。我無法進一步處理。請幫忙。任何替代方法也是受歡迎的。

編輯: -

我的Managed Bean的範圍 -

<managed-bean> 
    <managed-bean-name>notificationBox</managed-bean-name> 
    <managed-bean-class>com.comviva.workflow.ui.notification.NotificationBox</managed-bean-class> 
    <managed-bean-scope>view</managed-bean-scope> 
    <managed-property> 
     <property-name>notificationDao</property-name> 
     <value>#{notificationDaoService}</value> 
    </managed-property> 
    <managed-property> 
     <property-name>userInfoDao</property-name> 
     <value>#{userInfoDaoProxy}</value> 
    </managed-property> 
</managed-bean> 
+1

託管bean的範圍是什麼? –

+0

你的'rowKey'奇怪地令人懷疑。 PrimeFaces會吃那個嗎?它是否實現了equals/hashcode?我從來沒有嘗試過,但我希望在這裏看到一個'#{entity.id}'。 – BalusC

+0

@LuiggiMendoza,在視野範圍內。我已經更新了細節。 – theGamblerRises

回答

5

你有你p:dataTable裹在h:form標籤?這實際上對我的作品:

NotificationBox(@ViewScoped)

package com.mycompany; 

import java.util.Arrays; 
import java.util.List; 

import javax.faces.bean.ManagedBean; 
import javax.faces.bean.ViewScoped; 

import org.primefaces.event.SelectEvent; 

@ManagedBean 
@ViewScoped 
public class NotificationBox { 

    public class NotificationForm { 

     Integer notificationId; 

     String name; 

     public NotificationForm(Integer id, String nam) { 
      notificationId = id; 
      name = nam; 
     } 

     public String getName() { 
      return name; 
     } 

     public Integer getNotificationId() { 
      return notificationId; 
     } 

     @Override 
     public String toString() { 
      return "NotificationForm [notificationId=" + notificationId 
        + ", name=" + name + "]"; 
     } 
    } 

    private List<NotificationForm> notificationsList; 

    public NotificationForm notification; 

    public NotificationBox() { 
     notificationsList = Arrays.asList(new NotificationForm(1, "Form1"), 
       new NotificationForm(2, "Form2")); 
    } 

    public NotificationForm getNotification() { 
     return notification; 
    } 

    public List<NotificationForm> getNotificationsList() { 
     return notificationsList; 
    } 

    public void onRowSelect(SelectEvent event) { 
     System.out.println(event.getObject()); 
    } 

    public void setNotification(NotificationForm notification) { 
     this.notification = notification; 
    } 

} 

的index.xhtml如果您的XHTML頁面進口jQuery庫可發生

<?xml version='1.0' encoding='UTF-8' ?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 

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

<h:head> 
</h:head> 

<h:body> 

    <h:form> 
     <p:dataTable id="db" value="#{notificationBox.notificationsList}" 
      var="notificationForm" rows="15" 
      emptyMessage="${msgs.getMessage('table.empty.message')}" 
      paginator="true" paginatorPosition="bottom" 
      rowKey="#{notificationForm.notificationId}" 
      selection="#{notificationBox.notification}" 
      paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} (${notificationBox.notificationsList.size()} ${msgs.getMessage('table.records')})" 
      selectionMode="single" tableStyle="height:430px"> 

      <p:ajax event="rowSelect" listener="#{notificationBox.onRowSelect}" /> 

      <p:column> 
     #{notificationForm.name} 
     </p:column> 
     </p:dataTable> 
    </h:form> 
</h:body> 
</html> 
+0

謝謝,你救了我的一天。 :) – Sertage

0

同樣的問題。我刪除它,選擇工作。