I had this exception in selecting a single row:
javax.faces.FacesException: DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled or you need to define rowKey attribute
Here is the table:
<h:form>
<p:dataTable id="books" value="#{myCardBean.booksList}" var="book" selectionMode="single"
selection="#{myCardBean.selectedBook}" rowKey="book.id">
<p:ajax event="rowSelect" listener="#{myCardBean.onRowSelect}"/>
<p:ajax event="rowUnselect" listener="#{myCardBean.onRowUnselect}"/>
<p:column>
<f:facet name="header">Book ID</f:facet>
<h:outputText value="#{book.id}"/>
</p:column>
<p:column>
<f:facet name="header">Title</f:facet>
<h:outputText value="#{book.title}"/>
</p:column>
</p:dataTable>
</h:form>
And this is myCardBean
:
@ManagedBean
@ViewScoped
@Component
public class MyCardBean implements Serializable {
@Autowired
private BookDao bookDao;
private List<Book> booksList;
private Book selectedBook;
@PostConstruct
public void init() {
userCard();
}
@Transactional
public List<Book> userCard() {
booksList = bookDao.findAllBooks();
System.out.println("all Books size: " + booksList.size() + " books list type: " + booksList); // 3 , list of Object
return booksList;
}
public void onRowSelect(SelectEvent event) {
System.out.println("row selected, " + event.getObject());
}
public void onRowUnselect(UnselectEvent event) {
}
//getter/setter for selectedBook and bookList
And this is Book
model class:
public class Book implements Serializable {
@Id
@GeneratedValue
private Integer id;
@Column(nullable = false)
private String title;
//...
I defined rowKey
and Selection
and selectionMode
in datatable
I have no idea why should i got this error?!
should id
be String
instead of Integer
?
Your rowkey is (again) defined wrong. It should be #{book.id}
as can be seen in the PrimeFaces showcase. Not sure it is the only error, but I stopped reading further when I encountered this one.
Please investigate more yourself. StackOverflow is not a free training or consultancy site
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.