0

フォームに動的入力フィールドを追加できます。

                <h:form>
                    <h:dataTable id="tblFields" value="#{templateOptionBean.fields}"
                        var="field">
                        <h:column>
                            <h:inputText value="#{field.value}" />
                        </h:column>

                        <h:column>
                            <h:commandButton value="Remove">
                                <f:ajax
                                    listener="#{templateOptionBean.onButtonRemoveFieldClick(field)}"
                                    immediate="true" render="@form" />
                            </h:commandButton>
                        </h:column>
                    </h:dataTable>

                    <h:commandButton value="Add">
                        <f:ajax listener="#{templateOptionBean.onButtonAddFieldClick}"
                            execute="@form" render="tblFields" />
                    </h:commandButton>
                </h:form>

私のバックエンドは次のようになります:

    @ManagedBean
@ViewScoped
public class TemplateOptions implements Serializable
{
    private List<Field> m_lFields;

    public TemplateOptions()
    {
        m_lFields = new ArrayList();

        m_lFields.add(new Field());
    }

    public void setFields(List<Field> p_lFields)
    {
        m_lFields = p_lFields;
    }

    public List<Field> getFields()
    {
        return m_lFields;
    }

    public void onButtonRemoveFieldClick(final Field p_oField)
    {
        m_lFields.remove(p_oField);
    }

    public void onButtonAddFieldClick(AjaxBehaviorEvent p_oEvent)
    {
        m_lFields.add(new Field());
    }
}

ただし、フォームのボタンを変更するとp:commandButton、ロジックが機能しなくなります。

ただし、これらのタイプのボタンを使用したいです。したがって、このタイプのボタンをどのように使用できますか?

あなたの答えに本当に感謝します!!!

4

1 に答える 1

0

Primefaces のような他のコンポーネント ライブラリを使用する場合は、コンポーネントを他のものとは異なるものにすることができます。したがって、commandbutton の primefaces ショーケースを見ると、Bean で Ajax 呼び出しを簡単に行う方法がわかります ( http://www.primefaces.org/showcase/ui/pprPartialTree.jsf ) 。あなたのBeanとあなたのページです。次のページから始めましょう。

<p:commandButton value="Ajax Submit" update="tblFields" id="ajax"  
         actionListener="#{templateOptionBean.onButtonAddFieldClick}" />

Primefaces を使用すると、コマンド ボタンで直接使用できる f:ajax は必要ありません。

Bean で onButtonAddFieldClick メソッドを変更します。Primefaces は javax.faces.event.ActionEvent を起動しています (ショーケースを参照) したがって、次のように変更します。

public void onButtonAddFieldClick(ActionEvent  p_oEvent)
{
    m_lFields.add(new Field());
}

そして、それは p:commandButton で動作します

于 2013-06-06T12:39:12.543 に答える