本文介绍了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在数据表中.
Here If I am selecting any row, I am going for an ajax call (same as in primefaces instant row selection example). Ajax is inside the Datatable .
<p:ajax event="rowSelect" listener="#{notificationBox.onRowSelect}"
oncomplete="carDialog.show();" />
我的Backing Bean类-
My Backing Bean class -
private List<NotificationForm> notificationsList;
public NotificationForm notification;
public void onRowSelect(SelectEvent event) {
LOGGER.info("Here. +"+notification);
}
//Setter and Getters.
问题是,如果我选择任何行,则通知"为空.我无法进一步处理.请帮忙.也欢迎任何其他方法.
The problem is if I select any row, The "notification" is coming as null. I can not process further. Please help. Any alternate approach is also welcome.
-
我的托管Bean范围-
My Managed Bean Scope -
<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>
推荐答案
您是否将p:dataTable
包裹在h:form
标记中?这实际上对我有用:
Do you have your p:dataTable
wrapped in a h:form
tag? This actually works for me:
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
<?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>
这篇关于DataTable行选择不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!