JSF - ValueChangeListener in Datatable
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.
精彩评论