开发者

using a commandbutton in a datatable column to pass the row index in JSF

I added a commandbutton in a datatable column like this. When the button is clicked the id of the record will be used to update some other records in the database. Here is what I tried:

<ice:dataTable value="#{myBean.storedRecords}" var="record" rows="10">
   <h:column>
     <h:commandButton id="myButton#{record.id}" value="#{record.id}" 
             actionListener="my开发者_StackOverflow社区Bean.buttonActionListener" />
   </h:column>
</ice:dataTable>

In the output I can see that the value is the id of the record. However, in the buttonAction listener method the id of the button appears to be only "myButton" without the id of the record.

Can you please point out how to dynamically set the id of the button. Or can you please suggest a way to pass an index of the row to the server through the button click.

Your help is very appreciated.


Return DataModel to datatable instead of List.

private DataModel storedRecords;

public MyBean() {
    storedRecords = new ListDataModel(recordDAO.listStoredRecords());
}

// ...

Then you can obtain the current row by DataModel#getRowData() inside bean's action method.

public String buttonAction() {
    StoredRecord storedRecord = (StoredRecord) storedRecords.getRowData();
    // ...
    return "outcome";
}

Note that I implicitly hint to use action instead of actionListener on button.


here is my solution.

Through debugging and googling I realized that I HtmlDataTable is the grandparent(!) of HtmlCommandButton. and HtmlDataTable extends UIData class. and UIData has a public method to get the stored value in the current row.

public void btnActionListenerMethod(Action e) {
  if(e.getSource() instanceof HtmlCommandButton) {
    HtmlCommandButton button = (HtmlCommandButton) e.getSource();
    UIData dataTable = (UIData) button.getParent().getParent();
    MyRecord record = (MyRecord) dataTable.getRowData();
    // ......
  }
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜