hy all,
First: i m not ok for do @ManagedBean and @ViewScoped in Class TestManagedBean where this Class extend LazyDataModel
the solution is:
public final class lazyZoneChargeUtils extends LazyDataModel<ZoneChargeUtils> implements Serializable{
@Override
public List<ZoneChargeUtils> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map<String, Object> filters)
{
currentZones.clear();
setRowCount(zoneChargeService.rowCount(7L).intValue());
List loadedZones = zoneChargeService.getZoneChargeList(7L, first, pageSize);
for (ZoneChargeUtils zone : loadedZones){
if (bufferedZones.containsKey(zone.getId())){
//The zone has been loaded before in the view. Load the buffered value
currentZones.add(bufferedZones.get(zone.getId()));
}else{
currentZones.add(zone);
}
}
return currentZones;
}
}
and :
@ManagedBean
@ViewScoped
public class TestManagedBean {
//all variable
private LazyDataModel<ZoneChargeUtils> listZoneChargeUtils;
@PostConstruct
public void init() {
//init listZoneChargeUtils
this.listZoneChargeUtils = new lazyZoneChargeUtils();
}
public void onEdit(RowEditEvent event) {
ZoneChargeUtils var = (ZoneChargeUtils) event.getObject();
//do your code and save object in DataBase
}
public void onCancel(RowEditEvent event) {
ZoneChargeUtils var = (ZoneChargeUtils) event.getObject();
FacesMessage msg = new FacesMessage("Message : ", var.getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
/**
* @return the listZoneChargeUtils
*
* @author asghaier
*
* Created on 12/mag/2014
*/
public LazyDataModel<ZoneChargeUtils> getListZoneChargeUtils() {
return listZoneChargeUtils;
}
/**
* @param listZoneChargeUtils the listZoneChargeUtils to set
*
* @author asghaier
*
* Created on 12/mag/2014
*/
public void setListZoneChargeUtils(LazyDataModel<ZoneChargeUtils> listZoneChargeUtils) {
this.listZoneChargeUtils = listZoneChargeUtils;
}
//all getter and setter
}
and now in your page do this:
<p:panel id="dataPanel" header="Data">
<p:dataGrid value="#{testManagedBean.listZoneChargeUtils}" var="row" columns="3" rows="9" pageLinks="10" paginator="true" lazy="true" editable="true">
<p:ajax event="rowEdit" listener="#{testManagedBean.onEdit}" />
<p:ajax event="rowEditCancel" listener="#{testManagedBean.onCancel}" />
<p:column>
<h:outputText id="lblCharge" value="#{row.weight}" converter="#{bigDecimalGeneralOutputConverter}"/><br/>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{row.charge}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{row.charge}" style="width:100%" converter="#{bigDecimalGeneralConverter}"/>
</f:facet>
</p:cellEditor>
</p:column>
<p:column >
<p:rowEditor />
</p:column>
</p:dataGrid>
this is in general haw you must do, ( i dont have see good all code).
resume:
1- do class extends LazyDataModel<...> and dont put any @ManagedBean or @ViewScoped
2- do class @ManagedBeanand and @ViewScoped and in the class creat variable in type
LazyDataModel<...> listData, implement getter and setter for this variabile, and init the variable listData in methode init() where is @PostConstruct
3- add editable="true" to your dataTable
4- in the class creat in step 2 add method onCancel & onEdit , in the methode onEdit do your code Java and save the object in database.
5- all is ok :)