Sébastien Tromp
Sébastien Tromp

Reputation: 612

JSF - p:dataTable - p:commandButton doesn't work even inside p:column

I have a commandButton inside a DataTable. However the "action" isn't called when I click on it (same effect with an actionListener); I added logs at the beginning of the server action, and it never shows. Please note that the button works fine when outside from the datalist.

Here is my code:

<h:form id="compSearchForm">
    <p:dataTable var="competency" value="#{competencySearchBean.matchingCompetencies.toArray()}">
        <p:column>
            <f:facet name="header">
                <h:outputLabel value="Title" />
            </f:facet>
            <h:outputText value="#{competency.title}" />
        </p:column>

        <p:column>
            <p:commandButton value="Go" id="actionButton" action="#{myBean.doAction}" />
        </p:column>
    </p:dataTable>
</h:form>

Would you have any idea as to what the issue might be?

Upvotes: 1

Views: 8685

Answers (4)

David
David

Reputation: 1

I had the same problem and after trying different ways, I resolved it by initializing my List (the list that I use in the p:datatable) in the init method of the Bean.

With this form, the list goes to the page with some values.

Even when I put List<Paciente> myList = new ArrayList<Paciente>(); it didn't work. I have to put some values in the list. That was the only way that resolved the problem.

Upvotes: 0

user1073494
user1073494

Reputation:

see this example - http://www.primefaces.org/showcase/ui/datatableRowSelectionByColumn.jsf

I think you must prefix the id with the form ID

see how it updates ":form:display" instead of only "display"

worked for me

Upvotes: 0

Sean
Sean

Reputation: 981

I added your button column code to an existing of mine along with a dummy method in by backing bean. The method is called correctly when I press the button it works correctly. Here is the code I added to my dataTable:

<p:column>
    <p:commandButton value="Go" id="actionButton" action="#{tableBean.buttonAction()}" />
</p:column>

Here is the buttonAction method in my backing bean:

public void buttonAction()
    {
        int a = 0;
        for(int i = 0; i < 100; i++)
            a = i;
    }

I put a breakpoint on this method, so I know it is getting called when I click the button. I went a step further and used your datatable code (with my data source) and the method is still being called when I press the button. I don't know what development environment you are using, but if it has a debug mode put a breakpoint on your doAction method, and a few other strategic locations in your bean, then run your project in debug mode to see what is going on.

Upvotes: 1

Shervin Asgari
Shervin Asgari

Reputation: 24499

I think you have to change to:

<h:form id="compSearchForm">
<p:dataTable var="competency" value="#{competencySearchBean.matchingCompetencies.toArray()}">
    <p:column>
        <f:facet name="header">
            <h:outputLabel value="Title" />
        </f:facet>
        <h:outputText value="#{competency.title}" />
    </p:column>
</p:dataTable>
<p:commandButton value="Go" id="actionButton" action="#{myBean.doAction}" />
</h:form>

I don't think you can have the button inside the dataTable. Try this.

Upvotes: 0

Related Questions