将所有行保存在 h:dataTable 中
我有一个带有 h:dataTable
的 Facelets 页面。在 h:dataTable
的每一行中,我显示用户的一些启用和禁用的服务。这是模型对象
public class ServiceList {
private long userId;
private long serviceGroupId;
private String serviceGroupName;
private long serviceId;
private String serviceName;
private String serviceUrl;
private String serviceState;
public UserServiceList() {
}
//getters and setters....
}
这些是我在数据表的单行中显示的详细信息。 上述模型对象中的 serviceState 是“Y”或“N”。
我的问题是应用程序用户应该能够立即更新 dataTable 的所有行的服务状态,并在后端数据库中更新它们。
1)我需要在 dataTable 内部使用什么额外的 JSF 组件来达到这个目的?我正在考虑使用 h:selectOneradio 添加一列
2)如何获取选择了哪些行以及它们设置了什么状态?
我是 JSF 的新手。请帮忙。
更新:目前我在表格的页脚部分有两个按钮,即“禁用服务”和“启用服务”。
单击“禁用服务”时,我将导航到另一个页面,在该页面中向应用程序用户显示要禁用的已启用服务的列表
,反之亦然,单击“已启用服务”按钮。
I have a Facelets page with a h:dataTable.
In each row of the h:dataTable
, i am displaying some enabled and disabled services of a user.Here is the model object
public class ServiceList {
private long userId;
private long serviceGroupId;
private String serviceGroupName;
private long serviceId;
private String serviceName;
private String serviceUrl;
private String serviceState;
public UserServiceList() {
}
//getters and setters....
}
These are the details i am displaying in a single row of a dataTable.
serviceState in the above model object is either 'Y' or 'N'.
my problem is the application user should be able to update the servicestate of all rows of a dataTable at once and update them in the backend database.
1)what additional JSF component do i need to use inside dataTable to achive this? I am thinking of adding one more column with h:selectOneradio
2)How do i get which rows are selected and what status they have set?
I am kind of newbee to JSF.Please help.
Update:At present what i am having is two buttons namely 'Disable Service' and 'Enable Service' in the footer section of the table.
Onclick of Disable Service i am navigating to another page where i show the application user the list of enabled services to disable
And vice-versa for Enabled service button click.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因此,假设您在托管 Bean 中拥有一个希望用户编辑的服务列表:
您将此
列表
显示在数据表中。然后,您可以实现一个
commandButton
,它具有一个action
或一个actionListener
,它指向托管 bean 的某个方法,如下所示:保存它们的相应方法非常简单。您迭代托管 bean 字段
serviceList
并保留每个条目(无论您如何保留它们,就像在使用 Hibernate 或两者之间的任何 DAO 类时调用EntityManager
一样,您可以将其命名为.)关于服务状态:我最好使用 selectBooleanCheckbox 来切换状态,因为它可能是一个布尔值。
在评论 1 后编辑:
您的
Service
类中有serviceStatus
。目前它是一个字符串,但我认为它应该是布尔值来切换活动/非活动。如果此属性由selectBooleanCheckbox
显示,那么它会在相应的 Java 类中自动更改。因此,调用getServiceStatus()
返回true
或false
,具体取决于前端中选择的内容。如果您保留整个Service
对象,则无需执行任何操作,因为在前端 HTML 元素中所做的任何修改都会自动投影到其后面的 Java 对象。So, let's say you in your Managed Bean you have a list of services you would like the user to edit:
You take this
List
to be displayed in the data table.Then you can implement a
commandButton
that has either anaction
or anactionListener
which points to a certain method of your managed bean, like this:And the corresponding method to save 'em all is quite straight-forward. You iterate over the managed bean field
serviceList
and persist every single entry (however you persist them, like calling theEntityManager
when using Hibernate or any DAO class in between, you name it.)Concerning the service status: I'd preferably use a
selectBooleanCheckbox
for toggling the status, since it's probably a boolean value.Edit after comment 1:
You have the
serviceStatus
in yourService
class. Currently it's a string, but I suppose it should be boolean to toggle active/inactive. If this property is displayed by theselectBooleanCheckbox
it is automatically changed in your corresponding Java class. So callinggetServiceStatus()
returnstrue
orfalse
, depending on what is selected in the frontend. If you persist the wholeService
object then, you don't have to do anything because any modifications made in the frontend HTML elements are automatically projected to the Java object behind it.