user4832640
user4832640

Reputation:

Select Row in primefaces data table

I have this table:

enter image description here

<h:form>
    <p:dataTable id="books" value="#{ordersBean.userOrders}" var="book" selection="#{ordersBean.selectedBook}">
        <p:column>
            <f:facet name="header">Order ID</f:facet>
            <h:outputText value="#{book[0]}"/>
        </p:column>
        <p:column>
            <f:facet name="header">Title</f:facet>
            <h:outputText value="#{book[1]}"/>
        </p:column>
        <p:column>
            <f:facet name="header"></f:facet>
            <p:commandButton id="selectRowBtn" value="select" action="#{ordersBean.showSelectedBook}"/>
        </p:column>
    </p:dataTable>
</h:form>

I want when i click on each select button, it's row information assigned to selectedBook property and displays it in showSelectedBook() method:

Here is the ordersBean:

private Book selectedBook = new Book();

public Book getSelectedBook() {
    return selectedBook;
}

public void setSelectedBook(Book selectedBook) {
    this.selectedBook = selectedBook;
}

public void showSelectedBook() {
    System.out.println("In selected Book(), book: " + getSelectedBook());
}

But result is this:

In selected Book(), book: null

Upvotes: 1

Views: 32914

Answers (3)

Spartan
Spartan

Reputation: 1174

better solution and without select button :

Xml code:

<p:dataTable id="ListBook"
    value="#{ordersBean.bookList}"
    selection="#{ordersBean.selectedBook}" var="book"
    rowKey="#{book.id}" selectionMode="single">

    <p:ajax event="rowSelect"
        listener="#{ordersBean.onRowSelectDataTable()}"
                                    update="ListBook" />
  ..... <columns> ..
</p:datatable>

Java bean:

private Book selectedBook=new Book();
private boolean headerButtonsDisabled=true;
//add a List object for all books (bookList) with getter and setter


public boolean isHeaderButtonsDisabled() {
        return headerButtonsDisabled;
    }

public void setHeaderButtonsDisabled(boolean headerButtonsDisabled) {
    this.headerButtonsDisabled = headerButtonsDisabled;
}

public void onRowSelectDataTable() {
    this.setHeaderButtonsDisabled(false);
}

public Book getSelectedBook() {
        return selectedBook;
    }

public void setSelectedBook(Book selectedBook) {
    this.selectedBook = selectedBook;
}

Upvotes: 1

Parkash Kumar
Parkash Kumar

Reputation: 4730

Should be something like this:

XML code:

<p:commandButton id="selectRowBtn" value="select" 
    action="#{ordersBean.showSelectedBook}">
    <f:param name="bookId" value="#{book[0]}" />
</p:commandButton>

Java bean method:

public void showSelectedBook() {

    Map<String,String> params = 
        FacesContext.getExternalContext().getRequestParameterMap();

    int bookId = Integer.valueOf(params.get("bookId"));

    for(Book book : bookList){
        if(book.bookId == bookId){
            selectedBook = book;
            break;
        }
    }

    System.out.println("In selected Book(), book: " + getSelectedBook());
}

Beside, you must have knowledge about the patterns for sending parameters to the actions, refer below link. http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

Upvotes: 2

cнŝdk
cнŝdk

Reputation: 32175

If you want to show the selected book you have to set selectionMode="single" and there's no need to put a commandButton in each row, just specify only one commandButton in the footer facet like this:

<f:facet name="footer">
        <p:commandButton id="selectRowBtn" value="select" action="#{ordersBean.showSelectedBook}"/>
    </f:facet>

And your main problem here is that you are setting a new Book() to your selectedBook variable, so a null value to your selectedBook , this declaration:

private Book selectedBook = new Book();

Should be :

private Book selectedBook;

You don't have to instantiate a new Book() in your selectedBook.

Take a look at the second Example in this Showcase, to see how it works.

Upvotes: 1

Related Questions