Jsf datatable row selection problem

datatablejsfjsf-2primefaces

I have a problem with selecting rows in the Primefaces Datatable. I use dynamic columns, so the standard row selection mechanism is not usable here, I implement checkbox selection myself.

To help, here's s simplified version of what I have in my xhtml:

<h:form>
  <p:dataTable id="table"
               var="result"
               value="#{tableBean.results}">

    <p:columns value="#{tableBean.columnNames}" var="column" columnIndexVar="colIndex">
      <f:facet name="header">
        #{column}
      </f:facet>

      <h:panelGroup rendered="#{colIndex==0}">
        <h:outputLabel>#{rowIndex}</h:outputLabel>
        <h:selectBooleanCheckbox value="#{tableBean.selectedRows[result[0]]}"/>
      </h:panelGroup>
    </p:columns>
  </p:dataTable>
  <h:commandButton value="Submit"></h:commandButton>
</h:form>

And here's what I have in the managed bean to select the checkboxes:

package testpackage;

import java.util.*;
import javax.faces.bean.*;

@ManagedBean
@SessionScoped
public class TableBean
{

  private Map<String, Boolean> selectedRows = new HashMap<String, Boolean>();
  List<List<String>> results = new LinkedList<List<String>>();

  public TableBean()
  {
    List<String> row1 = new LinkedList<String>();
    List<String> row2 = new LinkedList<String>();
    row1.add("row1.ref");
    row1.add("row1.id");
    row1.add("row1.status");
    row2.add("row2.ref");
    row2.add("row2.id");
    row2.add("row2.status");
    results.add(row1);
    results.add(row2);

    //selectedRows.put("row2.ref", true);
  }

  public Map<String, Boolean> getSelectedRows()
  {
    return selectedRows;
  }

  public String submit()
  {
    List<List<String>> selectedResults = new ArrayList<List<String>>();
    for (List<String> result : results)
    {
      if (selectedRows.get(result.get(0)) != null)
      {
        selectedResults.add(result);
        selectedRows.remove(result.get(0));
      }
    }

    return null;
  }

  public List<List<String>> getResults()
  {
    return results;
  }

  public List<String> getColumnNames()
  {
    List<String> columnNames = new LinkedList<String>();
    columnNames.add("");
    columnNames.add("REF");
    columnNames.add("ID");
    columnNames.add("STATUS");
    return columnNames;
  }
}

The getSelectedRows method works great, but the problem is that the setSelectedRows method is never called, so I don't know which checkboxes the user has selected. Maybe I overlook something very trivial, but cannot find the solution.

Any ideas on this? I would be very glad if you helped, or give any other row selection solution for the dynamic columns.

Thx in advance,
Levi

Best Answer

To me it looks you are rendering the wrong field in selectBooleanCheckBox.
You should be using variable or field from the result variable.
My solution:
In your situation you are rendering an object from List as a form of table row so if you want to make some changes and retrieve the status of that row then you should be using the variable from that object only.

I understand you are submitting the whole form and want to pickup all updated rows, in that case you will have to loop through the whole List and find all the rows which have been updated by checking the status in Request Handler(Action) bean.

Hope that helps.

Related Topic