通过AJAX的dataTable与请求范围的bean,而不会丢失的数据行:添加行到h范围、数据、AJAX、dataTable

2023-09-10 21:40:33 作者:飘浮

我觉得冠军会告诉一切:我想一个行添加到dataTable的阿贾克斯。该表具有输入字段中,用户可以将文本。就这么简单,一个pretty的常见情况。

I think the title tells it all: I want to add a row to dataTable over AJAX. The datatable has input fields where user can put text. As simple as that, a pretty common case.

使用会话范围的bean没有这个问题,因为同一个bean被更新了一遍又一遍。不过,我想这样做的请求范围。对于每一个请求,我想我要创建一个新的bean,并从我的表单中的值来填充它。然后我想我的的commandButton 动作来添加一个新行,终于使的dataTable 在AJAX和往常一样。

With session-scoped bean this no issue, as the same bean is updated over and over again. However, I want to do this in request scope. For each request, I guess I want to create a new bean and to populate it with the values from my form. Then I want my commandButton's action to add a new row, finally render the dataTable over AJAX as usual.

问题是我不知道如何使JSF填充新创建的请求豆从dataTable组件的当前数据?

The issue is that I don't know how to make JSF fill the newly-created request-bean with the current data from the dataTable component?

有类似的问题问及answered.但是,解决方案似乎重新加载DataTable的每一一次被刷新的内容,并手动插入为新插入的行类似这样的空元素:

There was a similar question asked and answered. However, that solution seems to reload the contents of the dataTable each time it is refreshed and manually inserts empty elements for the newly-inserted rows like this:

// Preserve list with newly added items.
ror (int i = 0; i < (Integer) count.getValue(); i++) {
    list.add(new Item());
}

要我,似乎,这种做法会擦用户确实的行可能发生的变化(新老)......如果他不先救他们。

To me, it seems that this approach also wipes the possible changes that the user did to rows (new and old)... if he doesn't first save them.

任何指针?

推荐答案

您真的应该考虑使用新的JSF 2.0 查看范围。这在于请求和会话范围之间。只要这个范围,生活是你的交互(提交并导航到)相同的看法。这是为特定的功能要求一个精确的西服

You should really consider using the new JSF 2.0 view scope. This lies between the request and session scope in. This scope lives as long as you're interacting (submitting and navigating to) the same view. This is an exact suit for the particular functional requirement.

CRUD 2.0 福利和的陷阱@ViewScoped