DataTable 行选择不起作用。

4

我有一个数据表格

<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();" />

我的Backing Bean类 -

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

//Setter and Getters.

问题在于,如果我选择任何一行,"notification" 值为 null。我无法进一步处理,请帮忙。欢迎提供任何替代方法。
编辑:-
我的托管 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 的作用域是什么? - Luiggi Mendoza
你的“rowKey”非常奇怪。PrimeFaces 能处理吗?它有实现 equals/hashcode 吗?我从未尝试过这样做,但我更愿意在这里看到 #{entity.id} - BalusC
@LuiggiMendoza,它在视图范围内。我已经更新了细节。 - theGamblerRises
@BalusC,我已将它编辑为rowKey =“#notificationForm.notificationId}”。但是我仍然得到null。 - theGamblerRises
2个回答

7
你是否将p:dataTable包含在h:form标签中?下面是我成功实现的代码:

通知框 (@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

<?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>

PrimeFaces 6.2存在一个问题(至少),当点击复选框(而不是行中的其他位置)进行行选择时,会阻止事件的触发。具体问题可以参考此链接:https://github.com/primefaces/primefaces/issues/4089。 - Martin Höller

1
如果您的xhtml页面导入了jquery库,可能会出现相同的问题。我将其移除后,选择功能正常工作。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接