Primefaces - how to get the column of a CellEditEvent
java, jsf, jsf-2, primefaces
Solution
You can get the column by referring back to the column header that you passed.
In bean you could do this:
public void onCellEdit(CellEditEvent event) {
int alteredRow = event.getRowIndex();
String column_name;
column_name=event.getColumn().getHeaderText();
// now you can use this to identify the column we are working on.
}
Using getColumnId() or getColumnKey() returns the column Id but with primefaces code added to it - making it difficult to work with.
Problem
I am using primefaces 4. I am using an editable table and when I edit a cell, a listener method is called passing a CellEditEvent Like this ``` public void onCellEdit(CellEditEvent event) { /* * The rowIndex here can be changed according to the sorting/filtering. * FilteredData starts as null, but primefaces initializes it, so you * don't have to check for NPE here */ int alteredRow = event.getRowIndex(); UIColumn o = event.getColumn(); System.out.println(this.filteredData.get(event.getRowIndex()).get(columns.get(0))); } ``` So far, so good. The event has a getRowIndex() But it does not have a getColumnIndex(). Instead, it has a getColumn() method that returns a UIColumn object. The problem is, while debugging, I could not find a way to get any column information (name, id, etc) I can hack the column to have some unique ID like this ``` <p:ajax event="cellEdit" listener="#{myMB.onCellEdit}"/> <c:forEach items="#{myMB.columns}" var="column" varStatus="loop"> <p:column id="col#{loop.index}" headerText="#{column}" sortBy="#{column}" filterBy="#{column}" filterMatchMode="contains"/> <p:cellEditor> <f:facet name="output"> <h:outputText value="#{dataRow[column]}" /> </f:facet> <f:facet name="input"> <p:inputText value="#{dataRow[column]}" /> </f:facet> </p:cellEditor> </p:column> </c:forEach> ``` But still I can't find a way to retrieve the column id from the CellEditEvent So, assuming that a cell is something that has a row and a column, I have to ask How do I retrieve the column of an edited cell in a CellEditEvent? ps. I feel I am missing something, because no one would create a cell event without providing the row and the column, right? update - it seems I can get the ID like ``` org.primefaces.component.column.Column o = (org.primefaces.component.column.Column)event.getColumn(); ``` still, this seems like a hack for me. I am still interested in more elegant solutions for this ;-)