简体   繁体   中英

Select a row in jsf datatable

Here is my table:

<h:dataTable var="book" value="#{ordersBean.userOrders}"
             binding="#{ordersBean.htmlDataTable}">
    <h:column>
        <f:facet name="header">order ID</f:facet>
        <h:outputText value="#{book[0]}"/>
    </h:column>
    <h:column>
        <f:facet name="header">Book Title</f:facet>
        <h:outputText value="#{book[1]}"/>
    </h:column>
    <h:column>
        <f:facet name="header">Cost</f:facet>
        <h:outputText value="#{book[2]}"/>
    </h:column>
    <h:column>
        <f:facet name="header">Remove Order</f:facet>
        <h:commandButton action="#{ordersBean.deleteOrder}" image="resources/images/del.gif"/>
    </h:column>
</h:dataTable>

I need to get Order ID of which row that the user clicks on it's Remove Order icon and then remove that order from DAO .

Here is the deleteOrder() :

@ManagedBean
@SessionScoped
public class OrdersBean {

private List<Book> userOrders = new ArrayList<Book>();
private HtmlDataTable htmlDataTable;

@Transactional
public void deleteOrder() {
    Book selectedBook = (Book) htmlDataTable.getRowData(); // class cast exception (168)
}

//For populating table
@Transactional
public List<Book> userAllOrders() {
    userOrders = bookDao.getTitleCostQty(String.valueOf(currentUser.getId())).list();
    return userOrders;
}

public HtmlDataTable getHtmlDataTable() {
    return htmlDataTable;
}

public void setHtmlDataTable(HtmlDataTable htmlDataTable) {
    this.htmlDataTable = htmlDataTable;
}
//getter/setters

But i get this error:

javax.servlet.ServletException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.obs.model.Book
    javax.faces.webapp.FacesServlet.service(FacesServlet.java:659)
    org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

javax.faces.el.EvaluationException: java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.obs.model.Book
    javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)

java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to com.obs.model.Book
    com.obs.bean.OrdersBean.deleteOrder(OrdersBean.java:168)

Do you use JSF 2.2? Maybe it's valid from 2.0 ...

So you can pass the actual book within your commandButton:

<h:commandButton actionListener="#{ordersBean.deleteOrder(book)}" ... />

and receive the selected book as parameter inside your bean:

public void deleteOrder(Book selectedBook) {
  // ...
}

value="#{ordersBean.userOrders}" for h:dataTable should be of DataModel type including WrappedData of your model type to avoid cast exception. Do as following:

private DataModel userOrders = new ListDataModel();

public DataModel getUserOrders() {
    userOrders =  new ListDataModel();
    /* populate your list of Book in another list and setWrappedData for userOrders. */
    List<Book> bookList = userAllOrders();
    if (bookList != null && bookList.size() > 0){
        userOrders.setWrappedData(bookList);
    }   
    return bookList;
}

public void setUserOrders(DataModel userOrders) {
    this.userOrders = userOrders;
}

Now, here it will not through any cast exception:

Book selectedBook = (Book) htmlDataTable.getRowData();

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