Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding Primefaces components dynamically

I want to add Primefaces components dynamically. I'm using solution similar to this one, which was discussed there earlier:

<h:form>
    <h:panelGrid columns="2">
        <p:dataGrid id="categoriesGrid" value="#{bean.categories}"
            var="categoryBean" rowIndexVar="rowIndex">
            <p:column>
                <p:selectOneMenu id="categorySelect" effect="drop"
                    value="#{categoryBean.selectedCategory}" >
                    <f:selectItems value="#{categoryBean.availableCategories}"
                        var="category" itemLabel="#{category.name}"
                        itemValue="#{category}" />
                </p:selectOneMenu>
            </p:column>
        </p:dataGrid>
        <p:commandButton actionListener="#{bean.addNewCategory}"
            value="Add category" update="categoriesGrid"/>
    </h:panelGrid>
</h:form>

But there is problem with it. There is example of respond I get after "Add category" button was clicked:

<?xml version='1.0' encoding='UTF-8'?>
<partial-response>
<error>
    <error-name>
        class javax.faces.component.UpdateModelException
    </error-name>
    <error-message>
        <![CDATA[/createTutorial.xhtml @85,65 value=
            "#{categoryBean.selectedCategory}":java.util.NoSuchElementException]]>
    </error-message>
</error>
</partial-response>

Thanks in advance

like image 782
nikagra Avatar asked Nov 05 '22 10:11

nikagra


1 Answers

The problem was with my bean. In order to get selected item, I had to implement custom implementation of javax.faces.Converter interface. In my opinion it's quite much work to do for such simple problem (this converter must be able to have access to data source and so on). So I've made a little trick:

public class CategoryBean{

    private list<Category> availableCategories;
    private Category selectedCategory;

    public Long getCSelectedCategory(){
        // Get selected category by it's id and set selectedCategory
    }

    public void setSelectedCategory(Long selectedCategory){
        return selectedCategory.getId();
    }

    // The remaining setters and getters
}

And the corresponding piece of page code now looks like:

<p:column>
    <p:selectOneMenu id="categorySelect" effect="drop"
        value="#{categoryBean.selectedCategory}" >
        <f:selectItems value="#{categoryBean.availableCategories}"
            var="category" itemLabel="#{category.name}"
            itemValue="#{category.id}" />
    </p:selectOneMenu>
</p:column>

Please, pay your attention to the itemValue attribute and access methods that are shown.The bug I had was wrong getter return type.

Concluding, the only problem with adding new Primefaces components dynamically in my case was returning selected value. You may implement converter or use similar trick.

Is such trick a good solution in your opinion?

like image 162
nikagra Avatar answered Nov 11 '22 09:11

nikagra