Introduction
Have you been at your wit’s end trying to work out why your action just won’t fire? Have you exhausted BalusC’s list of potential reasons?
Does it happen to be just the last couple of buttons that don’t work, and you’re using PrimeFaces’s dataGrid?
This happens when your row size is set up strangely, given the number of columns you’ve set your dataGrid to. The trick to remember here is that rows refers to data rows, not table rows.
There is an issue where if there are not enough rows set to evenly fill up the number of columns you’ve set the grid to, but there is enough data, then the final spots are still filled up but any actions bound to them will not fire.
This also causes the issue where the dataGrid appears to be generating duplicate items if you’re using pagination – the extra rows that are rendered out are rendered again on the next page, as that’s where they’re meant to be.
Quick Fix
The quick fix is just to set the number of rows correctly, to fill the number of columns you have. As long as the number of rows is a multiple of the number of columns (eg, 2 columns, 6 rows) you should be fine. Perhaps they’ll fix this in a future version of Primefaces to not render the extra items, but your grid will look strange in that case without the columns being filled.
Recreating this issue
Here’s a minimal recreation if anyone wants to see this issue. As an XHTML page,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<!DOCTYPE html> <html lang="en" xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html" xmlns:p="http://primefaces.org/ui"> <h:head> <meta charset="UTF-8"/> <title>Minimal datagrid example</title> </h:head> <h:body> <h:form> <p:dataGrid value="#{testBean.theStrings}" var="theString" rows="7" columns="3"> <p:commandButton action="#{testBean.printOut(theString)}" value="#{theString}"/> </p:dataGrid> </h:form> </h:body> </html> |
And as the backing bean,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
@ManagedBean() @SessionScoped public class TestBean implements Serializable { private List<String> theStrings; @PostConstruct public void setup() { System.out.println("Setting up array"); theStrings = new ArrayList<>(); for (int i = 0; i < 9; i++) { theStrings.add(String.valueOf(i)); } } public List<String> getTheStrings() { return theStrings; } public void printOut(String theString) { System.out.println(theString); } } |
You’ll see here the final two buttons do not fire an action.
You just saved me.
may god bless you 🙏
Glad my struggles have helped someone 😂