开发者

JSF - ValueChangeListener in Datatable

开发者 https://www.devze.com 2023-01-31 17:31 出处:网络
How to implement correctly a ValueChangeListener with UIInput Component within a Datatable, then i getting in a listener method get the row index has changed, new value and old Value

How to implement correctly a ValueChangeListener with UIInput Component within a Datatable, then i getting in a listener method get the row index has changed, new value and old Value

<datatable>
<column>
  <input value="item.unitPrice" valueChangeListener="#{bean.myListener}">
</column>

Changes, not necessarily generate POST to Server, 开发者_运维问答Post may be generated by others events.

Any Help, Thanks.


Bind the datatable's value to a DataModel:

private DataModel<Item> dataModel; // +getter

public Bean() {
    this.dataModel = new ListDataModel<Item>(loadListOfItemsFromDatabase());
}

with

<h:dataTable value="#{bean.dataModel}">

This way you can access the current index (and also the current Item) in the valuechangelistener:

public void myListener(ValueChangeEvent event) {
    int index = dataModel.getRowIndex();
    Item item = dataModel.getRowData();
    Object oldValue = event.getOldValue();
    Object newValue = event.getNewValue();
    // ...
}


In your bean you should have the method like this:

public void myListener(ValueChangeEvent e){
    UIData data = (UIData) e.getComponent().findComponent("myDatatableId");
    int rowIndex = data.getRowIndex();
    Object myNewValue = e.getNewValue();
    Object myOldValue = e.getOldValue();
}

And now you have the row index, old and new values.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号