Primefaces update table after on-cell editing - jsf

I have a data table with on-cell editing feature, and I want to update the data table to show the modified record by apply them a different style class.
Here are my problems:
If i do not update the data table when the onCellEdit event fires, records are correctly updated, but I cannot see the applyed style class for modified rows.
If I update the data table when the onCellEdit event fires and use the return key to update a value, all works fine, and I can see the applied style class for modified rows.
If I update the data table when the on-cell edit event fires and use the mouse clic to update a value (clicking on another row or on another cell within the same row), only the first value is updated correctly; when trying to update other values, the onCellEdit event triggers before I can insert the new value, so the event triggers with newValue=oldValue, for all the subsequent changes.
The xhtml page:
<h:form id="frm_tbl_riv">
<p:dataTable id="tbl_rilevazioni" var="rilevazione"
value="#{rilevazioni.rilevazioni}" widgetVar="tbl_rilevazioni_id"
editable="true" editMode="cell" scrollable="true" scrollHeight="350"
rowKey="#{rilevazione.idRilevazione}" selectionMode="single"
selection="#{rilevazioni.selezionata}">
<p:ajax event="rowSelect"
update=":tView:frm_tbl_riv:popup_rilevazioni" />
<p:ajax event="cellEdit" listener="#{rilevazioni.onCellEdit}"
update=":tView:frm_btn_riv" />
<!-- update=":frm_btn_riv :frm_tbl_riv" -->
<p:ajax event="contextMenu"
listener="#{rilevazioni.onRilevazioneSelezionata}"
update="#this" />
<p:column headerText="#{msg['rilevazione']}" width="130">
<f:facet name="header">
<h:outputText value="#{msg['rilevazione']}" />
</f:facet>
<h:outputText value="#{rilevazione.descRilevazione}" id="descRil" />
</p:column>
<p:column headerText="#{msg['valore']}"
styleClass="#{rilevazioni.isModificata(rilevazione) ? 'modificata' : ''}"
width="30">
<h:outputText value="#{rilevazione.valore}"
rendered="#{!rilevazioni.isModificabile(rilevazione)}" />
<p:cellEditor
rendered="#{rilevazioni.isModificabile(rilevazione)}">
<f:facet name="output">
<h:outputText value="#{rilevazione.valore}" />
</f:facet>
<f:facet name="input">
<p:inputText value="#{rilevazione.valore}"
label="#{msg['valore']}">
</f:facet>
</p:cellEditor>
</p:column>
</p:dataTable>
</h:form>
And the managed bean (view scoped):
#ManagedBean(name = "rilevazioni")
#ViewScoped
public class GestioneRilevazioniBean implements Serializable
{
// ...
public void onCellEdit(CellEditEvent event)
{
FacesContext context = FacesContext.getCurrentInstance();
FacesMessage msg = null;
Object nuovoValore = event.getNewValue();
Object vecchioValore = event.getOldValue();
int i = event.getRowIndex();
RilevazioneGiornaliera r = rilevazioni.get(i);
r.setIdUtente(userBean.getUserId());
if (!nuovoValore.equals(vecchioValore))
{
try
{
RilevazioniService.getInstance().updateRilevazioneGiornaliera(r);
modificate.add(r);
} catch (Throwable ex)
{
// ...
}
}
}
public boolean isModificata(RilevazioneGiornaliera riv)
{
return modificate.contains(riv);
}
public boolean isModificabile(RilevazioneGiornaliera rilevazione)
{
// some logic
return true;
}
}
If I use:
update=":frm_btn_riv :frm_tbl_riv"
for the on-cell edit event, I obtain the behaviour specified on point 2 and 3. The same with #form or #parent.

I have founded a solution to update the style class to the modified cell after the onCellEdit event fires.
Data table:
<p:ajax event="cellEdit" listener="#{rilevazioni.onCellEdit}"
oncomplete="handleRilevazioniCellEdit(xhr, status, args);" />
// ...
// within the cell editor
<p:inputText id="rilevazioniCellEditInputtext" value="#{rilevazione.valore}"
label="#{msg['valore']}" />
Javascript:
<script>
function handleRilevazioniCellEdit(xhr, status, args)
{
var modelInput = $('#parentId:' + args.rowIndex + '\\:rilevazioniCellEditInputtext');
var cell = modelInput.parent().parent().parent();
cell.addClass('modificata');
}
</script>
Managed bean, at the end of the onCellEdit() event:
RequestContext requestContext = RequestContext.getCurrentInstance();
requestContext.addCallbackParam("rowIndex", event.getRowIndex());

Related

Show dynamically tooltip after mouseon event in jsf

I am trying to show tooltip dynamically after mouseon event in JSF. Also I need to call a java method on mouseon event in datatable row.
Here what I have tried:
<p:ajax event="mouseover" actionListener="#{buyerWalletView.testMethod)}"/>
public void testMethod(ActionEvent ev) throws IOException {
if (ev.getSource() != null && ev.getSource() instanceof HtmlDataTable) {
HtmlDataTable objHtmlDataTable = (HtmlDataTable) ev.getSource();
System.out.println(objHtmlDataTable.getRowIndex());
}
}
// for tooltip:
<p:column width="150">
<f:facet name="header">
<h:outputText value="#{lbl['wallet.platform']}" />
</f:facet>
<h:outputText value="#{cartItem.platformType}"/>
<p:tooltip for="walletHistoryTable"
value="test"/>
</p:column>
Thanks in advance.

PrimeFaces DataTable RowEditor updating cell elements on RowEditInit, but not on RowEdit or RowEditCancel

I have the following xhtml:
<h:form>
...
<p:dataTable id="table" value="#{BEAN.rows} var="row" >
<p:ajax event="rowEdit" listener="#{BEAN.rowEdit}" />
<p:ajax event="rowEditInit" listener="#{BEAN.rowEditInit}" />
<p:ajax event="rowEditCancel" listener="#{BEAN.rowEditCancel}" />
...
<p:columns id="columnsElement"
value="#{BEAN.columns} var="column" >
<f:facet name="header" >
<h:outputText value="#{column.code}" />
<p:selectBooleanCheckbox id="selectAll"
disabled="#{BEAN.rowInEdit}" />
</f:facet>
<p:cellEditor>
<f:facet name="output">
<p:selectBooleanCheckbox id="cellCheck"
disabled="#{BEAN.rowInEdit} />
</f:facet>
<f:facet name="input">
<p:selectBooleanCheckbox id="selectAll"
disabled="true" />
</f:facet>
</p:cellEditor>
</p:columns>
</p:dataTable> </h:form>
With the RowEdit listeners as follows:
public void rowEditInit(RowEditEvent event) {
rowInEdit = true;
UIData table = (UIData) event.getComponent();
String tableId = table.getClientId();
updateTableCheckboxes(tableId, table.getRowIndex()); }
public void rowEdit(RowEditEvent event) {
rowInEdit = false;
UIData table = (UIData) event.getComponent();
String tableId = table.getClientId();
updateTableCheckboxes(tableId, table.getRowIndex()); } // same for rowEditCancel
public void updateTableCheckboxes(String tableId, int index) {
int rowNumber = 0;
for (RowData row : rows) {
int columnNumber = 0;
for (ColumnData column : columns) {
if (row != index) {
String updateCheckboxId = tableId + ":" + rowNumber
+ ":columnsElement:" + columnNumber + ":cellCheck";
FacesContext.getCurrentInstance()
.getPartialViewContext().getRenderIds()
.add(updateCheckboxId);
}
columnNumber++;
}
rowNumber++;
}
columnNumber = 0;
while (columnNumber < columns.size()) {
String selectAllCheckboxId = tableId + ":columnsElement:"
+ columnNumber + ":selectAll";
FacesContext.getCurrentInstance().getPartialViewContext()
.getRenderIds().add(selectAllCheckboxId);
columnNumber++;
}}
This is dynamically populating the Ids of the check boxes in columnsElement to be updated and is working fine for rowEditInit, disabling all checkboxes as expected. When I click the tick/cross for the rowEdit or rowEditCancel event the Ids to be updated are populated exactly the same, but only the check boxes of the row which had been edited are re-enabled.
If I click on a component elsewhere on the screen or one of the re-enabled check boxes then check boxes of other rows and the selectAll check boxes are all re-enabled as well strangely.
I have tried debugging the JSF lifecycle phases as in this blog, but all stages of the lifecycle are called exactly the same for Init, Edit and Cancel.
As far as I can see I'm doing the same thing, but getting different results in the different cases. Any suggestions why this could be happening?
Thanks
I've managed to fix this using <p:remoteCommand/> outside of the datatable as follows...
<p:remoteCommand name="updateDataTable" update="table" />
and adding oncomplete="updateDataTable()" to the rowEdit and rowEditCancel ajax events.
This has given me the desired end result, but I am still curious why my initial solution didn't work or if there's a better solution.

Delete row form Primefaces LazyDataModel

I have a table with a date column that always null. The data is a LazyDataModel child. Also i have row editing.
<p:dataTable id="dataTable" var="Var" value="#{tableBean.model}"
lazy="true"...........
<p:ajax event="rowEdit" listener="#{tableBean.onRowEdit}"
update=":dataTableForm:messages"/>
<p:column sortBy="VarName">
<f:facet name="header">
<h:outputText value="#{msg['Var.table.header.assignee']}"/>
</f:facet>
<p:cellEditor>
<f:facet name="output">
<h:outputText value="#{Var.Name}"/>
</f:facet>
<f:facet name="input">
<h:inputText id="assigneeNameInput" styleClass="row-input" value="#{Var.Name}"/>
<p:message for="assigneeNameInput"/>
</f:facet>
</p:cellEditor>
</p:column>
<f:facet name="header">
<h:outputText value="#{msg['var.table.header.action']}"/>
</f:facet>
<p:rowEditor/>
</p:column>
I need to delete edited row if date column was filled.
I try this
public void onRowEdit(RowEditEvent event) {
Var updatedVar = (Var) event.getObject();
if (updatedVar.getReturnDate() != null) {
updatedVar = null;
}
}
And this
public void onRowEdit(RowEditEvent event) {
Var updatedVar = (Var) event.getObject();
if (updatedVar.getReturnDate() != null) {
((List<T>) getWrappedData()).remove(oldEntry);
}
}
Both attempts did not work, only if update table twice via remoteCommand . Suggest the decision. Thanks!
Edit: Data removed from lazydatamodel, but on page i still had updated row.
Edit : JSF application lifecycle consist of six phases.
Phase 4:Update model values :
After the JSF checks that the data is valid, it walks over the component tree and set the corresponding server-side object properties to the components' local values. The JSF will update the bean properties corresponding to input component's value attribute.
Phase 5: Invoke application :
During this phase, the JSF handles any application-level events, such as submitting a form / linking to another page.
So, invocation onRowEdit() method occur after dataTable's update.

Request doesn't render response in primefaces datatable

I have a datatable with search field and commandLink to sort. The commandLink that I use to trigger sorting is located not in column header but on the header of datatable. When I load my page and use only commandLink to sort everything works ok. Table sorts in two orders and I see result on my page. Problem appears when I search something in globalFilter. It also works, but after that I cant sort my table. I clear inputText of globalFilter and I cant sort table. To sum up, I see result of sorting only when I not use search field. Sort operation works but request not update the datatable. I put my code below. Maybe somebody knows how to solve it.
<ui:composition>
<p:panel header="Moje pomiary" footer="#{msgs.footer}" id="myMeasurement">
<h:form id="form" prependId="false">
<p:dataTable var="m" value="#{myMeasurementTable.measurement}" id="measureList" editable="true"
widgetVar="mTable"
emptyMessage="No files found with given criteria" filteredValue="#{myMeasurementTable.filteredMeasurement}" >
<f:facet name="header">
Sortowanie według: <p:commandLink id="sortByName" actionListener="#{myMeasurementTable.sortByName}" update="measureList">
<h:outputText value="nazwa pliku" />
</p:commandLink>
|<h:commandLink action="#{myMeasurementTable.sortByArchivisationDate}"> data archiwizacji </h:commandLink>
|<h:commandLink action="#{myMeasurementTable.sortByMeasureDate}"> data badania </h:commandLink>
<p:outputPanel styleClass="searchPanel">
<h:outputText value="Szukaj: " />
<p:inputText styleClass="globalFilter" id="globalFilter" onkeyup="mTable.filter()" style="width:150px" />
</p:outputPanel>
</f:facet>
<p:column headerText="Informacje pomiarowe" style="width:125px" filterStyle="display:none" filterBy="#{m.fileName} #{m.measureDate} #{m.place} #{m.archivisationDate}"
filterMatchMode="contains" >
<p:separator styleClass="separatorColumn"/>
Nazwa pliku: <h:outputText value="#{m.fileName}" /><br />
Data badania: <h:outputText value="#{m.measureDate}" /><br />
Data archiwzacji: <h:outputText value="#{m.archivisationDate}" /><br />
Miejscowość: <h:outputText value="#{m.place}"/> <br />
Współrzędne GPS:
</p:column>
<p:column headerText="Wykresy">
<img src="/tmp/21/myfile.xls/myfile.xls_Parametr x.png" width="150"/>
</p:column> </p:dataTable></h:form></p:panel></ui:composition>
and part of my bean
#ManagedBean(name = "myMeasurementTable")
#ViewScoped
public class myMeasurementTable implements Serializable{
private static final long serialVersionUID = -9193902657201234669L;
private List<Measurement> measurement;
private List<Measurement> filteredMeasurement;
private boolean sortAscending = true;
public myMeasurementTable() {
measurement = new ArrayList<Measurement>();
fillTable(measurement);
}
public String sortByName() {
System.out.println("naciskam sortowanie");
if (sortAscending) {
Collections.sort(measurement, new Comparator<Measurement>() {
#Override
public int compare(Measurement m1, Measurement m2) {
return m1.getFileName().compareTo(m2.getFileName());
}
});
sortAscending = false;
} else {
Collections.sort(measurement, new Comparator<Measurement>() {
#Override
public int compare(Measurement m1, Measurement m2) {
System.out.println(m2.getFileName());
return m2.getFileName().compareTo(m1.getFileName());
}
});
sortAscending = true;
}
return null;
}
Ok I found solution on primefaces forum. It's simple. I only added oncomplete="mTable.filter()" to commandButton and everything works as I want.

Dynamic Column in JSF Datatable JSF2.0

I need one help from you. I am using JSF 2.0 and I have a datatable component . One of the column in the datatable is an action column and I need to create a toolbar which contains different type of actionsource component such as command button, link etc. The type of actionsource is determined at run time and number of actionsource is also done at run time. How I can implement this in JSF 2.0
<p:dataTable value="#{listBranchBean1.rowDataModel}" var="rowItem"
id="myId" paginator="true"
paginatorTemplate="{FirstPageLink}{PreviousPageLink} {CurrentPageReport} {NextPageLink} {LastPageLink}{RowsPerPageDropdown} "
rowsPerPageTemplate="10,5,2" previousPageLinkLabel="<"
nextPageLinkLabel=">" widgetVar="branchTable"
selection="#{listBranchBean1.selectedBranchesPrime}"
resizableColumns="true"
sortBy="#{rowItem.columnsValueMap['branchId'].value}">
<f:facet name="header">
<p:outputPanel>
<h:outputText value="Search all fields:" />
<p:inputText id="globalFilter" onkeyup="branchTable.filter()"
style="width:150px" />
</p:outputPanel>
</f:facet>
<p:column selectionMode="multiple" style="text-align:left">
<f:facet name="header">
<h:outputText value="Select" />
</f:facet>
<h:outputText value="#{rowItem.uniqueId}" />
</p:column>
<p:column
rendered="#{listBranchBean1.columnsMap['objectId'].hidden==false}"
sortBy="#{rowItem.columnsValueMap['objectId'].value}"
filterBy="#{rowItem.columnsValueMap['objectId'].value}">
<f:facet name="header">
<h:outputText
value="#{listBranchBean1.columnsMap['objectId'].displayLabel}" />
</f:facet>
<h:outputText
value="#{rowItem.columnsValueMap['objectId'].value}" />
</p:column>
<p:column
rendered="#{listBranchBean1.columnsMap['actions'].hidden==false}">
<f:facet name="header">
<h:outputText
value="#{listBranchBean1.columnsMap['actions'].displayLabel}" />
</f:facet>
<p:toolbar>
<p:toolbarGroup>
<ui:repeat var="action"
value="#{rowItem.columnsValueMap['actions'].value}">
<p:commandButton title="#{action}" type="button">
</p:commandButton>
</ui:repeat>
</p:toolbarGroup>
</p:toolbar>
</p:column>
</p:dataTable>
I want to replace the last column with something like
<p:toolbar binding="#{listBranchBean1.getActions(rowItem)}">
</p:toolbar>
I appreciate your help
Prajeesh Nair
There is a difference between build-time and render-time in JSF. Build-time tags like <ui:repeat> have the ability to create new components dynamically, but they can only use data that is available at build-time.
However, using Java you are also allowed to alter the component tree programmatically, but this too can not just happen at any moment. The safe moment to do this is the preRenderViewEvent, which is a good bit later than the build-time moment (which is the restore view phase) and you should have all the data you need by then.
Inside an event handler for this event you can reference the tool bar you bound to your backing bean, and programmatically add columns to it.
For examples see:
http://balusc.omnifaces.org/2006/06/using-datatables.html#PopulateDynamicDatatable
http://arjan-tijms.omnifaces.org/2011/09/authoring-jsf-pages-in-pure-java.html
Do note that if your backing bean is #ViewScoped, you'd better not use binding but use a manual lookup instead. This is due to some bugs with respect to the view scope and binding components in JSF.
below code will create dynamic column on the basis of selected country
public void loadDynamicList() throws Exception {
int i=0;
dynamicList = new ArrayList<List<String>>();
dynamicList.add(Arrays.asList(new String[] { "ID1" }));
existingCountryList = new ArrayList<Country>();
String countryCode="US";
existingCountryList.add(getCountryService().getCountryByCode(countryCode));
Country country=getCountryService().getCountryByCode(countryCode);
countryLanguageSet=country.getCountryLanguage();
i=country.getCountryLanguage().size();
dynamicHeaders = new String[i] ;
int j=0;
for (CountryLanguage count: countryLanguageSet) {
System.out.println(count.getLanguage().getLanguageName());
dynamicHeaders[j]=count.getLanguage().getLanguageName();
j++;
}
}
public void populateDynamicDataTable() {
debugLogger.debug("populateDynamicDataTable:Enter");
// Create <h:dataTable value="#{myBean.dynamicList}" var="dynamicItem">.
HtmlDataTable dynamicDataTable = new HtmlDataTable();
dynamicDataTable.setValueExpression("value", createValueExpression("#{relationBean.dynamicList}", List.class));
dynamicDataTable.setVar("dynamicItem");
// Iterate over columns.
for (int i = 0; i < dynamicHeaders.length; i++) {
// Create <h:column>.
HtmlColumn column = new HtmlColumn();
dynamicDataTable.getChildren().add(column);
// Create <h:outputText value="dynamicHeaders[i]"> for <f:facet name="header"> of column.
HtmlOutputText header = new HtmlOutputText();
header.setValue(dynamicHeaders[i]);
column.setHeader(header);
HtmlInputText input=new HtmlInputText();
column.getChildren().add(input);
}
dynamicDataTableGroup = new HtmlPanelGroup();
dynamicDataTableGroup.getChildren().add(dynamicDataTable);
debugLogger.debug("populateDynamicDataTable:Exit");
}
public HtmlPanelGroup getDynamicDataTableGroup() throws Exception {
// This will be called once in the first RESTORE VIEW phase.
if (dynamicDataTableGroup == null) {
loadDynamicList(); // Preload dynamic list.
populateDynamicDataTable(); // Populate editable datatable.
}
return dynamicDataTableGroup;
}
public List<List<String>> getDynamicList() {
return dynamicList;
}
public void setDynamicList(List<List<String>> dynamicList) {
this.dynamicList = dynamicList;
}
public void setDynamicDataTableGroup(HtmlPanelGroup dynamicDataTableGroup) {
this.dynamicDataTableGroup = dynamicDataTableGroup;
}
public ValueExpression createValueExpression(String valueExpression, Class<?> valueType) {
FacesContext facesContext = FacesContext.getCurrentInstance();
return facesContext.getApplication().getExpressionFactory().createValueExpression(
facesContext.getELContext(), valueExpression, valueType);
}

Resources