jsf2中表内的selectonemenu

发布于 2024-10-19 05:20:56 字数 1147 浏览 1 评论 0原文

我有一个包含 2 个 selectOneMenu 组件的表。

我希望一旦在第一个 selectOneMenu 组件中选择一条记录,它将使用 Ajax 更新同一行中的另一个 selectOneMenu 。

我的表:

<p:dataTable value="#{myBean.myInfo}" var="myInfo">
    <p:column>
        <f:facet name="header">Group</f:facet>
        <h:selectOneMenu value="#{myInfo.myInfoType.code}">
            <f:selectItems value="#{myBean.myList}" />
            <f:ajax event="change" execute="@this" listener="#{myBean.refershNames}" render="myNames"/>
        </h:selectOneMenu>
    </p:column>
    <p:column>
        <f:facet name="header">Name</f:facet>
        <h:selectOneMenu id="myNames" value="#{myInfo.myInfoType.secondCode}">
            <f:selectItems value="#{myBean.mySecondList}" />
        </h:selectOneMenu>
    </p:column>
<p:dataTable>

在我的 bean 中:

List<SelectItem> myList,mySecondList;

public void refershNames(AjaxBehaviorEvent event){
    //how can I retrieve the selected item and update the relevant record?
}

我怎样才能用 Ajax 做到这一点?我正在使用 JSF2

I have a table with 2 selectOneMenu components.

I would like that once a record was chosen in the first selectOneMenu component it will update the other selectOneMenu with Ajax in the same row.

My table:

<p:dataTable value="#{myBean.myInfo}" var="myInfo">
    <p:column>
        <f:facet name="header">Group</f:facet>
        <h:selectOneMenu value="#{myInfo.myInfoType.code}">
            <f:selectItems value="#{myBean.myList}" />
            <f:ajax event="change" execute="@this" listener="#{myBean.refershNames}" render="myNames"/>
        </h:selectOneMenu>
    </p:column>
    <p:column>
        <f:facet name="header">Name</f:facet>
        <h:selectOneMenu id="myNames" value="#{myInfo.myInfoType.secondCode}">
            <f:selectItems value="#{myBean.mySecondList}" />
        </h:selectOneMenu>
    </p:column>
<p:dataTable>

In the bean I have:

List<SelectItem> myList,mySecondList;

public void refershNames(AjaxBehaviorEvent event){
    //how can I retrieve the selected item and update the relevant record?
}

How can I do it with Ajax? I am using JSF2

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

您的好友蓝忘机已上羡 2024-10-26 05:20:56

将数据表值包装在 DataModel< 中/code>以便您可以通过 DataModel#getRowData()。因此,

DataModel<MyInfo> myInfoModel; // +getter

@PostConstruct
public void init() {
    myInfo = myInfoDAO.list();
    myInfoModel = new ListDataModel<MyInfo>(myInfo);
}

根据

<h:dataTable value="#{myBean.myInfoModel}" var="myInfo">

评论

public void refreshNames(AjaxBehaviorEvent event){
    MyInfo myInfo = myInfoModel.getRowData();
    // Get code and update secondCode.
}

更新,这是我在您告诉它不起作用后创建的测试用例。它对我来说适用于 Tomcat 7.0.5 和 Glassfish 3.0.1 上的 Mojarra 2.0.3。

com.example.Item

public class Item {

    private String value1;
    private String value2;

    // Generate public getters/setters.    
}

com.example.Bean

@ManagedBean
@ViewScoped
public class Bean {

    private List<Item> items;
    private DataModel<Item> model;
    private List<String> list;

    @PostConstruct
    public void init() {
        items = Arrays.asList(new Item(), new Item(), new Item());
        model = new ListDataModel<Item>(items);
        list = Arrays.asList("one", "two", "three");
    }

    public void change(AjaxBehaviorEvent e) {
        Item item = model.getRowData();
        item.setValue2(item.getValue1());
    }

    public DataModel<Item> getModel() {
        return model;
    }

    public List<String> getList() {
        return list;
    }

}

test.xhtml

<h:form>
    <h:dataTable value="#{bean.model}" var="item">
        <h:column>
            <h:selectOneMenu value="#{item.value1}">
                <f:selectItem itemLabel="select..." itemValue="#{null}" />
                <f:selectItems value="#{bean.list}" />
                <f:ajax execute="@this" listener="#{bean.change}" render="list2" />
            </h:selectOneMenu>
        </h:column>
        <h:column>
            <h:selectOneMenu id="list2" value="#{item.value2}">
                <f:selectItem itemLabel="select..." itemValue="#{null}" />
                <f:selectItems value="#{bean.list}" />
            </h:selectOneMenu>
        </h:column>
    </h:dataTable>
</h:form>

此测试用例证明,每当您更改第一列中的下拉值时,同一行第二列中的下拉值将被反映以检索相同的值。

Wrap the datatable value in DataModel<E> so that you can obtain the myInfo object in question by DataModel#getRowData(). So,

DataModel<MyInfo> myInfoModel; // +getter

@PostConstruct
public void init() {
    myInfo = myInfoDAO.list();
    myInfoModel = new ListDataModel<MyInfo>(myInfo);
}

with

<h:dataTable value="#{myBean.myInfoModel}" var="myInfo">

and

public void refreshNames(AjaxBehaviorEvent event){
    MyInfo myInfo = myInfoModel.getRowData();
    // Get code and update secondCode.
}

Update as per the comments, here is the testcase I created after you told that it didn't work. It worked for me with Mojarra 2.0.3 on both Tomcat 7.0.5 and Glassfish 3.0.1.

com.example.Item

public class Item {

    private String value1;
    private String value2;

    // Generate public getters/setters.    
}

com.example.Bean

@ManagedBean
@ViewScoped
public class Bean {

    private List<Item> items;
    private DataModel<Item> model;
    private List<String> list;

    @PostConstruct
    public void init() {
        items = Arrays.asList(new Item(), new Item(), new Item());
        model = new ListDataModel<Item>(items);
        list = Arrays.asList("one", "two", "three");
    }

    public void change(AjaxBehaviorEvent e) {
        Item item = model.getRowData();
        item.setValue2(item.getValue1());
    }

    public DataModel<Item> getModel() {
        return model;
    }

    public List<String> getList() {
        return list;
    }

}

test.xhtml

<h:form>
    <h:dataTable value="#{bean.model}" var="item">
        <h:column>
            <h:selectOneMenu value="#{item.value1}">
                <f:selectItem itemLabel="select..." itemValue="#{null}" />
                <f:selectItems value="#{bean.list}" />
                <f:ajax execute="@this" listener="#{bean.change}" render="list2" />
            </h:selectOneMenu>
        </h:column>
        <h:column>
            <h:selectOneMenu id="list2" value="#{item.value2}">
                <f:selectItem itemLabel="select..." itemValue="#{null}" />
                <f:selectItems value="#{bean.list}" />
            </h:selectOneMenu>
        </h:column>
    </h:dataTable>
</h:form>

This testcase proves that whenever you change a dropdown value in the 1st column, then the dropdown value in the 2nd column in the same row will be reflected to retrieve the same value.

魔法少女 2024-10-26 05:20:56

我遇到了同样的问题,经过大量的试验和错误,这对我有用

<p:column style="width:50%">
                    <f:facet name="header">
                        <h:outputText value="Criteria" />
                    </f:facet>
                    <h:selectOneMenu value="#{option.predefinedMessageId}"
                        valueChangeListener="#{predefinedMessageBean.predefinedMessageChangeListener}">
                        <f:selectItems
                            value="#{campaignRecipientCriteriaBean.messages}"
                            var="predef" itemLabel="#{predef.fieldName}"
                            itemValue="#{predef.predefinedMessageId}" />
                            <p:ajax event="change" update="optionsList"/>

                    </h:selectOneMenu>
                </p:column>


                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Options" />
                    </f:facet>
                    <p:outputPanel id="optionsList">
                    <h:selectOneMenu
                        value="#{option.predefinedMessageOptionId}">
                        <f:selectItems
                            value="#{predefinedMessageBean.emptyOptionBeansList}"
                            var="predef" itemLabel="#{predef.optionCaption}"
                            itemValue="#{predef.predefinedMessageOptionId}" />

                    </h:selectOneMenu>
                    </p:outputPanel>
                </p:column>

valuechangelistener 是

public void predefinedMessageChangeListener(ValueChangeEvent e)
{

        getPredefinedMessageOptions(Integer.parseInt(e.getNewValue().toString()));


}

这很简单并且对我来说非常适合,因为我在第一列中选​​择的消息更新了第二列中的选项并通过 ajax 完成。但话又说回来,我仍在学习,我对 jsf 的很多知识都来自 @BalusC 博客的材料:),所以如果这种方法有一些缺陷,请让我知道。

i had the same issue and after a lot of trial and error this worked for me

<p:column style="width:50%">
                    <f:facet name="header">
                        <h:outputText value="Criteria" />
                    </f:facet>
                    <h:selectOneMenu value="#{option.predefinedMessageId}"
                        valueChangeListener="#{predefinedMessageBean.predefinedMessageChangeListener}">
                        <f:selectItems
                            value="#{campaignRecipientCriteriaBean.messages}"
                            var="predef" itemLabel="#{predef.fieldName}"
                            itemValue="#{predef.predefinedMessageId}" />
                            <p:ajax event="change" update="optionsList"/>

                    </h:selectOneMenu>
                </p:column>


                <p:column>
                    <f:facet name="header">
                        <h:outputText value="Options" />
                    </f:facet>
                    <p:outputPanel id="optionsList">
                    <h:selectOneMenu
                        value="#{option.predefinedMessageOptionId}">
                        <f:selectItems
                            value="#{predefinedMessageBean.emptyOptionBeansList}"
                            var="predef" itemLabel="#{predef.optionCaption}"
                            itemValue="#{predef.predefinedMessageOptionId}" />

                    </h:selectOneMenu>
                    </p:outputPanel>
                </p:column>

valuechangelistener is

public void predefinedMessageChangeListener(ValueChangeEvent e)
{

        getPredefinedMessageOptions(Integer.parseInt(e.getNewValue().toString()));


}

This is simple and works perfectly for me , as the message that i choose in the first column updates the options in the second column and its done through ajax. But then again, i m still learning and alot of my knowledge of jsf is from material from @BalusC blog :), so if this approach has some flaws, please lemme know.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文