JSF: Add AjaxBehaviour to dynamically generated HTMLInputText - ajax

I want to create UIComponents with an AjaxBehaviour dynamically and add it to a HTMLPanelGrid. The ajax method will be called but there was no value binding before and no rendering after that. Please help me! Thank you so much! Here is some code:
The Ajax function:
public void ajax(AjaxBehaviorEvent event) {
HtmlPanelGrid grid = (HtmlPanelGrid) event.getComponent().getParent();
HashMap<String, Object> params;
try {
ajaxSet(grid);
params = collect();
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException(e);
}
SearchComponent search = (SearchComponent) ComponentRegistry.instance()
.getComponent(IComponentConstants.SEARCH);
this.score = Integer.toString(search.getSystemsCount(params));
}
The function for creating a HTMLInputText:
public HtmlInputText inputText(String id, String expression,
UIComponent parent, AjaxBehaviorListener listener) {
HtmlInputText text = new HtmlInputText();
text.setId(id);
if (!expression.isEmpty()) {
text.setValueExpression("value", createValueExpression(expression));
}
AjaxBehavior ajax = (AjaxBehavior) FacesContext.getCurrentInstance()
.getApplication().createBehavior(AjaxBehavior.BEHAVIOR_ID);
ajax.setExecute(Arrays.asList(new String[] { "#form" }));
ajax.setRender(Arrays.asList(new String[] { "#all" }));
ajax.addAjaxBehaviorListener(listener);
text.addClientBehavior("valueChange", ajax);
add(text, parent);
return text;
}
The function for creating the fields depending on a list with the desired field informations and the listener:
public void createSearchFields(UIComponent parent) {
String beanname = Utilities.lookupManagedBeanName(this);
GUIHelper helper = GUIHelper.instance();
for (Searchfield searchfield : shown) {
helper.label(searchfield.id, searchfield.displayname, parent);
for (Field f : searchdto.getClass().getDeclaredFields()) {
GUISearchField gsf = f.getAnnotation(GUISearchField.class);
if (gsf == null)
continue;
if (gsf.id().equals(searchfield.id)) {
String expression = "#{" + beanname + ".searchdto."
+ f.getName() + "}";
helper.inputText(searchfield.id, expression, parent, this);
}
}
}
}
#Override
public void processAjaxBehavior(AjaxBehaviorEvent event)
throws AbortProcessingException {
ajax(event);
}
public abstract void ajax(AjaxBehaviorEvent event);
The xhtml part:
<h:form id="searchform" styleClass="ibm-column-form ibm-styled-form">
<h:panelGrid columns="2" binding="#{systemsearchable.searchfields}">
</h:panelGrid>
<h:commandButton value="Search" action="#{systemsearchable.search}">
<f:ajax execute="#form" render="results"></f:ajax>
</h:commandButton>
</h:form>
If you need more info please let me know :)

Related

Primefaces Nested DataTable with InputText Not Updating Bean

I've a dataTable within a dataTable. The initial dataTable has a list of components that appear on the page. One of these components will be a list of strings which can have elements added or deleted. When I change a string in this list, I expect the value to show up in the bean and it is not.
Below I have an example of my problem. The page renders a text input field for the first component then three text input fields to represent the second component which is a list of three input fields.
I have valueChange listener on all the input fields. The listener, is in the InnerBean class, prints out the source and the value that changed.
For the standalone input field, the listener correctly prints out the changed value and shows that the bean has been updated with this value. For any of the input fields from the list, the listener prints out the previous value of the input field and the bean has not been updated. On the ajax update of the inner datatable, the changed value is replace with the original value.
Since the valueChange listener is called, it appears that Primefaces knows that the value has changed. The code just doesn't seem to record the changed value.
Any help is appreciated.
I'm using Primefaces 8.0 and JSF 2.2.20.
Here is the xhtml:
<p:panel id="testPanel" header="#{myController.outerBean.name}" toggleable="true" collapsed="false" >
<p:dataTable id="testTable" value="#{myController.outerBean.innerBeanList}" var="bean">
<p:column >
<!-- TEXT COMPONENT-->
<h:panelGroup rendered="#{bean.type eq 'text'}" >
<p:inputText id="textfield" value="#{bean.value}" style="width:100%;" >
<p:ajax event="valueChange" listener="#{bean.textListListener}" update="testTable" />
</p:inputText>
</h:panelGroup>
<!-- LIST COMPONENT -->
<h:panelGroup rendered="#{bean.type eq 'textlist'}" >
<p:dataTable id="testListTable" styleClass="datatableWithoutBorder" style="width:320px"
var="textAddition" value="#{bean.list}" rowIndexVar="rowIndex" >
<p:column >
<p:inputText id="textAdd" value="#{textAddition}" style="width: 100%;">
<p:ajax event="valueChange" listener="#{bean.textListListener}" update="testListTable"/>
</p:inputText>
</p:column>
</p:dataTable>
</h:panelGroup>
</p:column>
</p:dataTable>
<h:panelGrid columns="1" style="width:100%;">
<h:panelGroup style="float:right">
<p:commandButton id="submitBtn" value="Submit"
action="#{dummyController.submit}"
update="messages #this"
icon="fa fa-save"/>
</h:panelGroup>
</h:panelGrid>
</p:panel>
My controller code:
public class MyController {
private OuterBean outerBean;
public MyController() {
System.out.println("MyController instantiated");
setOuterBean(new OuterBean());
}
public void submit() {
for (InnerBean ab: outerBean.getInnerBeanList()) {
System.out.println(ab.getLabel() + ": " + ab.getValue() + ":" + ab.getList() );
}
}
public void clear() {
// TODO
}
// Getter/Setter methods
public OuterBean getOuterBean() {
return outerBean;
}
public void setOuterBean(OuterBean outerBean) {
this.outerBean = outerBean;
}
}
My OuterBean with the list of components:
public class OuterBean implements Serializable {
private String name;
private String value;
private List<InnerBean> innerBeanList;
public OuterBean() {
name = "Entry Panel #1";
value = "";
innerBeanList = new ArrayList<InnerBean>();
InnerBean ab1 = new InnerBean();
ab1.setLabel("First Component");
ab1.setType("text");
ab1.setValue("Input text");
innerBeanList.add(ab1);
InnerBean ab2 = new InnerBean();
ab2.setLabel("Second Component");
ab2.setType("textlist");
ArrayList<String> list = new ArrayList<String>();
list.add("Item 1");
list.add("Item 2");
list.add("Item 3");
ab2.setList(list);
innerBeanList.add(ab2);
}
//
// Getter/Setters
//
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<InnerBean> getInnerBeanList() {
return innerBeanList;
}
public void setInnerBeanList(List<InnerBean> innerBeanList) {
this.innerBeanList = innerBeanList;
}
}
My InnerBean which represents a component to be render. One of which can be a list of strings:
public class InnerBean implements Serializable {
// Type of component
public static final String TEXT = "text";
public static final String TEXTLIST = "textlist";
private String label;
private String type; // If TEXT, use value; if TEXTLIST, use list.
private String value;
private List<String> list = new ArrayList<String>();
public InnerBean() {
}
public void textListListener(AjaxBehaviorEvent event) {
System.out.println("Listener called");
System.out.println(" Source: " + event.getSource().toString());
System.out.println(" Value: " + ((UIInput)event.getSource()).getValue());
System.out.println(" List: " + list.toString());
System.out.println(" Event: " + event.toString());
}
//
// Setters and getters
//
public String getLabel() {
return label;
}
public void setLabel(String label) {
this.label = label;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public List<String> getList() {
return list;
}
public void setList(List<String> list) {
this.list = list;
}
}

Ajax not work on primefaces selectManyCheckbox with converter

I'm trying to use primefaces selectManyCheckBox with ajax and converter, but the ajax not fired. If I'm didn't use converter, the ajax can fired. Is there something wrong with my converter?
<div class="form-group">
<p:outputLabel value="Atur Grade Pinjaman" for="gradePinjaman"/>
<p:selectManyCheckbox id="gradePinjaman" value="#{autoInvestController.param.grades}" converter="companyGradeConverter">
<f:selectItems value="#{autoInvestController.grades}" var="grade" itemLabel="#{grade.id}" itemValue="#{grade}"/>
<p:ajax update="selectAll estimation" listener="#{autoInvestController.valueChange}"/>
</p:selectManyCheckbox>
<p:message for="gradePinjaman"/>
</div>
Here is my backing bean code
public void valueChange() {
if (param.getGrades() != null && !param.getGrades().isEmpty()) {
checkAll = param.getGrades().size() == grades.size();
calculateCollectEstimation();
}
}
Here is my converter
#Override
public Object getAsObject(FacesContext context, UIComponent component, String value) {
if (Strings.isNullOrEmpty(value)) {
return null;
} else {
try {
PlatformService platformService = (PlatformService) CDI.current().select(PlatformService.class).get();
Map<String, Object> param = new HashMap<>();
param.put("id", value);
CompanyGradeResponse companyGrade = platformService.getCompanyGrade(param).get(0);
return companyGrade;
} catch (EndpointException e) {
LOG.error(e.getMessage(), e);
return null;
}
}
}
#Override
public String getAsString(FacesContext context, UIComponent component, Object value) {
if (value != null) {
return ((CompanyGradeResponse) value).getId();
} else {
return null;
}
}
I think my converter work well, but why the ajax won't fired when i check the checkbox?
Thanks

Ajax Call get null value in backing bean in jsf

I have two SelectoneMenu and one inputText. within inputText I called one Ajax event(blur). But this ajax method does not get selected value from SelectoneMenu. But I called the same method using CommandButton it will get selected values and worked fine.
Here is My Code:
<h:form id=HarvestRateForm>
<table width="670px;">
<tr>
<td width="100px;"><p:outputLabel value="Crushing Season" for="crushingSeason"/></td>
<td width="80px;">
<p:selectOneMenu id="crushingSeason" style="width: 110px;" value="#{harvestRateBean.selectedSeason}">
<f:selectItem itemValue="#{null}" itemLabel="Select"/>
<f:selectItems value="#{harvestRateBean.seasons}"/>
</p:selectOneMenu><p:growl for="crushingSeason"/>
</td>
<td width="60px;" align="right"><p:outputLabel value="Plant" for="plant"/></td>
<td width="80px;"><p:selectOneMenu id="plant" style="width: 85px;" value="#{harvestRateBean.selectedPlant}">
<f:selectItem itemValue="#{null}" itemLabel="Select"/>
<f:selectItems value="#{harvestRateBean.plants}"/>
</p:selectOneMenu><p:growl for="plant"/></td>
<td width="60px;" align="right"><p:outputLabel value="Plot No" for="plotNo"/></td>
<td><p:inputText id="plotNo" value="#{harvestRateBean.sapPlotNo}" size="16">
<p:ajax event="blur" listener="#{harvestRateBean.loadPlotDetails}"
update="HarvestRateForm:plotNo HarvestRateForm:ryotCode"/>
</p:inputText>
<p:growl for="plotNo"/>
</td>
</tr>
<tr>
<td><p:outputLabel value="Ryot No" for="ryotCode"/></td>
<td><p:inputText value="#{harvestRateBean.ryotNo}" size="15" id="ryotCode" readonly="true" style="background: transparent;background-color: #cccccc"/></td>
My Bean Class:
public class HarvestRateBean implements Serializable {
private Map<String, String> seasons;
private Map<String, String> plants;
private String selectedPlant;
private String selectedSeason;
private String sapPlotNo;
private String ryotNo;
public HarvestRateBean() {
seasons = new HashMap<String, String>();
plants = new HashMap<String, String>();
}
public Map<String, String> getSeasons() {
List<Season> season_list = loadSeason();
for (Iterator<Season> it = season_list.iterator(); it.hasNext();) {
Season season1 = it.next();
seasons.put(season1.getSeason(), season1.getSeason());
}
return seasons;
}
public void setSeasons(Map<String, String> seasons) {
this.seasons = seasons;
}
public List<Season> loadSeason() {
Session session = HibernateUtil.getSessionFactory().openSession();
List<Season> seasonlist = null;
try {
seasonlist = session.createCriteria(Season.class).list();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
session.close();
HibernateUtil.getSessionFactory().close();
}
return seasonlist;
}
public Map<String, String> getPlants() {
List<Plant> plant_list = loadPlants();
for (Iterator<Plant> it = plant_list.iterator(); it.hasNext();) {
Plant plant = it.next();
plants.put(plant.getId().getPlant(), plant.getId().getPlant());
}
return plants;
}
public void setPlants(Map<String, String> plants) {
this.plants = plants;
}
public List<Plant> loadPlants() {
Session session = HibernateUtil.getSessionFactory().openSession();
List<Plant> plantlist = null;
try {
plantlist = session.createCriteria(Plant.class).list();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
session.close();
}
return plantlist;
}
public String getSelectedPlant() {
return selectedPlant;
}
public void setSelectedPlant(String selectedPlant) {
this.selectedPlant = selectedPlant;
}
public String getSelectedSeason() {
return selectedSeason;
}
public void setSelectedSeason(String selectedSeason) {
this.selectedSeason = selectedSeason;
}
public String getSapPlotNo() {
return sapPlotNo;
}
public void setSapPlotNo(String sapPlotNo) {
this.sapPlotNo = sapPlotNo;
}
public String getRyotNo() {
return ryotNo;
}
public void setRyotNo(String ryotNo) {
this.ryotNo = ryotNo;
}
public void loadPlotDetails {
Session session = HibernateUtil.getSessionFactory().openSession();
List<HarvesterRate> rateList = null;
FacesContext context = FacesContext.getCurrentInstance();
try {
if (getSelectedSeason() == null || getSelectedSeason().isEmpty()) {
context.addMessage(null, new FacesMessage("Season is required", ""));
} else if (getSelectedPlant() == null || getSelectedPlant().isEmpty()) {
context.addMessage(null, new FacesMessage("Plant is required", ""));
} else if (getSapPlotNo() == null || getSapPlotNo().isEmpty()) {
context.addMessage(null, new FacesMessage("Plot No is required", ""));
} else {
rateList = session.createCriteria(HarvesterRate.class).add(Restrictions.eq("id.season", getSelectedSeason())).add(Restrictions.eq("id.plant", getSelectedPlant())).add(Restrictions.eq("id.plotNo", getSapPlotNo())).list();
if (rateList.size() > 0) {
for (Iterator<HarvesterRate> it = rateList.iterator(); it.hasNext();) {
HarvesterRate harvesterRate = it.next();
setSapPlotNo(harvestRate.getPlotNo());
setRyotNo(harvestRate.getVendorCode());
}
}
}
} catch (Exception ex) {
ex.printStackTrace();
}
}
getSelectedSeason() and getSelectedPlant() always return null in ajax Call but I Called same method using CommandButton it return Selected value and everything works fine.
The <p:ajax> will by default process only the parent ClientBehavior component, which is in your particular case the <p:inputText>. In other words, the actual value of <p:ajax process> is #this. If you actually want to process the entire form, then you should be using #form as in
<p:inputText ...>
<p:ajax process="#form" ... />
</p:inputText>
The process attribute of <p:commandButton> defaults to #form, that's why it works therewith.
Or, if you have more input components in the form and intend to process only the input and those two dropdowns, then you can specify the (relative) client IDs space separated:
<p:inputText ...>
<p:ajax process="crushingSeason plant #this" ... />
</p:inputText>
Implement ajaxEvent in bean class
Ajax event is not like normal method rather than it invokes the listeners as input, you can configure the Blur action on a text field as following
In JSF page
<p:inputText id="name" value="#{loginBean.name}">
<f:ajax event="blur" listener="#{loginBean.ajaxEvent}" />
</p:inputText>
Ensure that the above must be inside in <h:form>
In ManagedBean class
public void ajaxEvent(AjaxBehaviorEvent e)
{
//Do your stuffs
}
Know more about JSF start from here

sortMode="multiple" and lazy="true" in <p:dataTable> clears multisort meta during pagination

I just enabled multiple sorting in the showcase code for "DataTable - Lazy Loading"
datatableLazy.xhtml
<html lang="en" xmlns="http://www.w3.org/1999/xhtml"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui"
xmlns:ui="http://java.sun.com/jsf/facelets">
<h:head>
<title>CarDataTable</title>
</h:head>
<h:body>
<h:form id="form">
<p:dataTable var="car" value="#{tableBean.lazyModel}" paginator="true"
rows="10"
paginatorTemplate="{RowsPerPageDropdown} {FirstPageLink} {PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}"
rowsPerPageTemplate="5,10,15" id="carTable" lazy="true"
sortMode="multiple">
<p:ajax event="rowSelect" listener="#{tableBean.onRowSelect}"
update=":form:display" oncomplete="carDialog.show()" />
<p:column headerText="Model" sortBy="#{car.model}"
filterBy="#{car.model}">
<h:outputText value="#{car.model}" />
</p:column>
<p:column headerText="Year" sortBy="#{car.year}"
filterBy="#{car.year}">
<h:outputText value="#{car.year}" />
</p:column>
<p:column headerText="Manufacturer" sortBy="#{car.manufacturer}"
filterBy="#{car.manufacturer}">
<h:outputText value="#{car.manufacturer}" />
</p:column>
<p:column headerText="Color" sortBy="#{car.color}"
filterBy="#{car.color}">
<h:outputText value="#{car.color}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
TableBean.java
package com.solartis.primefaces.sample;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import javax.faces.bean.ManagedBean;
import org.primefaces.model.LazyDataModel;
#ManagedBean
public class TableBean {
private LazyDataModel<Car> lazyModel;
private Car selectedCar;
private List<Car> cars = new ArrayList<Car>();
private final static String[] colors;
private final static String[] manufacturers;
static {
colors = new String[10];
colors[0] = "Black";
colors[1] = "White";
colors[2] = "Green";
colors[3] = "Red";
colors[4] = "Blue";
colors[5] = "Orange";
colors[6] = "Silver";
colors[7] = "Yellow";
colors[8] = "Brown";
colors[9] = "Maroon";
manufacturers = new String[10];
manufacturers[0] = "Mercedes";
manufacturers[1] = "BMW";
manufacturers[2] = "Volvo";
manufacturers[3] = "Audi";
manufacturers[4] = "Renault";
manufacturers[5] = "Opel";
manufacturers[6] = "Volkswagen";
manufacturers[7] = "Chrysler";
manufacturers[8] = "Ferrari";
manufacturers[9] = "Ford";
}
public TableBean() {
populateRandomCars(cars, 50);
lazyModel = new LazyCarDataModel(cars);
}
public Car getSelectedCar() {
return selectedCar;
}
public void setSelectedCar(Car selectedCar) {
this.selectedCar = selectedCar;
}
public LazyDataModel<Car> getLazyModel() {
return lazyModel;
}
private void populateRandomCars(List<Car> list, int size) {
for (int i = 0; i < size; i++) {
list.add(new Car(getRandomModel(), getRandomYear(),
getRandomManufacturer(), getRandomColor()));
}
}
private String getRandomColor() {
return colors[(int) (Math.random() * 10)];
}
private String getRandomManufacturer() {
return manufacturers[(int) (Math.random() * 10)];
}
private int getRandomYear() {
return (int) (Math.random() * 50 + 1960);
}
private String getRandomModel() {
return UUID.randomUUID().toString().substring(0, 8);
}
}
LazyCarDataModel.java
package com.solartis.primefaces.sample;
import java.util.ArrayList;
/**
* Dummy implementation of LazyDataModel that uses a list to mimic a real
datasource like a database.
*/
public class LazyCarDataModel extends LazyDataModel<Car> {
private List<Car> datasource;
public LazyCarDataModel(List<Car> datasource) {
this.datasource = datasource;
}
#Override
public Car getRowData(String rowKey) {
for(Car car : datasource) {
if(car.getModel().equals(rowKey))
return car;
}
return null;
}
#Override
public void setRowIndex(int rowIndex) {
if (rowIndex == -1 || getPageSize() == 0) {
super.setRowIndex(-1);
} else
super.setRowIndex(rowIndex % getPageSize());
}
#Override
public Object getRowKey(Car car) {
return car.getModel();
}
#Override
public List<Car> load(int first, int pageSize,
List<SortMeta> multiSortMeta,Map<String, String> filters) {
System.out.println("\nTHE INPUT PARAMETER VALUE OF LOAD METHOD :
\t"+"first=" + first + ", pagesize=" + pageSize + ", multiSortMeta=" +
multiSortMeta + " filter:" + filters);
System.out.println("\nTHE MULTISORTMETA CONTENT : \t");
if (multiSortMeta != null) {
for (SortMeta sortMeta : multiSortMeta) {
System.out.println("SORTFIELD:" +sortMeta.getSortField());
System.out.println("SORTORDER:" +sortMeta.getSortOrder());
System.out.println("SORTFUNCTION:"
+sortMeta.getSortFunction());
System.out.println("COLUMN:" +sortMeta.getColumn());
System.out.println("CLASS:" +sortMeta.getClass());
}
}
List<Car> data = new ArrayList<Car>();
//filter
for(Car car : datasource) {
boolean match = true;
for(Iterator<String> it = filters.keySet().iterator(); it.hasNext();) {
try {
String filterProperty = it.next();
String filterValue = filters.get(filterProperty);
String fieldValue = String.valueOf(car.getClass().
getField(filterProperty).get(car));
if(filterValue == null || fieldValue.startsWith(filterValue)) {
match = true;
}
else {
match = false;
break;
}
} catch(Exception e) {
match = false;
}
}
if(match) {
data.add(car);
}
}
//rowCount
int dataSize = data.size();
this.setRowCount(dataSize);
//paginate
if(dataSize > pageSize) {
try {
return data.subList(first, first + pageSize);
}
catch(IndexOutOfBoundsException e) {
return data.subList(first, first + (dataSize % pageSize));
}
}
else {
return data;
}
}
}
It works well except when I paginate with multiple columns sorting, the load() method with List<SortMeta> does not give me the column details which are currently sorted to carry over to the other page, unlike the load() method with String sortField, SortOrder sortOrder which gives those sorting details.
For example:
Click on the sorting arrow in "manufacturer" and then Ctrl+click on the sorting arrow of "year"
you would get the sorting column details to the load() method (I have printed the input parameters value inside load method).
Now, do pagination. Here the load() method fails to give the sorting columns detail
not only for pagination, if you enter column filter values after clicking on the sorting columns, the same problem exist
How can I fix this?
I solved this question in a temporary way...
Have a sessionscoped managed bean for storing the sorting column details, inorder to get within load() during pagination,
like:-
#ManagedBean
#SessionScoped
public class StoreSortColumnDetail implements Serializable{
/** holds multisort values**/
private List<SortMeta> mMultiSortMeta;
public List<SortMeta> getMultiSortMeta() {
return mMultiSortMeta;
}
public void setMultiSortMeta(List<SortMeta> multiSortMeta) {
mMultiSortMeta = multiSortMeta;
}
public void clearMultiSortMeta() {
if(this.mMultiSortMeta != null)
this.mMultiSortMeta.clear();
}
}
and use it in load() as like this:
#Override
public List<Car> load(int first, int pageSize,
List<SortMeta> multiSortMeta,Map<String, String> filters) {
/** Instance to the SessionScoped scoped StoreSortColumnDetail managed bean*/
#ManagedProperty(value="#{StoreSortColumnDetail }")
private StoreSortColumnDetail storeSortColumnDetail ;
public void setStoreSortColumnDetail (StoreSortColumnDetail sortColumnDetail ) {
this.storeSortColumnDetail = sortColumnDetail ;
}
/** to hold the handled sort column detail**/
List<SortMeta> handledMultiSortMeta = new ArrayList<SortMeta>();
/*Here starts the multisortmeta handling process*/
/** check for List<SortMeta> for null**/
if(multiSortMeta != null ) {
/** updates StoreSortColumnDetail's List<SortMeta> with Load()'s List<SortMeta>**/
storeSortColumnDetail.setMultiSortMeta(multiSortMeta);
handledMultiSortMeta = multiSortMeta;
}
/** check for List<SortMeta> for notnull **/
else if (multiSortMeta == null) {
/**assigns Load()'s List<SortMeta> with StoreSortColumnDetail's List<SortMeta>**/
handledMultiSortMeta = storeSortColumnDetail.getMultiSortMeta();
}
/*Now u have handled multisortmeta from load()...
and u can process now from handledMultiSortMeta*/
}
i hope u came to know how i handled, if not intimate me...
but this is a temporary way,need to handle it through primefaces way...
While this approach might do the trick, you can easily delegate the actual operation to Primefaces in your lazyDataModel. By doing that you keep your codebase clearer since you won't have any other classes to operate and you will reuse the already developed components(which you should anyhow).
If you examined the PrimeFaces source code you will see that DataTable uses concrete classes for each of its features such as filtering, sorting, expanding etc... For sorting Primefaces uses a class called BeanPropertyComparator this class requires certain properties to its constructor but most of these attributes can be get from sortMeta attribute sent to the load method of lazyDataModel. But if you want to get all the attributes you need to have the DataTable object which you can get from FacesContext if you know the client side id of the DataTable in question.
Suppose you have the datatable instance in a variable called activeTable than all you have to do is this:
UIColumn sortColumn = sortMeta.getSortColumn()
if(sortColumn.isDynamic())
((DynamicColumn)sortColumn).applyStatelessModel();
//for single mode sorting
ValueExpression sortByVal = activeTable.getValueExpression("sortBy");
//multiColumn sorting
ValueExpression sortByVal = sortColumn.getValueExpression("sortBy");
int caseSensitive = activeTable.isCaseSensitiveSort();
SortOrder order = sortMeta.getSortOrder();
MethodExpression exp = sortColumn.getSortFunction();
//pass required properties to constructor
//single mode sort
Collections.sort(filteredItems, new BeanPropertyComparator(......))
//for multi column sort use ChainedBeanPropertyComparator and add every new comparator to it than
Collections.sort(filteredItems, chainedComparator);
that way by default your sort will support any feature that primefaces default mechanism support without sacrificing safety.

JSF 2 -- Save All Valid Component Values

I have a requirement to create a javascript function that when invoked will save all of the valid components on a JSF 2.0 form. Since the complete form will never be valid as a whole I need to figure out a way to run the lifecycle per component so that if the validation phase is successful the model will be updated and eventually saved.
Ideally, this needs to be a single ajax request as iterating over each component with a separate ajax request would be painfully inefficient.
Has anyone solved the problem before? If not could you give me some pointers on possible implementations?
Edit:
Here's what I have that seems to be working well so far:
#ManagedBean(name = "partialAppSaveBean")
#RequestScoped
public class PartialAppSaveBean implements Serializable {
protected static final Logger LOGGER = LoggerFactory.getLogger(PartialAppSaveBean.class);
private static final long serialVersionUID = 1L;
/**
* Save any valid Application values
*
* #param event
*/
public void saveData(AjaxBehaviorEvent event) {
final FacesContext context = FacesContext.getCurrentInstance();
UIForm form = getParentForm(event.getComponent());
Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_UNRENDERED);
form.visitTree(VisitContext.createVisitContext(context, null, hints), new VisitCallback() {
#Override
public VisitResult visit(VisitContext context, UIComponent component) {
if (component instanceof UIInput) {
UIInput input = (UIInput) component;
input.validate(context.getFacesContext());
if (input.isValid() && input.getValue() != null) {
ValueExpression valueExpression = input.getValueExpression("value");
if (valueExpression != null
&& valueExpression.getExpressionString() != null) {
try {
valueExpression.setValue(context.getFacesContext().getELContext(), input.getValue());
} catch (Exception ex) {
LOGGER.error("Expression [ " + valueExpression.getExpressionString() +
"] value could not be set with value [" + input.getValue() + "]", ex);
}
}
}
}
return VisitResult.ACCEPT;
}
});
//Save data here
}
/**
* Returns the parent form for this UIComponent
*
* #param component
* #return form
*/
private static UIForm getParentForm(UIComponent component) {
while (component.getParent() != null) {
if (component.getParent() instanceof UIForm) {
return (UIForm) component.getParent();
} else {
return getParentForm(component.getParent());
}
}
return null;
}
}
Invoked with something like:
<h:commandButton
id="saveData">
<f:ajax listener="#{partialAppSaveBean.saveData}" execute="#form" immediate="true" onevent="onPartialSave" />
</h:commandButton>
You could use UIComponent#visitTree() on the UIForm.
FacesContext context = FacesContext.getCurrentInstance();
UIForm form = getFormSomehow();
Map<String, Object> validValues = new HashMap<String, Object>();
Set<VisitHint> hints = EnumSet.of(VisitHint.SKIP_UNRENDERED);
form.visitTree(VisitContext.createVisitContext(context, null, hints), new VisitCallback() {
#Override
public VisitResult visit(VisitContext context, UIComponent component) {
if (component instanceof UIInput) {
UIInput input = (UIInput) component;
if (input.isValid()) {
validValues.put(input.getClientId(context.getFacesContext()), input.getValue());
}
}
return VisitResult.ACCEPT;
}
});

Resources