I'm new to JSF. In my case I want to display several tables (for that purpose I'm using datalist - number of tables will known at runtime) and in each datatable row I need to execute some handler after checkbox has been checked (without any submit button - it will called later).
In code below
<f:ajax listener="#{dataListTry.run(o)}" event="change" render="some_datatable_id"/>
will be called after button have been submitted, but I need to refresh after checkbox will be checked, without submitting button. If I use datatable without datalist, callback calls without any problem. How can I make callback works in datatable which is in datalist ?
Example of code:
<?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:f="http://java.sun.com/jsf/core"
xmlns:t="http://myfaces.apache.org/tomahawk"
>
<h:body>
<h:form id="some_example_form">
<t:dataList id="some_data_list"
var="dataListTry"
value="#{packageGroups.packageGroupList}" rowIndexVar="pos">
<t:div>
<t:outputLabel title="label"/>
<t:outputText value="Table_#{pos}"/>
</t:div>
<t:selectBooleanCheckbox title="External">
<f:ajax event="change" listener="#{dataListTry.run}" render=":some_example_form"/>
</t:selectBooleanCheckbox>
<t:column>
<t:dataTable id="some_datatable_id"
forceId="true"
forceIdIndex="true"
value="#{dataListTry.packageItems}"
var="o">
<t:column>
<h:outputText value="#{o.prefix}"/>
</t:column>
<t:column defaultSorted="false" sortable="false" width="28px">
<t:selectBooleanCheckbox value="#{o.selected}">
<f:ajax render="some_datatable_id"/>
<f:ajax event="change" listener="#{dataListTry.run(o)}"/>
</t:selectBooleanCheckbox>
</t:column>
</t:dataTable>
</t:column>
</t:dataList>
<br/>
<h:commandButton value="Submit" action="demo" forceId="true"/>
<h:commandButton value="MoveBack" action="demo" forceId="true" actionListener="#{packageGroups.moveBack()}"/>
</h:form>
</h:body>
</html>
And beans are: PackageItem:
@ManagedBean(name = "packageItem")
@SessionScoped
public class PackageItem {
public boolean selected;
public String prefix;
public PackageItem(String prefix) {
this.prefix = prefix;
}
public boolean isSelected() {
return selected;
}
public void setSelected(boolean selected) {
this.selected = selected;
setPrefix("selected after submit");
}
public String getPrefix() {
return prefix;
}
public void setPrefix(String prefix) {
this.prefix = prefix;
}
}
PackageGroup:
@ManagedBean(name = "packageGroup")
@SessionScoped
public class PackageGroup {
private List<PackageItem> packageItems;
private int value;
private String idGroup;
private String resultValue = "Created";
public PackageGroup(String groupId, int groupName, String value, String value2) {
this.idGroup = groupId;
this.value = groupName;
packageItems = new ArrayList<>();
packageItems.add(new PackageItem(value));
packageItems.add(new PackageItem(value2));
}
public PackageGroup() {
}
public List<PackageItem> getPackageItems() {
return packageItems;
}
public void setPackageItems(List<PackageItem> packageItems) {
this.packageItems = packageItems;
}
public void run() {
doClickAction();
}
public void run(PackageItem p) {
doClickAction();
this.resultValue = p.prefix;
}
private void doClickAction() {
packageItems.forEach(e -> e.setPrefix("selected!"));
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
}
PackageGroups:
@ManagedBean(name = "packageGroups")
@SessionScoped
public class PackageGroups {
private List<PackageGroup> packageGroupList;
public PackageGroups() {
packageGroupList = new ArrayList<>();
packageGroupList.add(new PackageGroup("100", -1, "one", "two"));
packageGroupList.add(new PackageGroup("200", -2, "three", "four"));
}
public void moveBack() {
packageGroupList.clear();
packageGroupList.add(new PackageGroup("100", -1, "one", "two"));
packageGroupList.add(new PackageGroup("200", -2, "three", "four"));
}
public List<PackageGroup> getPackageGroupList() {
return packageGroupList;
}
public void setPackageGroupList(List<PackageGroup> packageGroupList) {
this.packageGroupList = packageGroupList;
}
}
Generated html (only the view - it won't work on stackoverflow):
<?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"><body>
<form id="some_example_form" name="some_example_form" method="post" action="/JavaServerFaces_war_exploded/faces/demo.xhtml" enctype="application/x-www-form-urlencoded">
<input type="hidden" name="some_example_form" value="some_example_form" />
<div><label title="label">
</label>Table_0</div><script type="text/javascript" src="/JavaServerFaces_war_exploded/faces/javax.faces.resource/jsf.js?ln=javax.faces&stage=Development"></script><input id="some_example_form:some_data_list:0:j_idt8" type="checkbox" name="some_example_form:some_data_list:0:j_idt8" value="true" onchange="mojarra.ab('some_example_form:some_data_list:0:j_idt8',event,'change',0,'some_example_form')" title="External" />
<table id="some_datatable_id[0]">
<tbody id="some_datatable_id[0]:tbody_element">
<tr><td>one</td><td width="28px"><input id="some_datatable_id[0]:0:j_idt13" type="checkbox" name="some_datatable_id[0]:0:j_idt13" value="true" onchange="jsf.util.chain(document.getElementById('some_datatable_id[0]:0:j_idt13'), event,'mojarra.ab(\'some_datatable_id[0]:0:j_idt13\',event,\'change\',0,0)', 'mojarra.ab(\'some_datatable_id[0]:0:j_idt13\',event,\'valueChange\',0,\'some_datatable_id[0]\')'); return false;" /></td></tr>
<tr><td>two</td><td width="28px"><input id="some_datatable_id[0]:1:j_idt13" type="checkbox" name="some_datatable_id[0]:1:j_idt13" value="true" onchange="jsf.util.chain(document.getElementById('some_datatable_id[0]:1:j_idt13'), event,'mojarra.ab(\'some_datatable_id[0]:1:j_idt13\',event,\'change\',0,0)', 'mojarra.ab(\'some_datatable_id[0]:1:j_idt13\',event,\'valueChange\',0,\'some_datatable_id[0]\')'); return false;" /></td></tr>
</tbody></table>
<div><label title="label">
</label>Table_1</div><input id="some_example_form:some_data_list:1:j_idt8" type="checkbox" name="some_example_form:some_data_list:1:j_idt8" value="true" onchange="mojarra.ab('some_example_form:some_data_list:1:j_idt8',event,'change',0,'some_example_form')" title="External" />
<table id="some_datatable_id[1]">
<tbody id="some_datatable_id[1]:tbody_element">
<tr><td>three</td><td width="28px"><input id="some_datatable_id[1]:0:j_idt13" type="checkbox" name="some_datatable_id[1]:0:j_idt13" value="true" onchange="jsf.util.chain(document.getElementById('some_datatable_id[1]:0:j_idt13'), event,'mojarra.ab(\'some_datatable_id[1]:0:j_idt13\',event,\'change\',0,0)', 'mojarra.ab(\'some_datatable_id[1]:0:j_idt13\',event,\'valueChange\',0,\'some_datatable_id[1]\')'); return false;" /></td></tr>
<tr><td>four</td><td width="28px"><input id="some_datatable_id[1]:1:j_idt13" type="checkbox" name="some_datatable_id[1]:1:j_idt13" value="true" onchange="jsf.util.chain(document.getElementById('some_datatable_id[1]:1:j_idt13'), event,'mojarra.ab(\'some_datatable_id[1]:1:j_idt13\',event,\'change\',0,0)', 'mojarra.ab(\'some_datatable_id[1]:1:j_idt13\',event,\'valueChange\',0,\'some_datatable_id[1]\')'); return false;" /></td></tr>
</tbody></table>
<br /><input type="submit" name="some_example_form:j_idt15" value="Submit" /><input type="submit" name="some_example_form:j_idt16" value="MoveBack" /><input type="hidden" name="javax.faces.ViewState" id="javax.faces.ViewState" value="-6485444455087447596:-6234064892138587883" autocomplete="off" />
</form></body>
</html>
In posted html view if you check at the checkbutton under Table_% it will execute and change names of other two checkboxes (for example, if you check Table_0 one and two will change their names). But if you check checkboxes near, for example, one and two, they will change their names only after executing button submit, but I need to get them changed after I clicked on them without need to call button submit.
Finally found the solution. If forceId and forceIdIndex wouldn't set to true, then the ajax callback works.