选择一个组件并显示 - Ajax

发布于 2024-11-28 10:48:58 字数 1744 浏览 0 评论 0原文

我需要输出在 selectOneMenu 列表中选择的文本。我的代码如下;

<h:selectOneMenu value="#{DataForm.stationed}" id="globalFilter" onchange="carsTable.filter()" style="width:350px;font-size:13px;"  >
    <f:selectItems value="#{DataForm.listHotel}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" />
    <h:outputText value="#{carsTable[1]}" style="width:350px"/>
</h:selectOneMenu>

我该如何编码?

编辑

此列表框位于

<h:body>
    <h:form id="form1" >
        <p:dataTable var="car" value="#{DataForm.listHotels}" widgetVar="carsTable" paginator="true" rows="10" onRowSelectComplete="carDialog.show()" emptyMessage="No hospital found with given criteria" selectionMode="single" onRowSelectUpdate="growl" style="width:1400px;font-size:13px;">

            <h:selectOneMenu value="#{DataForm.stationed}" id="globalFilter" onchange="carsTable.filter()" style="width:350px;font-size:13px;"  >
                <f:selectItems value="#{DataForm.listHotel}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" />
                <h:outputText value="#{carsTable[1]}" style="width:350px"/>
            </h:selectOneMenu>

        </p:dataTable>

        <f:ajax render= "@form1" >
            <h:selectOneMenu value="#{DataForm.stationed}">
                <f:selectItems value="#{DataForm.listHotels}" var="item" itemValue="#{DataForm.listHotels}" itemLabel="#{DataForm.listHotels}" />
            </h:selectOneMenu>
        </f:ajax>
    </h:form>
</h:body>

I need to output the text that is selected in the selectOneMenu-list. My code is as follows;

<h:selectOneMenu value="#{DataForm.stationed}" id="globalFilter" onchange="carsTable.filter()" style="width:350px;font-size:13px;"  >
    <f:selectItems value="#{DataForm.listHotel}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" />
    <h:outputText value="#{carsTable[1]}" style="width:350px"/>
</h:selectOneMenu>

How do I code this?

EDIT

This Listbox is within a <datatable>

<h:body>
    <h:form id="form1" >
        <p:dataTable var="car" value="#{DataForm.listHotels}" widgetVar="carsTable" paginator="true" rows="10" onRowSelectComplete="carDialog.show()" emptyMessage="No hospital found with given criteria" selectionMode="single" onRowSelectUpdate="growl" style="width:1400px;font-size:13px;">

            <h:selectOneMenu value="#{DataForm.stationed}" id="globalFilter" onchange="carsTable.filter()" style="width:350px;font-size:13px;"  >
                <f:selectItems value="#{DataForm.listHotel}" var="user" itemValue="#{user[1]}" itemDisabled="false" itemLabel="#{user[1]}" />
                <h:outputText value="#{carsTable[1]}" style="width:350px"/>
            </h:selectOneMenu>

        </p:dataTable>

        <f:ajax render= "@form1" >
            <h:selectOneMenu value="#{DataForm.stationed}">
                <f:selectItems value="#{DataForm.listHotels}" var="item" itemValue="#{DataForm.listHotels}" itemLabel="#{DataForm.listHotels}" />
            </h:selectOneMenu>
        </f:ajax>
    </h:form>
</h:body>

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

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

发布评论

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

评论(1

死开点丶别碍眼 2024-12-05 10:48:59

我认为您应该首先删除嵌套的 ,它在该位置实际上没有意义。另外,从您的问题和示例代码来看,并不清楚 PrimeFaces 与它有什么关系。

要输出所选值,只需打印表达式#{DataForm.stationed},因为这是将接收所选值的绑定。

以下示例通过 AJAX 演示了这一点,但如果您使用操作和常规表单提交,它也会以相同的方式工作。

Facelet

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
    <h:head />

    <h:body>

        <h:form id="form">

            <f:ajax render="form">
                <h:selectOneMenu value="#{selectOneMenuBean.value}">
                    <f:selectItems value="#{selectOneMenuBean.items}" var="item" itemValue="#{item}" itemLabel="#{item}" />
                </h:selectOneMenu>
            </f:ajax>

            Selected value: #{selectOneMenuBean.value}

        </h:form>

    </h:body>
</html>

Bean

@ViewScoped
@ManagedBean
public class SelectOneMenuBean {

    private List<String> items = Arrays.asList("a", "b", "c");
    private String value;

    public List<String> getItems() {
        return items;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

ps
在您的代码示例中,bean 的 EL 名称以大写字母开头。这有点违反典型惯例。另外,您可能需要注意您的名称和类型。该 bean 返回名为 listHotel 的内容,但它被分配给名为 user 的变量,然后通过整数进行索引。我建议对齐名称(例如集合名称=users,变量名称user)并使用属性而不是索引(例如user.name)。

I think you should remove the nested <h:outputText/> first, it doesn't really make sense at that position. Also, from your question and example code, it doesn't really become clear what PrimeFaces has to do with it.

To output the selected value, simply print the expression #{DataForm.stationed}, since this is the binding that will receive the selected value.

The following example demonstrates this via AJAX, but it would work the same way if you used an action and a regular form submit.

Facelet

<html xmlns="http://www.w3.org/1999/xhtml"
    xmlns:h="http://java.sun.com/jsf/html"
    xmlns:f="http://java.sun.com/jsf/core">
    <h:head />

    <h:body>

        <h:form id="form">

            <f:ajax render="form">
                <h:selectOneMenu value="#{selectOneMenuBean.value}">
                    <f:selectItems value="#{selectOneMenuBean.items}" var="item" itemValue="#{item}" itemLabel="#{item}" />
                </h:selectOneMenu>
            </f:ajax>

            Selected value: #{selectOneMenuBean.value}

        </h:form>

    </h:body>
</html>

Bean

@ViewScoped
@ManagedBean
public class SelectOneMenuBean {

    private List<String> items = Arrays.asList("a", "b", "c");
    private String value;

    public List<String> getItems() {
        return items;
    }

    public String getValue() {
        return value;
    }

    public void setValue(String value) {
        this.value = value;
    }

}

p.s.
In your code example the EL names for your beans start with a capital. This is a little against the typical conventions. Also, you might want to pay some attention to your names and types. The bean returns something called listHotel, but this is assigned to a variable named user, which is then indexed by an integer. I would recommend aligning names (e.g. collection name = users, variable name user) and using properties instead of indexes (e.g. user.name).

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