在p:datatable中添加新行,然后提交内容 [英] Add a new row to a p:datatable then submit content

查看:173
本文介绍了在p:datatable中添加新行,然后提交内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图在Primefaces数据表中添加新行,然后我想提交该表的内容并执行一些业务逻辑.数据表模型是一个在ViewScoped托管Bean中维护的集合.

I'm trying to add a new row in a Primefaces datatable, then I would like to submit the content of this table and do some business logic. The datatable model is a collection that is maintained in a ViewScoped managed bean.

我正在将JSF 2.1和Primefaces 3.3一起使用.

I'm using JSF 2.1 with Primefaces 3.3.

简短示例:

    <h:form id="my-form">
        <p:dataTable value="#{testBean.list}" var="s" id="datatable">
            <p:column>
                <h:inputText value="#{s}"/>
            </p:column>
            <f:facet name="footer">
                <p:commandButton value="Add row" action="#{testBean.addRow()}" process="@form" update="@form" immediate="true" />
                <p:commandButton value="Do stuff" action="#{testBean.doSomeLogic()}" process="@form" update="@form"/>
            </f:facet>     
        </p:dataTable>
    </h:form>

受管Bean:

@ManagedBean
@ViewScoped
public class TestBean implements Serializable {

private List<String> list;

public TestBean() {
}

@PostConstruct
public void init() {
    list = new ArrayList<String>();
    list.add("one");
    list.add("two");
}

public void addRow(){
    list.add(new String());
}

public void doSomeLogic(){
    for (String string : list) {
        System.out.println(string);
    }
}

// getters and setters

}

实际发生的事情:

  • 用户单击添加行"按钮,添加新行(我需要立即输入真值,以便不进行任何验证,这些字段是较大表格的一部分).
  • 用户单击执行任务",该集合具有正确的大小(带有新行),但是未考虑用户的输入(既不修改预先退出的行,也不添加新添加的行中的新值). /li>
  • the user clicks on "add row" button, a new row is added (I need immediate to be true so no validation is done, those fields are part of a bigger form).
  • the user clicks on "do stuff", the collection has the right size (with new rows) but the user's input in not taken into account (neither modification to pre exiting rows, nor new values in freshly added rows).

我也可以提交新值吗?我只是开始使用JSF,而且不确定我已经100%掌握了它.

What can I do to submit the new values too? I'm only beginning JSF and I'm not sure I'm already 100% getting it.

感谢您的帮助.

可能的重复项:

  • Add a row to h:dataTable via AJAX with request-scoped bean without losing the row data
  • How to Dynamically add a row in a table in JSF?
  • JSF datatable: adding and removing rows clear rows values

问题由于有了Jitesh而得以解决,可以在此处找到一个有效的示例:

problem is solved thanks to Jitesh, a working example can be found here: JSF2, can I add JSF components dynamically?

推荐答案

唯一的问题是您在inputText中使用了不可变对象.要低估此情况,请查看 BaluC的答案

The only problem is you are using immutable object in inputText. To understatnd this check out BaluC's Answer

根据它的说法:由于String是不可变的对象,因此没有setter方法.它将永远无法设置输入的值."

According to it "As being an immutable object, the String doesn't have a setter method. The will never be able to set the entered value."

尝试从commandButton中删除立即属性,您会发现在插入每一行时,数据将被清除.

Try to remove immediate attribute from the commandButton you will find that on insertion of each row the data will be cleared.

这篇关于在p:datatable中添加新行,然后提交内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆