mercredi 7 juin 2017

Inject object to array in controller if checkbox is checked JSF

I am trying to create a datatable which shows dishes, and gives the possibility to select the dish. When the checkbox is marked, I want the dish to be added in a formDishes I have in my MenuBean. However, I have struggled with this for a while now and can't seem to get it to work. This is how it looks:

enter image description here

My menu bean:

@Named
@SessionScoped
public class MenuController implements Serializable
{

private Map<Dish, Boolean> checked = new HashMap<>();
private String formDate;
private List<Dish> formDishes;


@EJB
private MenuEJB menuEJB;



public String createMenu()
{
    List<Dish> checkedDishes= new ArrayList<Dish>();

    for (Dish item : formDishes) {
        if (checked.get(item)) {
            checkedDishes.add(item);
        }
    }

    try {menuEJB.createMenu(LocalDate.parse(formDate), formDishes);}
    catch (Exception e)
    {
        return "dishes.jsf";
    }
    return "home.jsf";
}

public String getFormDate() {
    return formDate;
}

public void setFormDate(String formDate) {
    this.formDate = formDate;
}

public List<Dish> getFormDishes() {
    return formDishes;
}

public void setFormDishes(List<Dish> formDishes) {
    this.formDishes = formDishes;
}

public Map<Dish, Boolean> getChecked() {
    return checked;
}

public void setChecked(Map<Dish, Boolean> checked) {
    this.checked = checked;
}

Nevermind the current state of createMenu(), as there are leftovers from a failed attempt of using Map.

My newMenu.xhtml:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://ift.tt/kkyg93">
<html xmlns="http://ift.tt/lH0Osb"
      xmlns:h="http://ift.tt/J1LPWj"
      xmlns:ui="http://ift.tt/1g9HgFb"
      xmlns:f="http://ift.tt/19DXC4H"
      xmlns:c="http://ift.tt/1kyxZva"
      xmlns:fn="http://ift.tt/1rL2RMC"
>

<ui:composition template="layout.xhtml">

    <ui:define name="title">Menu</ui:define>

    <ui:define name="content">


        <c:choose>
            <c:when test="#{dishController.dishes.size() == 0}">
                <h:outputText value="There are currently no dishes available. Please "/><h:link value="create one." outcome="dishes.xhtml" id="dishesLink"/>
            </c:when>

            <c:otherwise>

                <h:form id="createMenuForm">
                        <h:inputText value="#{menuController.formDate}" id="name"/>

                    <h:commandButton value="Create" action="#{menuController.createMenu}" id="createButton"/>

                <h:dataTable value="#{dishController.dishes}" var="dish" border="1" id="dishTable">
                    <h:column>
                        <f:facet name="header">Dish</f:facet>
                        <h:outputText value="#{dish.name}"/>
                    </h:column>

                    <h:column>
                        <f:facet name="header">Include</f:facet>
                        <h:selectBooleanCheckbox value="#{menuController.checked[dish]}"/>
                    </h:column>
                </h:dataTable>
                </h:form>
            </c:otherwise>
        </c:choose>

    </ui:define>

</ui:composition>
</html>




Aucun commentaire:

Enregistrer un commentaire