JSF Primefaces SelectOneMenu [英] JSF Primefaces SelectOneMenu

查看:149
本文介绍了JSF Primefaces SelectOneMenu的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

人员只能有一辆汽车,但是在数据表中,我想显示列表中的所有汽车,但选择一个用户所属的汽车。这样,用户可以快速更新任何人的车。



让我们说两个



个人

  id 
名称
car_id

汽车

  id 
name

理想情况下,个人应该有汽车 id为 primary 键,但事实并非如此。所以每个人都有车,对了。



现在我在datatable中显示 person 的列表,例如

  ------------------------------ ------ 
名称| Car
----------------------------------------
ABC | 1
DDD | 2

但是我想显示如下:

  ------------------------------------ 
名称| Car
----------------------------------------
ABC |丰田
DDD |福特

现有代码:

 < p:dataTable value =#{test.persons} var =person> 
< p:column headerText =Name >
#{person.name}
< / p:column>
< p:column headerText =Name>
#{person.carID}
< / p:column>
< / p:dataTable>

但是我想做一些例子:

 < p:dataTable value =#{test .persons} var =person> 
< p:column headerText =Name>
#{person.name}
< / p:column>
< p:column headerText =Car>
< p:selectOneMenu value =#{test.selectedCar}
converter =entityConverter>
< f:selectItems value =#{spMBean.cars}var =car
itemLabel =#{car.name}itemValue =#{car}/>
< / p:selectOneMenu>
< / p:column>
< / p:dataTable>

如果有人可以帮助我,我非常高兴。

解决方案

您想将所选车辆与个人相关联。



但是,您将下拉列表绑定到通用的支持bean属性,而不是对该invidivual的人。同一数据表中所有这些行中的所有下拉列表现在指向一个并且相同的后台bean属性。提交后,每一行的选定值将相互覆盖,直到支持bean属性最后一行的选定值为止。



这不会感。您需要将下拉式值绑定到个人。

 < p:selectOneMenu value =#{person.car} > 

如果 Person entity has a 私家车车厢属性,而不是一个私人长carID 。您当然可以将可用项目保存在单独的bean中。


Person can have only one car , but in the datatable I want to display all the cars in the list but select the one user person belongs to. This way user can update any person's car on the fly.

Let say I have two tables

Person

id 
name 
car_id

Cars

id
name

Ideally , person should have Cars id as primary key but that is not the case. So each person has car ,right.

Now I am displaying list of person in datatable e.g.

------------------------------------
Name | Car 
----------------------------------------
ABC | 1
DDD | 2

But I want to show like :

------------------------------------
Name | Car 
----------------------------------------
ABC | Toyota
DDD | Ford

The existing code :

<p:dataTable value="#{test.persons} var="person">
    <p:column headerText="Name"> 
        #{person.name}
    </p:column>
    <p:column headerText="Name"> 
        #{person.carID}
    </p:column>
</p:dataTable>

But I want to do something like:

<p:dataTable value="#{test.persons} var="person">
    <p:column headerText="Name"> 
        #{person.name}
    </p:column>
    <p:column headerText="Car">
        <p:selectOneMenu value="#{test.selectedCar}"
            converter="entityConverter">
            <f:selectItems value="#{spMBean.cars}" var="car" 
                itemLabel="#{car.name}" itemValue="#{car}" />
        </p:selectOneMenu>
    </p:column>
</p:dataTable>

If someone can help me with this, I'll highly appreciate that.

解决方案

You want to associate the selected car with the individual person.

However, you're binding the dropdown value to a generic backing bean property instead of to the invidivual person. All those dropdowns in all those rows in the same data table now point to one and same backing bean property. Upon submitting, the selected value of every single row will override each other until the backing bean property ends up with the selected value of the last row.

This doesn't make sense. You need to bind the dropdown value to the individual person.

<p:selectOneMenu value="#{person.car}">

This is easiest if Person entity has a private Car car property instead of a private Long carID. You can of course keep the available items in a separate bean.

这篇关于JSF Primefaces SelectOneMenu的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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