简体   繁体   中英

Primefaces - how to get the column of a CellEditEvent

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 ;-)

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.

If you use p:columns rather than p:column - which is also more readable - event.getColumn() returns a DynamicColumn that implements UIColumn . Then you can use:

((DynamicColumn)event.getColumn()).getIndex()

to determine the column index. There is also a column.isDynamic() to check validity.

Using the header would need further code and might be more inefficient.

I found only one way to do this. On the xhtml, add an id to the column:

<p:column id="myColumn">
...
</p:column>

In the backing bean, column.getColumnKey() will return a generated id that contains the id you set in the xhtml (something like j_idt30:j_idt32:0:contratoReal ). Thus, you can do this:

public void onCellEdit(CellEditEvent event) {
    if (event.getColumn().getColumnKey().endsWith("myColumn")
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM