Java 在素数数据表中选择行
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29965835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Select Row in primefaces data table
提问by
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
In selected Book(), book: null
采纳答案by cн?dk
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 problemhere is that you are setting a new Book()
to your selectedBook
variable, so a null
value to your selectedBook
, this declaration:
您的主要问题是您正在new Book()
为selectedBook
变量设置 a ,因此null
您的值selectedBook
,此声明:
private Book selectedBook = new Book();
Should be :
应该 :
private Book selectedBook;
You don't have to instantiate a new Book()
in your selectedBook
.
你不必new Book()
在你的selectedBook
.
Take a look at the second Example in this Showcase, to see how it works.
看看这个Showcase中的第二个例子,看看它是如何工作的。
回答by Parkash Kumar
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/
回答by Spartan
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:
爪哇豆:
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;
}