nosnhoj
nosnhoj

Reputation: 793

Primefaces dataTable with checkbox not work

I am using Primefaces 4.0 with jsf2.2.

When I use dataTable with checkbox, it won't send back any record whenever I select records.

It supposed to send back the record once I click the checkbox.

I remove extra stuff and make my code simple to test it:

There is a polling printing selected items every second, so that I can check is there something sent back.

And after doing this, I found there's nothing sent back. Here's my code:

Page:

<h:head>
    <title>System Monitor</title>

</h:head>

<h:body>
    <h:form>
        <p:poll interval="1" listener="#{indexBean.printSelect()}"/> 
    </h:form> 

    <h:form>     

        <p:dataTable id='data' var="proc" value="#{indexBean.procStatus}"
                     rowKey="#{proc.pid}" selection="#{indexBean.selectedProcs}">
            <p:column>
                <f:facet name='header'>
                    <h:outputText value='Process Name'/>
                </f:facet>
                <h:outputText styleClass="outputCell" id="pname" value='#{proc.name}'/>
            </p:column>
            <p:column selectionMode="multiple"/>
        </p:dataTable>

    </h:form>
</h:body>

Backing Bean:

@ManagedBean(name = "indexBean")
@ViewScoped
public class indexBean implements Serializable {
private ProcStatDataModel procStatus;
private SingleProcess[] selectedProcs;

@PostConstruct
public void loadProcStat() {
    List<SingleProcess> temp = new ArrayList<>();
    temp.add(new SingleProcess("test1"));
    temp.add(new SingleProcess("test2"));
    temp.add(new SingleProcess("test3"));
    procStatus = new ProcStatDataModel(temp);
}

public void printSelect() {
    if (selectedProcs != null) {
        String str = "";
        for (SingleProcess sp : selectedProcs) {
            str += sp.getName() + "_";
        }
        System.out.print(str);
    } else {
        System.out.println("selectedProcs is null");
    }
}

public ProcStatDataModel getProcStatus() {
    return procStatus;
}

public SingleProcess[] getSelectedProcs() {
    return selectedProcs;
}

public void setSelectedProcs(SingleProcess[] selectedProcs) {
    this.selectedProcs = selectedProcs;
}
}

I've tried attaching rowCheckListener like this before but in vain.

Also, I tried adding f:view contentType="text/html" like this and it was not help.

It's weird because I do the same thing with the case of primefaces show case and it acts as I think. So I think this approach is OK, there should be something wrong in my code.

Any help is appreciated. Thanks in advance.

Upvotes: 1

Views: 7780

Answers (1)

Yigitalp Ertem
Yigitalp Ertem

Reputation: 2039

I made a few changes and it worked. Not sure, which one is the solution.

1) Changed all the ' to ".

2) Datatable's value="#{indexBean.procStatus}" is wrong i think. I changed it to the name of the ArrayList inside the class. So, it became value="#{indexBean.procStatus.mylist}"

3) Added an ajax listener, like the one you mentioned in the question. <p:ajax event="rowSelectCheckbox" listener="#{indexBean.check}" />.

4) Added pid to the constructor which will be our rowKey.

5) Now, the poll prints the array.

Resultin xhtml is as follows:

<h:head>
    <title>System Monitor</title>

</h:head>

<h:body>
    <h:form>
        <p:poll interval="1" listener="#{indexBean.printSelect()}"/> 
    </h:form> 

    <h:form>     

        <p:dataTable id="data" var="proc" value="#{indexBean.procStatus.mylist}"
                     rowKey="#{proc.pid}" selection="#{indexBean.selectedProcs}">
                     <p:ajax event="rowSelectCheckbox" listener="#{indexBean.check}"   />
            <p:column>
                <f:facet name="header">
                    <h:outputText value="Process Name"/>
                </f:facet>
                <h:outputText styleClass="outputCell" id="pname" value="#{proc.name}"/>
            </p:column>
            <p:column selectionMode="multiple"/>
        </p:dataTable>

    </h:form>
</h:body>
</ui:composition>

Bean:

@ManagedBean(name = "indexBean")
@ViewScoped
public class indexBean implements Serializable {
private ProcStatDataModel procStatus;
private SingleProcess[] selectedProcs;

@PostConstruct
public void loadProcStat() {
    List<SingleProcess> temp = new ArrayList<SingleProcess>();
    temp.add(new SingleProcess("test1",1));
    temp.add(new SingleProcess("test2",2));
    temp.add(new SingleProcess("test3",3));
    procStatus = new ProcStatDataModel(temp);
}

public void printSelect() {
    if (selectedProcs != null) {
        String str = "";
        for (SingleProcess sp : selectedProcs) {
            str += sp.getName() + "_";
        }
        System.out.print(str);
    } else {
        System.out.println("selectedProcs is null");
    }
}

public ProcStatDataModel getProcStatus() {
    return procStatus;
}

public SingleProcess[] getSelectedProcs() {
    return selectedProcs;
}

public void setSelectedProcs(SingleProcess[] selectedProcs) {
    this.selectedProcs = selectedProcs;
}
public void check(SelectEvent event) {
    System.out.println("in check");
}
}

Upvotes: 0

Related Questions