Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Primefaces Datatable doesnt update selected rows

Tags:

jsf

primefaces

I have a simple problem. I have a Primefaces Datatable. When the user clicks on a row, I would like the selected rows property in the backing bean to be updated. This can be achieved if the form that the Datatable is in is submitted, but I would like it to happen asynchronously. Ive read the various questions on here about this question, but still have not been able to find a solution.

Here is a small example to demonstrate the issue :

Test JSF Page:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
  xmlns:h="http://java.sun.com/jsf/html"
  xmlns:p="http://primefaces.org/ui">
<h:head>
    <title>Facelet Title</title>
</h:head>
<h:body>
    <p:dataTable var="v" value="#{test.values}" selectionMode="multiple"
                 selection="#{test.selectedValue}" rowKey="#{v.value}" >
        <p:column headerText="Test">
            <h:outputText value="#{v.value}" />
        </p:column>
    </p:dataTable>
</h:body>

Backing Bean:

import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.component.menuitem.MenuItem;
import org.primefaces.component.stack.Stack;

@ManagedBean
@ViewScoped
public class Test 
{

    private Value[] selectedValues;

    public List<Value> getValues()
    {
        List<Value> retVal = new ArrayList<Value>();
        retVal.add(new Value("a"));
        retVal.add(new Value("b"));
        return retVal;
    }

    public Value[] getSelectedValues() {
        return selectedValues;
    }

    public void setSelectedValues(Value[] selectedValues) {
        this.selectedValues = selectedValues;
    }
}

And a simple POJO that they use:

public class Value {

    private String value;
    public Value(String value)
    {
        this.value = value;
    }

    public String getValue()
    {
        return value;
    }

    public void setValue(String value)
    {
        this.value = value;
    }
}

As per the responses, I have update the Datatable like so:

    <p:dataTable id="dt" var="v" value="#{test.values}" selectionMode="multiple"
                 selection="#{test.selectedValues}" rowKey="#{v.value}" >
        <p:column headerText="Test">
            <h:outputText value="#{v.value}" />
        </p:column>
        <p:ajax event="rowSelect"/>
        <p:ajax event="rowUnselect" />
    </p:dataTable>

This however still fails to call the setter setSelectedValues(); I made them also say:

    <p:ajax event="rowSelect" update="@this" />
    <p:ajax event="rowUnselect" update="@this" />

And this only called the getter when a row was clicked. Any ideas?

like image 276
user489041 Avatar asked Aug 26 '26 07:08

user489041


1 Answers

You can use the rowSelect event for it in <p:ajax>.

<p:dataTable ...>
    ...
    <p:ajax event="rowSelect" />
</p:dataTable>

This makes however very little sense in this context. It'd have made more sense if you'd like to hook a listener or an update attribute on that as well. E.g.

<p:dataTable ...>
    ...
    <p:ajax event="rowSelect" update="menu" />
</p:dataTable>

If you'd like to hook on unselecting of rows as well, add another one which hooks on rowUnselect:

<p:dataTable ...>
    ...
    <p:ajax event="rowSelect" />
    <p:ajax event="rowUnselect" />
</p:dataTable>

See also:

  • PrimeFaces User's Guide - You can find all available ajax behavior events of <p:dataTable> in page 145
like image 54
BalusC Avatar answered Aug 31 '26 15:08

BalusC



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!