JSF Primefaces SelectOneMenu

18,385

Solution 1

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.

Solution 2

Based on BalusC answer . Here is the solution.

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

It will list the car names but display the one which a person has.

Thanks all for your answer.

Solution 3

There are a couple of options.

The first is that you could change the member in your Person() class to take an instance of Car instead of an integer of carId:

public class Person {

    private String name;
    private Car car;
    }

So then your dataTable could look like this:

<p:dataTable value="#{test.persons} var="person">

                        <p:column headerText="Name"> 
                            <h:outputText value="#{person.name}" />
                            </p:column>

                        <p:column headerText="Car">
                            <h:outputText value="#{person.car.model}" />
                            </p:selectOneMenu>
                        </p:column>
                    </p:dataTable>

...assuming a Car pojo such as:

public class Car {

private String make;
private String model;
}

The second is that your could add a getCarById() method to your backing bean and use it in the DataTable.

So, in your page:

<p:dataTable value="#{test.persons} var="person">

                    <p:column headerText="Name"> 
                        <h:outputText value="#{person.name}" />
                        </p:column>

                    <p:column headerText="Car">
                        <h:outputText value="#{myBean.getCarById(person.carId).model}" />
                        </p:selectOneMenu>
                    </p:column>
                </p:dataTable>

and in your backing bean:

public Car getCarById(int carId) {
for(Car c: getAllMyCars() {
if(c.carId == carId) {
  return c;
}
return null;
}

Again, assuming a Car pojo such as:

public class Car {

private String make;
private String model;
}
Share:
18,385
Makky
Author by

Makky

Professional Software Engineer with couple of years experience in core Java and web development. Currently I am spending time on ExtJS , Mule and MyBatis. In my spare time , I play games, specially Street Fighter. I used to be an online warrior, not any more. I aint get much time.

Updated on August 07, 2022

Comments

  • Makky
    Makky over 1 year

    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.

  • Makky
    Makky over 10 years
    Thanks for your answer. But I would like to display all the available cars in the list and select the one which person currently have. I've updated the question.
  • Makky
    Makky over 10 years
    This makes sense. Thanks.
  • Makky
    Makky over 10 years
    Person doesn't have private Car car but have private Long carID.
  • BalusC
    BalusC over 10 years
    Then you'd need to change <f:selectItems itemValue> to #{car.id} and remove the converter. Your current code namely expects a Car property. Really, I'd just use a @OneToOne here with a real Car property instead of only an ID.
  • Makky
    Makky over 10 years
    Thanks BalusC. You're the best. I have managed to sort it . Thanks.