简体   繁体   English

在primefaces数据表中选择“行”

[英]Select Row in primefaces data table

I have this table: 我有这张桌子:

在此处输入图片说明

<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: 我想要当我单击每个select按钮时,它是分配给selectedBook属性的行信息,并在showSelectedBook()方法中显示它:

Here is the ordersBean : 这是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

Should be something like this: 应该是这样的:

XML code: XML代码:

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

Java bean method: Java bean方法:

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/ http://www.mkyong.com/jsf2/4-ways-to-pass-parameter-from-jsf-page-to-backing-bean/

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: 如果要显示所选的书,则必须设置selectionMode="single"而无需在每行中放置一个commandButton ,只需在页脚小平面中仅指定一个commandButton ,如下所示:

<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: 在这里,你的主要问题是,要设置一个new Book()到您的selectedBook变量,所以一个null值,你selectedBook ,此声明:

private Book selectedBook = new Book();

Should be : 应该 :

private Book selectedBook;

You don't have to instantiate a new Book() in your selectedBook . 您不必在selectedBook实例化new Book()

Take a look at the second Example in this Showcase , to see how it works. 看一下此Showcase中的第二个Example,看看它是如何工作的。

better solution and without select button : 更好的解决方案,并且没有选择按钮:

Xml code: xml代码:

<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: 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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM