The <h:dataTable> of Jakarta Faces (formerly known as JSF) renders a List as a HTML table, one row per item. This article shows how to show, edit, add, delete, select, sort and page its rows, how to nest tables, how to build the columns dynamically and how to style the rows. Everything is backed by a @ViewScoped bean and a JPA service which lets the database do the sorting and paging. It targets Faces 4.0 and newer. The differences for JSF 2.x, and the way it was done in JSF 1.2, are at the bottom.
The model and the service
The examples use a simple JPA entity.
@Entity
public class Product {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@Column(nullable = false)
private BigDecimal price;
@ManyToOne(fetch = FetchType.LAZY)
private Category category;
// +getters+setters
}
Use wrapper types such as Long instead of primitives such as long for the properties. A new entity has no ID yet, and an empty input field must be able to end up as null. The FetchType.LAZY matters: a @ManyToOne is eagerly fetched by default, and JPA then fires one more query per category for every list of products.
The fields the table can be sorted on are an enum, holding the name of the field in the entity.
public enum ProductSortField {
ID("id"),
NAME("name"),
PRICE("price");
private final String field;
ProductSortField(String field) {
this.field = field;
}
public String getField() {
return field;
}
}
The service does all database work. It lets the database sort and page, so that only the rows of the current page are ever loaded.
@ApplicationScoped
@Transactional
public class ProductService {
@PersistenceContext
private EntityManager entityManager;
public List<Product> list(int first, int pageSize, ProductSortField sortField, boolean ascending) {
var order = ascending ? "ASC" : "DESC";
return entityManager.createQuery("SELECT p FROM Product p ORDER BY p." + sortField.getField() + " " + order + ", p.id", Product.class)
.setFirstResult(first)
.setMaxResults(pageSize)
.getResultList();
}
public long count() {
return entityManager.createQuery("SELECT COUNT(p) FROM Product p", Long.class).getSingleResult();
}
public void create(Product product) {
entityManager.persist(product);
}
public void update(List<Product> products) {
products.forEach(entityManager::merge);
}
public void delete(List<Long> ids) {
entityManager.createQuery("DELETE FROM Product p WHERE p.id IN :ids").setParameter("ids", ids).executeUpdate();
}
}
The sort field ends up in the query string. Thanks to the enum, only the field names it holds can end up there. Never let a request parameter go into a query string as is. The p.id at the end of the ORDER BY keeps the order stable when two rows have the same name or price; otherwise a row may show up on two pages, or on none. The setFirstResult() and setMaxResults() are translated by JPA into the paging clause of the database at hand, such as LIMIT and OFFSET.
Showing the table
Here is the backing bean.
@Named
@ViewScoped
public class Products implements Serializable {
private static final int PAGE_SIZE = 10;
@Inject
private ProductService productService;
private List<Product> list;
private long count;
private int first;
private ProductSortField sortField;
private boolean ascending;
@PostConstruct
public void init() {
sortField = ProductSortField.NAME;
ascending = true;
load();
}
private void load() {
list = productService.list(first, PAGE_SIZE, sortField, ascending);
count = productService.count();
}
public List<Product> getList() {
return list;
}
// ...
}
And here is the table in the Facelet.
<!DOCTYPE html>
<html lang="en"
xmlns:f="jakarta.faces.core"
xmlns:h="jakarta.faces.html">
<h:head>
<title>Products</title>
<h:outputStylesheet name="products.css" />
</h:head>
<h:body>
<h:form id="productsForm">
<h:dataTable id="products" value="#{products.list}" var="product" styleClass="products">
<h:column>
<f:facet name="header">Name</f:facet>
#{product.name}
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
<h:outputText value="#{product.price}">
<f:convertNumber type="currency" currencyCode="EUR" />
</h:outputText>
</h:column>
</h:dataTable>
</h:form>
</h:body>
</html>
The var attribute names the item of the current row. The @ViewScoped keeps the bean, and thus the list, alive for as long as the enduser interacts with the same page. This is important for everything below: on a postback, Faces must find the very same rows again in order to know which row a submitted input or a clicked button belongs to. With a @RequestScoped bean the list would be gone by then.
The bean holds only the rows of the current page. Don't load the whole table into a @ViewScoped bean: it lives in the HTTP session, and every open page has its own instance.
An action per row
To act on a specific row, pass the item of the row as a method argument.
<h:column>
<h:commandButton id="delete" value="Delete" action="#{products.delete(product)}">
<f:ajax render="@form" />
</h:commandButton>
</h:column>
public void delete(Product product) {
productService.delete(List.of(product.getId()));
load();
}
That's all. No binding of the table to the bean, no DataModel and no getRowData() are needed for this.
Editing the rows
To make the table editable, put input components in the columns, bound to the properties of the row item.
<h:column>
<f:facet name="header">Name</f:facet>
<h:inputText id="name" value="#{product.name}" required="true" />
<h:message for="name" />
</h:column>
<h:column>
<f:facet name="header">Price</f:facet>
<h:inputText id="price" value="#{product.price}" required="true" />
<h:message for="price" />
</h:column>
Put the save button outside the table, but inside the same form.
<h:commandButton id="save" value="Save" action="#{products.save}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
public void save() {
productService.update(list);
}
Faces sets the submitted values straight in the Product instances of the list, so the bean only has to hand over the list to the service. Although there is only one <h:inputText id="name"> component, each row gets its own client ID, such as productsForm:products:3:name, and the <h:message for="name"> picks the message of its own row. When a conversion or validation error occurs, the action is not invoked and every row keeps the values the enduser typed.
Adding a row
Add rows via a separate form, so that the inputs of the table are not validated when a new row is added.
<h:form id="newProductForm">
<h:inputText id="name" value="#{products.product.name}" required="true" />
<h:inputText id="price" value="#{products.product.price}" required="true" />
<h:commandButton id="add" value="Add" action="#{products.add}">
<f:ajax execute="@form" render="@form :productsForm" />
</h:commandButton>
<h:messages />
</h:form>
private Product product; // +getter
@PostConstruct
public void init() {
// ...
product = new Product();
}
public void add() {
productService.create(product);
product = new Product();
load();
}
The render references the other form with a leading colon, because it lives outside the current form.
Selecting several rows
For a checkbox per row, bind it to a Map keyed by the ID of the row item.
<h:column>
<h:selectBooleanCheckbox id="selected" value="#{products.selected[product.id]}" />
</h:column>
<h:commandButton id="deleteSelected" value="Delete selected" action="#{products.deleteSelected}">
<f:ajax execute="@form" render="@form" />
</h:commandButton>
private Map<Long, Boolean> selected; // +getter
@PostConstruct
public void init() {
// ...
selected = new HashMap<>();
}
public void deleteSelected() {
var ids = selected.entrySet().stream().filter(Entry::getValue).map(Entry::getKey).toList();
if (!ids.isEmpty()) {
productService.delete(ids);
}
selected.clear();
load();
}
The item itself needs no extra selected property this way. The entity stays clean.
Selecting one row
For a radio button per row, use the group attribute of <h:selectOneRadio>. It exists since JSF 2.3. All radio buttons with the same group name act as one: they get the same name in HTML, so the browser lets the enduser select only one of them.
<h:column>
<h:selectOneRadio id="chosen" group="chosen" value="#{products.chosenId}">
<f:selectItem itemValue="#{product.id}" itemLabel="" />
</h:selectOneRadio>
</h:column>
private Long chosenId; // +getter+setter
Each row has one radio button with the ID of the row item as its value. The empty itemLabel prevents the ID from being shown as the label.
Sorting and paging
To sort a column, put a command link in its header.
<h:column>
<f:facet name="header">
<h:commandLink id="sortByName" value="Name" action="#{products.sort('NAME')}">
<f:ajax render="@form" />
</h:commandLink>
</f:facet>
#{product.name}
</h:column>
To page, put a previous and next button below the table.
<h:commandButton id="previous" value="Previous" action="#{products.page(-1)}" disabled="#{products.first eq 0}">
<f:ajax render="@form" />
</h:commandButton>
#{products.first + 1} - #{products.first + products.list.size()} of #{products.count}
<h:commandButton id="next" value="Next" action="#{products.page(1)}" disabled="#{products.first + products.pageSize ge products.count}">
<f:ajax render="@form" />
</h:commandButton>
public void sort(ProductSortField field) {
ascending = field != sortField || !ascending;
sortField = field;
first = 0;
load();
}
public void page(int direction) {
first = Math.max(0, first + direction * PAGE_SIZE);
load();
}
public int getFirst() {
return first;
}
public long getCount() {
return count;
}
public int getPageSize() {
return PAGE_SIZE;
}
EL converts the string 'NAME' into the enum constant ProductSortField.NAME. Clicking the same header again flips the direction. Sorting starts again at the first page. The actual sorting and paging is done by the database, see "The model and the service", so this stays fast on a table with millions of rows.
The <h:dataTable> has first and rows attributes as well. Those page in memory: they show a part of a list which was loaded as a whole. They are therefore only fine for a short list.
Nested tables
An <h:dataTable> can contain another one. The inner table iterates over a collection of the item of the outer row. Here each Category has a list of products.
@Entity
public class Category {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
@Column(nullable = false)
private String name;
@OneToMany(mappedBy = "category")
@OrderBy("name")
private List<Product> products;
// +getters+setters
}
Load the categories together with their products in one query with a JOIN FETCH. Otherwise JPA fires one more query per category, as soon as the inner table asks for its products.
public List<Category> listWithProducts() {
return entityManager.createQuery("SELECT DISTINCT c FROM Category c LEFT JOIN FETCH c.products ORDER BY c.name", Category.class)
.getResultList();
}
<h:dataTable id="categories" value="#{categories.list}" var="category">
<h:column>
<f:facet name="header">Category</f:facet>
#{category.name}
</h:column>
<h:column>
<f:facet name="header">Products</f:facet>
<h:dataTable id="products" value="#{category.products}" var="product">
<h:column>#{product.name}</h:column>
<h:column>#{product.price}</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
The categories bean is a plain @RequestScoped bean which loads the list in its @PostConstruct, as this page has no form. As soon as the page gets one, make it @ViewScoped.
Dynamic columns
When the columns are only known at runtime, create the <h:column> components with <c:forEach>.
<h:dataTable id="products" value="#{products.list}" var="product">
<c:forEach items="#{products.columns}" var="column">
<h:column>
<f:facet name="header">#{column}</f:facet>
#{product[column]}
</h:column>
</c:forEach>
</h:dataTable>
private List<String> columns; // +getter
@PostConstruct
public void init() {
// ...
columns = List.of("id", "name", "price");
}
This needs the xmlns:c="jakarta.tags.core" namespace. The #{product[column]} reads the property whose name is in column. A <ui:repeat> does not work here. It is one component which renders its body once per item, while the <h:dataTable> needs a physical <h:column> component per column. The <c:forEach> creates those while the view is being built.
Row numbers
The rowIndex property of the table holds the index of the current row. To reach the table from inside, bind it to a variable of the page itself, never to a bean property. A component belongs to the view of a single request, and through its parent and children it holds the entire component tree of that request. A bean which lives longer than the request would keep that tree alive and share it with the next requests.
<h:dataTable id="products" binding="#{productsTable}" value="#{products.list}" var="product">
<h:column>#{products.first + productsTable.rowIndex + 1}</h:column>
...
</h:dataTable>
The rowIndex starts at 0 on every page, hence the products.first to continue counting on the next pages.
Styling the rows
Alternating row colors and highlighting the row under the mouse need nothing more than CSS. Here is the products.css from the Facelet.
table.products tbody tr:nth-child(even) {
background: #f4f4f4;
}
table.products tbody tr:hover {
background: #ffffcc;
}
Alternatively, rowClasses="odd,even" on the <h:dataTable> puts those style classes on the rows in turn. For a style class which depends on the row item, use the rowClass attribute. It is evaluated per row.
<h:dataTable ... rowClass="#{product.price gt 100 ? 'expensive' : null}">
Component libraries
Everything above is plain Faces. A component library such as PrimeFaces offers it all out of the box in one component, with filtering on top. Its <p:dataTable lazy="true"> with a LazyDataModel does the sorting, paging and filtering in the database, the same way as the service above.
JSF 2.x
Everything above works on JSF 2.2 and 2.3 as well, with these differences.
- The packages are
javax.*instead ofjakarta.*, and the namespaces arehttp://xmlns.jcp.org/jsf/html,http://xmlns.jcp.org/jsf/coreandhttp://xmlns.jcp.org/jsp/jstl/core. - The CDI
@ViewScopedisjavax.faces.view.ViewScopedand exists since JSF 2.2. - The
groupattribute of<h:selectOneRadio>exists since JSF 2.3. Below that, use a<h:selectBooleanCheckbox>per row, or a component library.
On JSF 2.0 and 2.1 there is no CDI @ViewScoped. There you use the @ManagedBean and @ViewScoped of the javax.faces.bean package. Passing the row item as a method argument needs EL 2.2, which comes with Servlet 3.0, e.g. Tomcat 7.
JSF 1.2
JSF 1.2 has no view scope and no EL method arguments. The usual way was a request scoped managed bean declared via <managed-bean> in faces-config.xml, the table bound to a HtmlDataTable property via binding, or its value wrapped in a ListDataModel, and the item of the clicked row obtained via getRowData(). To keep the list alive for the next request, it was put in a <h:inputHidden> or a Tomahawk <t:saveState>. Sorting was done in Java with a Comparator and paging via the first and rows attributes, both on the whole list in memory. See also this Stack Overflow answer for all the ways to get the selected row, from JSF 1.x to JSF 2.x.

312 comments:
1 – 200 of 312 Newer› Newest»i gone throgh your post "Using datatables" for creating pagination in JSF. but i didn't understood the "mydao" which you have importing it in java class.
is it folder or api or any custom class. Please let me know what is mydao. becouse of this am not getting any thing. please reply.
my email id
vishwanath.bannur@shivasoftware.com
Regards,
Vishwanath
The literal package name "mydao" ought to be straightforward enough. Also the comments like "Do your "SELECT * FROM mydata" thing." should be clear enough. This article handles the using of h:dataTable in detail, not how you handle the actual data.
Hi BalusC, i am very thankful to u for ur post "Using datatables". it is very useful for me in my project. I need another help from u regarding TabbedPanes. The problem is my jsf page has to show the tabbedpane with tabs and the number of tabs should depend on the property in the bean which is obtained from the list. And each tab should display its own information as in the bean. Moreover we should able to make changes to the data in tab and save it back to the backing bean.
please give suggestions on this how to do. reply me to surendrachowdary.e@gmail.com. i will be waiting for ur reply
Thanks in advance
Regards,
Surendra
Thank you for your great articel about datatable, i just have one question. Is it possible to have dynamic columns and rows with inputtext fields?
Surendra: This is out of the scope of this article. I suggest you to read the documentation of the tabbedpanel component you're going to use.
Joe: Surely it's possible. I'll extend the article with that information as soon as I have time.
Hi,
Your article is verry usefull and I thank you for that.
But I have question too:
What about exporting(xls,pdf) data of the table?
Downloading the .ear files is not working today. The connection times out.
Are you sure it's possible to have a commandlink working in a datatable with a backing bean with the request scope?
I tried to make this work with SUN's JSF 1.2_04 p02 without success.
Thanks in advance.
@Joe: the article is updated.
@viking: there are 3rd party API's for that. Google on that. Exporting to CSV isn't that hard, you can write it yourself, also see the "Parse CSV upload" article in this blog.
@mitch: I've removed the links, the EAR's are outdated in perspective to the article. A newer EAR will come up sooner or later. I'll also look for a better hoster meanwhile.
@Tiago: then you're probably doing something wrong with the data loading logic. Take over the code given in this article and don't change it. You'll see that it works.
Wow,
This is a great tutorial for jsf data table. I have long put off using jsf and this has convinced me to give it another chance. Thank you
Thanks for the great post!
But I am having a headache about how to display set of records page by page, one record each page with irregualr layout...
Can you help me or just knock my head, thanks!
Joe
Balusc, I have tried to deploy the datatable ear file in glassfish and I am not having any luck. I get an error: The requested resource () is not available. I can't even hit the index.jsf. Is importing the Ear file supposed to be the only thing I should do? Thanks! melvingant (at) gmail dot com
Likely you're using the wrong context root. Check the web.xml what it is. In the case of the current EAR it is "UsingDatatables12", so it should be accessible on http://localhost:8080/UsingDatatables12
Thanks,
It now works!
Hi BalusC, I can accomplish the datatable paging with session scope, but unable to with request scope. Do you have any ideas of what I'm doing wrong? thx for your help!
Maybe you're doing something wrong. At least the article is focused on the request scope and the EAR contains an example of paging in request scope.
Excellent post... Helped me a lot with JSF. One question I had was regarding have paging functionality in a dynamically populated datatable... my issue is that the pageNext, pagePrev methods are not getting called.. I tried to create the paging buttons in the bean itself..
HtmlPanelGroup buttonGroup = new HtmlPanelGroup();
List c = buttonGroup.getChildren();
HtmlCommandButton first = new HtmlCommandButton();
first.setAction(app.createMethodBinding("#{DynamicViewBean.pageFirst}",null));
first.setActionListener(app.createMethodBinding("#{DynamicViewBean.pageFirst}",null));
first.setValue("first");
HtmlCommandButton prev = new HtmlCommandButton();
prev.setAction(app.createMethodBinding("#{DynamicViewBean.pagePrevious}",null));
prev.setValue("prev");
HtmlCommandButton next = new HtmlCommandButton();
next.setAction(app.createMethodBinding("#{DynamicViewBean.pageNext}",null));
//next.setActionListener(app.createMethodBinding("#{DynamicViewBean.pageNext}",new Class[]{ActionEvent.class}));
next.setValue("next");
but somehow the Action's are not called Any ideas ?
At least also assign an ID to the buttons.
resolved the issue.. the problem was with other form elements on the page and some validation error :) due to which the button actions were not called.. thanks for the helpful article..
Nice post - very useful!
very informative session.
was wondering if you could point me in the right direction.
i was trying to combine the multiple selection with checkboxes and the paging sections together, but if i select some options on the first page and then change to another and select some more, then go back to the previous pages, the previous selection does not show. am i doing something wrongly?
mei
would appreciate any help. thanks
You'll have to store them in session. Get the EAR and checkout the CRUD example. It demonstrates exactly what you want.
Hi I was looking at your code to sort the DataList within DataTable.. Now my issue is that the DataTable is generated dynamically in the backing bean.. How do I pass the attribute parameter to the HtmlCommandLink with the name/value....In my case the DataTable column value is bound this way...
HtmlOutputText output = new HtmlOutputText();
output.setValueBinding("value",app.createValueBinding("#{dynamicItem[" + i + "]}"));
How do I create a HtmlCommandLink with the parameter value as the get method of the Bean..
Any ideas ?
Use UIComponent#getAttributes(). It returns the attribute map and you can use it the same way like f:attribute.
Hello Balusc,
I'am using t:selectOneMenu in t:datatable. In each row it's a selectOneMenu, one (group1) with 4 items (Key: "0", "1", "2", "3") and one (group2) with 3 items (Key: "0", "1", "3"). Here the problem:
Only if I select in group1 the value with Key="2" and press the save-button, I become a "validation error: Value not valid". The values are String.
The Html-Code looks also right.
I'am using MyFaces1.1.3 and Tomahawk1.1.2.
How can I solve this problem? Have you an idea?
Thanks in advance
Regards,
Mimi
This error can occur if in the next request the selected value isn't one of the values of the list. So all what you need to do is to make sure that in the next request the SelectItem list/array contains exactly the same values as during the form submit and that the selected value matches with one of them.
Great job, thank you.
I have problem. I have tried to update an item in a nesting datatable, but it doesn't work. When I check the values of updated item in a backing bean, there is no change (I use list of ListDataModels wrapped in root ListDataModel). I have also tried to use a binding parameter in inner h:dataTable binding to extra DTO in the backing bean but this doesn't work as well (properties are not changed). When I check parameters from request, I can see a change of item's properties, but it hasn't any influence to ListDataModel in a backing bean.
Thank you, Tony.
Maybe your data loading logic is wrong. Hard to say without actual code. Read the "Editable datatable" and "Nested datatable" paragraphs of this article and play around with the given code examples.
Hey Balusc,
Its an excellent article on insigh of datatable, however it would be helpful if you could guide on tips of look and feel of table like scrolling body and fixed head
Thanks,
Prakash
Balus, I could not download UsingDatatables12EAR.ear
Would you email it to me, hobione@gmail.com
Thank you
I got it, it must be a some kind of server error at that time.
BalusC, you should write a book about JSF. You are good.
Thanks
Hobi
Oklahoma, USA
It's a good info. But I want a scroll bar for data table, but the header should not move when we scroll down.
hi, i think i'm having trouble in distinguishing binding vs value @_@
I already figured it out. Thanks :) you have a really nice tutorial here
Hi Balu sir,
This article is very nice and I am very thankful to u for this "using datatables"..It is very useful to me..
Once again thank u very much...
Regards,
Sathya,
Hyderabad,
India
Brilliant article, thank you. Better than most books on the topic - you should consider doing one.
i have gone through "Select row by radio button"
can u please give the code in jdk1.4 and what is dataItem,dataTable and selectedDataList i could not understand.
please give a soln in JDK 1.4..
Jscript and JSP will be same..
Thanks for grate post! You articles - excellent help for n00b in JSF :)
ThanKs!!
a great article and resources.
BalusC,
Thanks a lot for such manual. It's the best I could find on datatables. However, I think you have couple mistypes in "Customized tables". At least, I couldn't make it work until I changed '#' on '$' in <c:forEach...> and '<h:outputText...>' on '<c:out ...>'. Or maybe I use different versions of libs or something else?
Thank you for the article. This is the best and most complete of any I've seen. Well done.
Great article. Thank you for that.
When invoking an action using a commandLink inside the dataTable, the action "loadMyDataList" is invoked BEFORE the "editMyData" action (see MyRequestBean).
Is there also a way to invoke the reload the data AFTER the commandLink action is invoked?
A use case for this is deleting a database row, so you need to refresh data after it has been deleted.
No. The datatable component in request scope needs to know what data exactly was all listed in the table, so that it under each knows which row object it should return by getRowData().
Solution for your problem: use a session bean for data, load data once in constructor and reload data whenever to your taste after update and delete actions.
Another solution which keeps the bean request scoped: manually remove the deleted item from the dataList using List#remove() instead of reloading the whole dataList.
Additional note: also see the CRUD example in the EAR for the session bean logic.
Hello BalusC, you have a very nice article but I'm sorry for this newbie question... Can you post some code or just a link to another article on how to load data in the datatable using List in the backing bean without the use of generics? I've been googling for days but I can't find what I'm looking for.
It's nothing special. As stated in the comments, just do your DAO thing. The given DAO examples are purely illustrative.
Great article! I am trying to implement a CRUD example with request-scope bean. Using your examples, I have almost got it to work.
I still have one problem: when I am on the "Edit details" page and click on a "cancel" or "save" button, I go back to the "List" page. I always go back to the first page. How can I go back to the page where I was before going to the details page?
Other question: do you ususally work with a request-scope bean or session-scope bean in your apps?
hi,
my requirement is I have to display some data in dataTable and I need to put fixed height for the columns.if the data in that column exceeds then the data should hide and onmouseover one tooltip should come showing the exceeded data.
but the jsf data table is not hiding the data at all.I have given width and height for that column but it is showing the data completely..
please tell me how can I do this one.
Hi. I have implemented your code for datatable pagination and I am wondering how I can reset the "first" counter back to zero if I read in a new set of data. As far as I can tell, I can't directly set it as the backing bean doesn't come into scope until the page is being rendered.
Thanks,
* Robinson
I have used pagination part of your blog. It is simple and effective. Thank you very much for your great blog.
Keep doing it. Because world needs it.
Hello,
What is the best way to trasnlate a value from the database into a text in the datatable?
Thanks,
Romal
Hi balusC, very good article. I have an issue with dynamically generated datatable.
I wish to use a .GIF image as a command button.
I see that there is the option to set value as follows:
_downButton.setValue("");
I wish to use something like
_downButton.setImage(path) (path is the iimage path) but my API does not have such an option. There is however something like setHImage, setDImage, setPImage which does not however show any results. Please advice.
Thanks,
Ashok.
@Romal: start learning the JDBC API.
@Ashok: use setAttribute("image", imageUrl).
UIColumn _editColumn = new UIColumn();
HtmlCommandExButton _editCommand = new HtmlCommandExButton();
String _editActionListener = "#{bkgHandler.editBkgLP}";
String _editAction = "#{bkgHandler.editBkgLPOutcome}";
_editCommand.setId("cmdEdit" + num + type);
Class[] params = {ActionEvent.class };
MethodBinding _editActionListenerBinding = application.createMethodBinding(_editActionListener,params);
MethodBinding _editActionBinding = application.createMethodBinding(_editAction,null);
_editCommand.setActionListener(_editActionListenerBinding); _editCommand.setAction(_editActionBinding);
_editCommand.getAttributes().put("paramNum",Integer.toString(num));
_editCommand.getAttributes().put("paramType",Integer.toString(type));
_editCommand.setStyleClass("commandButton");
if (currentFlt.isA()) {
_editCommand.setOnclick("setPress('popup','A')");
}else {
_editCommand.setOnclick("setPress('popup','B')");
}
_editCommand.setValue("Edit");
_editColumn.getAttributes().put("width","20px");
_editColumn.getChildren().add(_editCommand);
_dataTable.getChildren().add(_editColumn);
The above code works perfectly for a commandButton. However, I have now to use a commandLink instead, So i tried to change commandButton to commandLink as follows:
HtmlCommandLink _editCommand = new HtmlCommandLink();
This does not work to deliver a commandLink. What am I missing here. What can I do to get a commandLink? Please help and advice.
Thanks,
Ashok.
This renders an <a></a> element without any body value. You need to add some body value as child of HtmlCommandLink, e.g. a HtmlOutputText with a value "click here".
Thank you so much. The link appeared. I am now having trouble bring up the pop-up page now. Also, with the command Button(edit), I was able to get row contents from the dataModel for edit onto a pop-up page.
With the commandLink, the .jsp page which is supposed to pop-up is opening as a seperate page.
I go through the document. It is very useful. But I have an issue. How to add a datatable as a row in another datatable?
BalusC et al,
Has anyone tried binding to a nested datatable?
For example lets say we have an Pallete managed bean which contains a List of Box and in the Box object there is a List of Item and a binding to the item datatable.
In the page we have a datatable listing the Boxes, with var="currentBox", and in a column of this table we have a nested datatable which lists the items (value="#{currentBox.items}").
This works ok, but when trying to do a binding to the inner datatable like binding="#{currentBox.itemTable}" there is an exception as follows:
/test.xhtml @55,90 binding="#{currentBox.itemTable}": Target Unreachable, identifier 'currentBox' resolved to null
I am using:
JSF 1.1 (tried on both MyFaces and SUN RI)
Facelets 1.1.14
Your help will be appreciated!
Hi Balu,
The article on daatable helped me a lot. I have an additional requirement to have the feature where the user may add rows to data table
Also, I do not want to hit the server, while adding rows. I am able to add rows using javascript but not able to get the values back. Could you suggest some ways to do the same?
@Stef: bind it to the parent bean. There is only one component in the view, so you should not bind it to the row object.
@Life: not possible without server side interaction. JSF must be notified somehow that it must prepare the list with new rows. If you want to do this asynchronously, consider Ajax4jsf.
BalusC,
I am still struggling with the concept of poping up a window by clicking a commandLink. The js function works perfectly with a Command Button but not with a commandLink. I changed the setOnClick to setTarget without any luck though. Please advice.
Kindly read my earlier post on the same for ref.
Thanks,
AShok.
The onclick must return false to prevent the default action being fired.
Hello,
This is a great post and I would love to learn from the attached code. I have a technical problem - I am using NetBeans and not sure how I can watch the attached examples and if it is possible, at all?
How can I extract the code to run it in NetBeans?
Thank you,
Dana
Hi Bauke,
I have a requirement of highlighting the textBox (inputText) when the validation fails. Can you please give me an idea of how can I achieve this.
Thanks and Regards,
Niki
Use JavaScript. You can find here an example: http://balusc.blogspot.com/2007/12/set-focus-in-jsf.html.
I have found your site to be the most helpful in discovering on to create dynamic objects in JSF. I used your examples to create a list of dynamic check boxes (ajax driven) and to create a dynamic data table.
What I was hoping you could help me with is it possible to display a datatable split between 2 rows? I have a limited amount of space on the page and would like to 'wrap' one column of a datatable below the other information for that row?
@jsage: put <br> in cells or check the 'customized table' section.
@everyone who is interested: I've updated the code behind the downloadable EAR file and replaced it by a WAR file. The DAO logic is more simplified and the code updated according to the current version of the article (and vice versa). The CRUD example is also slightly enhanced (better validation and so on).
balusc, Thank you for the quick response. Using the 'Customized tables' was a snap. Is there anyway of doing this with just a normal jsf datatable (non dynamically generated)? Thanks again
Great tutorial!
I tried to make editable table as you suggested. Maybe I'm doing something wrong though.
On each row I have inputtext & commandbutton. It seems, that values in backing bean dont update except for the last row (nor valuechangelistener is called).
Cant post my code as I write from mobile phone.
Can you advise me something on this issue?
Thanks in advance!
You have apparently bound the inputText to a single bean property instead of to a row object property (in terms of this tutorial, you apparently bound it to #{myBean.value} instead of #{dataItem.value}).
Thank you for immediate response!
This is not the case though. I bind inputboxes same way as other noneditable columns and values print out correctly. Just setters are not called.
With no success I tried also component binding.
I dont know if that matters as I am complete beginner, but I have Netbeans 6.1 with JSF 1.1 plugin.
Sorry for posting this spam but I am desperate and would appreciate any help.
I posted my code here:
http://forum.builder.cz/read.php?15,2670515
OUTPUT ON INITIAL PAGE LOAD:
id get 854
vstup get 854
id get 1102
vstup get 1102
id get 1103
vstup get 1103
id get 1152
vstup get 1152
OUTPUT ON NONLAST ROW DATA ENTER AND ITS BUTTON CLICK
854 Default text
id get 854
vstup get 854
id get 1102
vstup get 1102
id get 1103
vstup get 1103
id get 1152
vstup get 1152
OUTPUT ON LAST ROW DATA ENTER AND ITS(LAST) BUTTON CLICK
vstup get 1152
AHOJ
vstup set 1152
1152 New entered text
id get 854
vstup get 854
id get 1102
vstup get 1102
id get 1103
vstup get 1103
id get 1152
vstup get 1152
I know that on each page load values are set to default. This is for debug only. What I dont understand is, why setter and action listener is called only for last row (see output)
Thank you very much even for reading this :)
You should place the h:dataTable inside a single h:form. You should not place h:form in h:column, this will render multiple form elements in each row.
Yeah! It works! I was bumping my head of it til Monday.
So I understand that h:form tags should be out of h:dataTable scope, not otherwise.
Does it matter if I have more forms in one html page? What about one form around all my page body content?
I guess I should read generated html code, but my knowledge of it is just very basic.
Thanks a lot again, man!
You can have as many as you want of them, as long as you don't nest them (which is just prohibited by HTML spec, not by JSF spec). And in case of a datatable, obviously place the datatable inside a single form; each input component inside a table must know of each other.
Hi again, one last question.
I got through generated pages code.
There are no "recursive" forms. Thats why I dont understand why h:form in h:column dont work, as values from different rows dont have to see each other.
Moreover I read somewhere that html form in table is ok as long as all of is nested in one column. It also seems to me more natural - one form for each row.
Is that not an issue of JSF than?
P.S. I'll do it like you wrote but I'd like to understand reasones.
Hi Balusc
Thank-you very much for your detailed article on using datatables - it seems like you are the leading authority on JSF out there in webland at the moment.
My question relates to the use of getRenderResponse to determine whether or not to reload the data. E.g.
public List getDataList() {
if (FacesContext.getCurrentInstance().getRenderResponse()) {
loadDataList(); // Reload to get most recent data.
}
return dataList;
}
I am using JSF within a portlet on IBM's Websphere Portal.
Unfortunately within within this environment getRenderResponse always returns false. I'm not sure whether this is linked to the Portlet life cycle.
Do you have any other suggestions for preventing unnecessary loads of data from the database the user selects an item, but also maintaining the most recent data when the user displays the list of items.
Regards
tc
@tc: please continue here: http://forums.sun.com/thread.jspa?messageID=10395497.
Thank you for the great tutorial.
I would like to edit a table's item in which one of the values should come from a "h:selectOneListbox" and not an "h:inputText".
How Can I do that?
Thank you,
Dana
@dana: just replace the 'h:inputText' by 'h:selectOneListbox'. The 'f:selectItems' or 'f:selectItem' values can come either from the backing bean (if the values are the same for listboxes in all rows), or from the row object (if the values are specific for every row).
this article was great! It helped in a very efficient way.
As a newbie in the topic, one thing I missed was including h:form around the datatable. It was only when I looked at the code for adding the edit functionality (later in the tutorial) did I notice that you were actually operating in the context of a h:form.
Thanks again
Hi, the blog was extermly very useful. But i would like to suggest you that for 'Populate dynamic datatable' it would have rather been correct if you had used 'i' instead of '0' in the for loop i.e.for (int i = 0; i < dynamicList.get(0).size(); i++)->for (int i = 0; i < dynamicList.get(i).size(); i++)
The code assumes that every row has the same amount of columns.
Hi, Balu how to refresh datatable in jsf for every x minutes, the System retrieves data from database
Add a meta refesh tag to the HTML head.
BalusC you are the best...
Thanks balusc for this code,
I tried to use section "Populate dynamic datatable" and it was very useful.
But, when I was checking the code on my side I was not able to display rows dynamically by your code.
I added one line to set the value of datatable dynamicDataTable.setValue(dynamicList);
and it worked Great!!!
Excellent and useful tutorial!!
This is a superb tutorial, and it has given me good insight into creating nice and dynamic components in the world of JSF.
The one problem that I have, is very similar to the one noted by Stef, but unfortunately, in the component, I do not know the parent bean. Let me explain quickly, I have a facelet component whom has a parameter passed in, lets call it 'controller', and this controller has the method to create the dataTable,
<h:dataTable binding="#{controller.dataTableImplementation}"/>
but when I set it on the binding, I get the same error as steff, i.e.
javax.el.PropertyNotFoundException: /test-component.jspx @14,67 binding="#{controller.dataTableImplementation}": Target Unreachable, identifier 'controller' resolved to null.
Is this concept possible with a facelet component?
Any help greatfully appreciated,
Thanks,
Osh
Hi,
Your tutorial is very helpful. Can u publish an article on how to dynamically generate JSF components based on permission/previlige of the user.
Manu
It's was one the best tutorials I've ever met!
good on you:)
Great Post!!! You are the GOD of JSF!
Please correct me if I'm wrong.
Your code is working if the bean scope is session. The session scope, however, could lead
to the memory problem if a huge amount of data is sitting in session beans.
This is why request scope is perferred.
When using request scope, your code is not guaranteed to be working.
With request scope, you'll have to load data list from database twice.
If the data list you got at the second time is not exactly the same as at the first time,
then
JSF can not bind data table correctly and editDataItem() is screwed up.
In the worse case, if at the second time, no data list can be found in the database, editDataItem() is not even get executed.
This is a big problem with JSF when request scope is used. One has to somehow cache the d
ata to make the datatable binding possible.
For that, use Tomahawk's t:dataTable instead. It works the same, but it has under each one important extra attribute: preserveDataModel. If you set it to true, then the datamodel will be cached for the next request and it will remove the potential problems you mentioned.
Thanks for a great tutorial BalusC. However I think, in the code for MyBean Class, the line:
private MyDataDAO dataDAO = DAOFactory.getMyDataDAO();
needs to be replaced by the following lines:
DAOFactory DAOFactory = DAOFactory.getInstance("javabase");
private MyDataDAO dataDAO = DAOFactory.getMyDataDAO();
Does in fact not really matter. It's all about the idea :)
But a purist as I am, I have changed it.
Great! This is of Great help.I had struggled alot with datatables.This has solved many of my problems.
Thanks Again.
Awesome! Lot of insight into the building of dynamic tables. Thanks.
I am actually building a dynamic datatable where Checkbox, Dropdown, etc., are randomly sprinkled into the cells. I hope your examples should help me more!
heyy its owesome thanks for ur work..
A big Thx from France "you saved my life" for this work. ;-)
Thanks for the fantastic piece of dataTable and the debug JSF lifecycle work.
I had a question regarding commandbutton/link placed inside the datatable.
I had a look at your code - add_action_to_every_row.jsp & populate_action_datatable.jsp and the backingbean i.e. MyRequestBean.java.
I have this bean as request scope. I can display my datatable with rows having commandLink. If I click on any of the links, event is triggered in my backing bean if i have the following lines in my getDataList() method.
if (FacesContext.getCurrentInstance().getRenderResponse()) {
loadDataList(); // Reload to get most recent data.
}
But if I remove these lines my event is not detected and nothing happens in my invoke_application phase.
Can you explain this behavior? Why is my event not getting queued?
Please feel free to ask questions if I'm not clear.
Thanks very much.
JP
Apologies..Just want to correct something in the previous post. It works if the getDataList() has the following code:
public List getDataList() {
loadDataList(); // Preload by lazy loading.
}
return dataList;
}
Doesn't work if I have this condition
(FacesContext.getCurrentInstance().getRenderResponse())
Sorry about that.
The getter is also called during apply request values phase.
The renderResponse trick is only applicable when you're using a session scoped bean. Also see the text in the article.
Thanks for the quick reply. I understand that.
But I'm afraid I'm still not clear that if I remove the loadDataList() call from within the getDataList() method, my event is not detected and nothing happens in my invoke_application phase.
My design was to call the loadDataList() when my event is triggered and the backing bean method is invoked. Issue is event is not being queued.
Can you please explain this behavior? Why is my event not getting queued?
Thanks
JP
Balus - Thank you! Great info on the multi-select checkboxes within a data table. Nice stuff.
Hi Baluc,
Thanks a lot for your support on this. Will this "populating dynamic datalist would work with JSF 1.1? I am restricted to use only JSF 1.1 and I am getting an error at the "createValueExpression stating this method does not exist". Can you let me know your inputs.
Thanks
Aisg
Hi Baluc,
In continuation with my earlier post, I found the helper method private ValueExpression createValueExpression() and included in my code. Unfortunatly, It is erroring out in ValueExpression, getExpressionFactory, getELContext, etc. When I included javax.el.ValueExpression, it is not taking it and showing an error. Can you please let me know what I am missing.
Thanks
Aish
Either upgrade to at least JSF 1.2 or make use of the JSF 1.1 ValueBinding methods.
Hi Balu,
Thanks for getting back to me. When you say "make use of ValueBinding method of JSF 1.1", how do I do it? I changed my faces-config.xml to refer to version 1.1. I checked my .jar files and they seem to be of the same size as JSF 1.1 jar. Please let me know what else I should do to use valueBinding method of JSF 1.1?
Thanks
Aish
Start with reading the javadoc of Application#createValueBinding().
Hi Baluc,
Thanks for informing me. I looked at createValueBinding in javax.faces.application.Application and it says that this method is deprecated. Please let me know
Thanks
Aish
So? You're using JSF 1.1, not JSF 1.2. It is just deprecated since JSF 1.2.
Hi Baluc,
I modified the code and it worked fine. Thanks a lot for your code and your support.
What if I have a long list of columns to be displayed in the table. HTML Table may not be enough as we need to scroll 9both vertically and horizontally) to see all the columns. Can you please guide me so that I can try the same.
Thanks
Aish
What if we have the situation in which in one browser window a datatable row is deleted and in another browser window the same row is to be modified, what will happen in that case? Is there a way to place a message saying that the row is already deleted?
thanks,
Marius
Hi BalusC,
Thanks for the great Block it helped me alot since I am a newbie with JSF.
I used the selecting multiple example with a checkbox for my table and I also added the paging at the bottom of my table.
Everything works fine, except I get a NullPointerException if I select a few lines and don't page thought the whole table.
How can I resolve. Basically if someone wants to select rows only in page 1 and submits my form it doesn't work.
Thanks for your help.
@Tshadi
I had the same problem.
Had a few pages displayed and wanted to bulk delete a few rows. It didn't worked because the code says:
public String getSelectedItems() {
// Get selected items.
selectedDataList = new ArrayList< MyData >();
for (MyData dataItem : dataList) {
if (dataItem.isSelected()) {
selectedDataList.add(dataItem);
dataItem.setSelected(false); // Reset.
}
}
// Do your thing with the MyData items in List selectedDataList.
return "selected"; // Navigation case.
}
as you can see:
for (MyData dataItem : dataList)
that means that you'll itereate trough the whole list of objects. when the dataItem will go over the rows displayed you'll get the NullPointerException.
You need to identify what are the rows displayed currently.
What I did was:
List< MyData > batchRows = new ArrayList< MyData >();
for (int i = this.dataTable.getFirst(); i < (this.getDataTable()
.getFirst() + (this.dataTable.getRowCount() - this.dataTable
.getFirst())); i++) {
batchRows.add(this.< yourOriginalListOfObjects >().get(i));
}
then you retry the code as :
for (MyData dataItem : batchRows) {
should work ...
Hi Maruis,
Thanks for the reply, but it still craches in the for loop:
for (Parameter batchrow : batchRows) {
if (selectedParamIds.get(batchrow.getId()).booleanValue()) {
selectedParamList.add(batchrow);
selectedParamIds.remove(batchrow.getId());
}
Exellent tutorial, very ellucidative. Thank you so much for giving so much details and samples.
Just a glitch: the download of the war file seem broken. Sad.
The download works fine and the WAR extracts fine. Maybe you're on a company network and its firewall is blocking file sharing sites.
Thanks. The info regarding checkboxes selection really helped.
Bedankt voor de grondige uitleg over deze component, het was heel nuttig en leerrijk binnen ons project!
I had question about the multiple row selection using checkboxes in dataTable.
I went through this example.
http://balusc.blogspot.com/2006/06/using-datatables.html#SelectMultipleRows.
This works fine for session scoped backing beans.
Just wondering if the backing bean is in REQUEST scope and not session scope, how can I retrieve the selected rowIndexes. Please advise.
It is designed for request scoped beans. Maybe your data loading logic is wrong.
Thanks for the reply BalusC, I'll have a look again at my data loading logic code.
I really hope you are right.
Cheers
JPSingh
Hi BalusC,
Just had a quick question regarding the following method
FacesContext.getCurrentInstance().getRenderResponse()
In the getter method of my backing bean I want to distinguish between apply request phase and render response phase so i'm using
FacesContext.getCurrentInstance().getRenderResponse()
But it appears it does not return TRUE for all requests.
I have got the LifeCycleListener configured for an application. In my logs I can see
LifeCycleListener.BeforePhase: RENDER_RESPONSE 6 and then it goes into one of my getter methods...
public List getPaymentDetails() {
if(FacesContext.getCurrentInstance().getRenderResponse()) {
JSFUtil.debugInfo("InvoiceSearch.getPaymentDetails - Render Response Phase");
//Response phase logic
} else {
//Apply request values phase logic
}
My understanding was this method will always return true if in Render response phase...but i'm afraid it's not the case
Can you please advise what I'm missing here.
Thanks
JPSINGH
Upgrade your environment. I've seen cases where in an outdated IBM portlet application with an outdated JSF version would incorrectly return false in getRenderResponse() all the time.
Thanks for the reply. I am currently using JSF RI 1.1_02 on TOmcat 5.5.
For some reason i cannot upgrade to jsf 1.2, can you please advise any other way of checking the phase?
Thanks
JPSINGH
Thanks a ton for the article. Is it possible to create such a datatable using jsp alone without jsf?
Use JSTL c:forEach.
thanks for replying, BaluSC. if i use c:forEach how do I name the form fields and transfer values them to database. Any inputs would be appreciated.
Use varStatus to get the loop index, use names with array index and use getParameterValues().
This has nothing to do with JSF nor with this blog article. Please use the appropriate places/forums for more detailed answers.
Hi BalusC,
Another question regarding dataTable
I'm doing a search by calling a webservice that returns me a list or orderVos. DataTable displays the list as rows. Each row has a Approve and Reject button, on pressing either invokes action methods.
So far so good.
Now an additional requirement is that the user can add some comments before approving or rejecting. What i'm doing now is that i have a textarea component hidden (display:none) in each row. Looking at the html source that gets generated the ids for the
textareas is 'j_id_jsp_1908125534_1:dataTable:2:paymentCommentArea'. I can retrieve the rowIndex and make up the id as 'dataTable:rowIndex:paymentCommentArea' and what im doing is
Iterator i = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterNames();
while(i.hasNext()) {
String s = (String) i.next();
if(s.indexOf("dataTable:" + rowIndex + ":paymentCommentArea") != -1) {
System.out.println("EXACT Key Found [ " + s + " ]");
System.out.println("trying to get the value ["+FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(s)+"]");
}
}
This works fine but just wondering if there's a better way of grabbing the textarea value..like can i bind it with
the backing bean....instead of looping through the request params.
Bind textarea value to row object?
Thanks for the reply.
The dataTable rows are bound to the list of OrderVOs returned by the web service.
To bind the textarea i have to add an extra property to the OrderVO...i was wondering if i can get away without having to do this.
Any other advise will be appreciated.
Cheers
Add the property to the parent managed bean then. Use Map<RowIdType, TextAreaValueType>. You can get the idea by looking at "Select multiple rows" chapter more closely.
Thanks for the advise, i'll certainly have a closer look to it.
Cheers
Thanks Balusc for sharing your knowledge with us. Just 6 years of java and you have such amazing expertise, it's unbelievable dude. Keep up the good work.
Hi BalusC,
I have a problem in using Datatable for my reqirement. I want a datatable whichwhich requiresme to have dynamic columns. The number of colums to be displayed is to be fetched from server. How can i do this. Please reply
Read chapter "Populate datatable".
I am having an issue on the radio datatable select option. I did the following:
h:selectOneRadio onclick="dataTableSelectOneRadio(this); submit();"
valueChangeListener="#{pc_Selectaccount.setSelectedItem}"
f:selectItem itemValue=""
/h:selectOneRadio
But it keeps iterating through the entire datatable and executing the valuechangelistener. But when I actually select a button, it doesn't run the valueselectlistener on the button I clicked, it just rereuns it on all the data again. In this way, my currently selected radio is always the last row. Any help? Thank you!
Hi,
I am trying to build a validator to check wether at least one row is selected via check box.
My approach is similar to the second way to select multiple rows using bindings (via a Map(String, UISelectBoolean)) for the checkboxes in order to access their values in an validator (attached to a button).
Doing so I run into the following issue:
h:dataTable value="#{myBean.itemList}" var="item">
h:column>
f:facet name="header">
headerText
/f:facet>
h:selectBooleanCheckbox value="#{item.selected}" binding="#{myBean.checkboxBinding[item.id]}" />
/h:column>
...
/h:dataTable>
Gives me a PropertyNotFoundException: binding="#{myBean.checkboxBinding[item.id]}": Target Unreachable, 'BracketSuffix' returned null
The only explanation I have is that binding expressions are evaluated before the dataTable loops over its rows, hence there is no "item" during binding evaluation.
Any hints on how to get this binding working (or another way to validate the checkboxes)?
Thanks a lot!
The binding="#{myBean.checkboxBinding[item.id]}" isn't going to work. There's only one JSF component. Replace by binding="#{myBean.checkboxBinding}"
That brought me one step further ;-)
How can I now access the values for the different rows in the one component?
Tanks a lot for your help!
And maybe you can point me to some good documentation / tutorial regarding the internals of the dataTable. I guess I am struggling with this because I haven't found any yet (besides your great tutorial regarding dataTable usage of course ;-).
Use its 'value' attribute.
Can u please explain how to use column and row span in datatable. Please can u provide sample piece of code
Not possible with h:dataTable. Colspans are possible with Tomahawk t:dataTable. Rowspans are only possible if you generate your own HTML table using t:dataList or ui:repeat (or c:forEach).
Hi BaluC,
I am in a catch 22 kind of situation. I am using rich:datatable to display a couple of rows. I cannot assign ids to them because they are looping through a data structure from the bean. My funtionality is to submit each row independently. So here comes the issue: I need to have the bean in session scope for the actions menthods to be called from a a4j:commandLink inside my rich table. But since the scope is session, my ids are not getting refreshed and I am getting an duplicate id error.
If I solve this issue by having the session to request, my action methods are not getting called from within the rich datatable.
I tried issuing ids to the rows by using jstl but they are getting ignored during rendering phase.
Can you please advise on how to solve this situation.
Hi Amita,
I believe you have some kind of command button or link called as 'Submit' on each row of the dataTable. If you try wrapping your collection in ListDataModel we would actually get hold of the row/object the is button is clicked, in the action method.
Regards
-hm
Hi Fluxion,
The problem is that the action method doesnot get hit in the request scope.
Otherwise, in the session mode I am able to uniquely identify the user selection.
Hi BalusC,
I have a requirement to populate dynamic datatable based on value selected from drop down.
step 1: User will select name of table from drop down. No submit button is there.
step 2: Using Ajax, dynamically data table will be populated.
What I have done :
// Using rich faces ajax support
start h:selectOneMenu ....
.....
a4j:support event="onchange" reRender="dynamictableList"
.....
end h:selectOneMenu
then using similar code as BalusC code for dynamic data table (populated from backing bean) :
h:panelGroup binding="#{userLoginSuccessBean.dynamicDataTableGroup}" id="dynamictableList"
But the panelgroup with id="dynamictableList" is not re-rendering in JSF page to fetch new values.
Where as other UI components are getting re-rendered when used with a4j:support.
Any solution would be really helpful.
Thanks
Hi BalusC,
I have been working with your example .war code for the DataTables with the Radio Button. The ValueChangeEvent is not fired without a submit(), which is missing in the explanation / Javascript function dataTableSelectOneRadio(). Is this True or am I missing Something ?
The Javascript does indeed not submit the form immediately. If you want to submit the form immediately (so that the ValueChangeEvent at the server side goes immediately fired), then you need to add a JavaScript line which submits the form.
Great post thanks! could you help me with setting progmaticaly addActionListener to HtmlCommandLink in datatable.I did idLink.setActionListener(facesContext.getApplication().createMethodBinding("#{myBean.processAction}",null));
idColumn.getChildren().add(idLink);
but it does not work. says Method not found.
Thank's.
You forgot to specify the ActionEvent method parameter in createMethodBinding.
If you're using JSF 1.2 better get rid of the deprecated methods and use createMethodExpression and MethodExpressionActionListener.
Thank you for a tip. I got the full working code like this:
FacesContext facesContext = FacesContext.getCurrentInstance();
Application application = facesContext.getApplication();
ExpressionFactory el = application.getExpressionFactory();
MethodExpression methodExpression = el.createMethodExpression(facesContext.getELContext(), "#{myBean.processAction}",
null, new Class[]{ActionEvent.class});
MethodExpressionActionListener methodsexpression = new MethodExpressionActionListener(methodExpression);
idLink.addActionListener(methodsexpression);
Hello again.
I still in solution search.
I thought addActionListener with action listener method
HtmlCommandLink hc = (HtmlCommandLink) getTable().getChildren().get(0).getChildren().get(0);
System.out.println("value cell: " + hc.getValue());
will help me to get cell value but I get null instead of cell value.
I have anagrammatically generated datatable of HtmlCommandLink's and need to detect which link was pressed by user, get link value (cell of datatable) and send him to next page with appropriate settings.
GetTable().getRowData() gives me whole row data what is not good in this case.
Hi!
I don't know how can I open the war or use it to view your examples. What should I do with this file?
Thanks!
It's just a Webapplication ARchive. It's just a complete Webapplication file structure in ZIP format. You normally deploy such files in the application server.
Hi BalusC,
I am having problems with having selectOneMenu inside datatable and setting it's default value.
What I want to happen is that when my "EDIT ROLE" page loads, datatable will consist of selectOneMenus depending on how many rows the page returns based from roleId. The selectOneMenu is dynamic, as well as the selectItems. Data comes from DB. The datatable loads properly if I don't set value for each selectOne menu.
To illustrate:
FUNCTIONS VIEW ADD EDIT DELETE
#selectOneMenu>with setValue #checkbox> #checkbox> #checkbox> #checkbox>#selectOneMenu>with setValue #checkbox> #checkbox> #checkbox> #checkbox>#selectOneMenu>with setValue #checkbox> #checkbox> #checkbox> #checkbox>#selectOneMenu>with setValue #checkbox> #checkbox> #checkbox> #checkbox>#selectOneMenu>with setValue #checkbox> #checkbox> #checkbox> #checkbox>
This table can be edited that's why I needed a drop-down (selectOneMenu). I hope you may be able to assist me. Thanks!
You forgot to tell the actual problem.
If your actual problem is "I do not know how to set selectOneMenu's default value.", then the answer is simple: just preset the property behind the 'value' attribute.
Hi BalusC,
I know how to set the default value, problem is, when I set default value of selectOneMenu, it doesn't appear inside the datatable. In short nothing appears.
I've been googling this past 2 days about putting selectOneMenu inside datatable, and it seems that most people are having the same problem. Another question is, what kind of approach would you suggest to display selectOneMenu per row on a datatable. Thanks in advance!
I have no idea about those problems. Most likely the code flow / data loading logic is plain wrong. Just debug it or post a topic with an SSCCE at a JSF forum (forums.sun.com maybe?).
is there any way to get data from cell i clicked on? i tried to use a4j:support inside rich:column but it didn't worked. It didn't fire an event at all. But even if it had fired an event i wouldn't have known what to do with it. Because i don't know how to take cell's value inside backing bean. :)
For example, if i want that a click on a cell would call some action that would take a value of this cell and printed it in console. How can i do this?
Thanks Balu. this article has been a tremendous help.
- Anand
I want to use checkboxes in datatables and I'm considering your second approach with
<h:selectBooleanCheckbox value="#{myBean.selectedIds[dataItem.id]}" />
I see there is a
private Map<Long, Boolean> selectedIds = new HashMap<Long, Boolean>();
in MyBean but it has only a getter method, no setter. What happens when you submit the form? How does the information get into selectedIds which is a private member?
In EL, there is no setter needed for collection and map properties. EL just does bean.getList().add(index, object) or bean.getMap().put(key, value).
Hi balusC,
I need radio buttons inside
the datatable(JBoss & RichFaces).
How to create rows inside the dataTable? Post me as soon as possible....i ve to complete by tomo..
thanks in advance...
Hi Balus,
Great blog. One question regarding your "actionSearchReset()" in your crud.jsp example. To reset the search parameters, all you did was searchExample = new MyData();
However, when I try doing that the search parameters still remain. In order for me the clear those values, I had to set null to the submittedValue for those components.
Am I missing something here? Is there something else I need to do. I am new to JSF and I get really confused as to when you use the component i.e., HtmlInputText skuInput versus a simple getter/setter value in a POJO like private Long sku.
Your help would be much appreciated. Thanks again for the blog.
It is not clear to me if the problem in CRUD example or in your own webapp. At least the CRUD example works fine here.
With regard to component vs property: component binding is useful if you want to do a more than just getting/setting the value as bean property. In case of HtmlInputHidden, the value will survive the complete JSF cycle regardless of the outcome of validation and conversion. This is important to pass the value from request to request in a request scoped bean. This is also explained in the article's text. Just carefully read it and do not only focus on the code.
Thanks for the reply Balus.
I realized what I did wrong. For my clear button, I set the immediate attribute to true. When I set it back to false, the clear button now works like yours.
However, there's still one problem, which was the reason why I switched it to immediate equals true. The reason is this. If you enter a non-numeric value for Sku, JSF will do it's validation first. As a result, it will error out and not continue with the clear.
So what I did was, set the button action to immediate equals true. In the backing bean, I cleared out the submitted value for the parameter component I wanted to clear out.
In your opinion, what is the best way to implement a clear action where all the user wants to do is to clear out the input parameters regardless if there are any validation errors inside those parameters.
So in this case for search parameters, would it be better to map those parameters to an HtmlInputText component value versus to how you are doing it now? And then in the backing bean, you would have a clear method that would set the submitted value to null for those components? In the jsp, you would map the value to [component].value?
Thanks again.
The immediate="true" on the button (and not having it on the input components) only prevents the input values being set. When response is to be rendered, JSF unfortunately retrieves the input values from the components itself, not from the bean.
Validation and conversion in JSF is hard to skip, indeed. The control is only limited to the required attribute. Easiest way is to separate the reset button in its own h:form.
I have put checkboxes in a datatable using a HashMap, but it only works in session mode. Trouble is that my datatable depends on what I select from a selectOneMenu element, and I think that the selected value is applied too late in the JSF lifecycle so the checkboxes are not correctly bound to the HashMap. How could I keep my bean in request scope?
I managed to make it work inserting this in the bean's constructor:
HttpServletRequest request = (HttpServletRequest) FacesContext.getCurrentInstance().getExternalContext().getRequest();
selectedInstanceId = request.getParameter("logsform:selectedInstanceId");
It works but isn't there a better way to do this?
BalusC,
Thank you sooooo much for your article. It helped me a lot for the JSF work I am doing. It is the best article I have found online. Please don't remove it. People will refer to it again and again, I am sure.
Hi Baluc,
Greatest Artical, well described and much appreciated one .
It helped me to have sorting dataTables in my application.
Using your sorting method i am facing a problem . i have 'storageBean' named bean class and from that bean class i am getting 'nsList' as List to be represented on dataTable.Problem is that on header of the dataTable some one clicks it calls getNsList() twice rather once.
do you have any idea am i missing something which is causing this?
i might not provided you full information to understand the problem , please let me know what else you need to understand it .
Thanks in Advanced.
Bhargav.
First call is done to get original input values and the second call is done to display current values. That shouldn't harm. Getters are just there to return data. Read "Debug JSF lifecycle" article for more details.
hi, i tried to display my data from a database using an arraylist as a managed propertyand using a h:datatable but it repeats like
apple
orange
nut
apple
orange
nut
in the jsp page :S
what should i do now?
thanks in advance.
Sounds like you wrote data loading logic in the getter instead of the bean constructor! I suggest you to read the "Retrieve and store data" chapter.
hello,
thanks for urgent reply.
yes i wrote the load logic in getter method.
i will read that chapter.
thanks in advance.
hello again,
i carried the loading logic to the default constructor with has no parameters and now it works fine. thanks.
Hi, BalusC,
I tried to use your sortDataList approach to sort my table, but it didn't work, because if myBean is a request scope, the sortField is always set to null and the "if (sortField != null && sortField.equals(sortFieldAttribute))" is never invoked, and sortAscending is aways set to true in the "else" block, correct?
Can you tell me what I did wrong?
Likely you forgot the h:inputHidden elements. Check the code example once again.
BalusC,
You are probably right, I may have forgotten "h:inputHidden value="#{myBean.sortField}"
and "h:inputHidden value="#{myBean.sortAscending}" "
I will try again. Thank you so much!
Hi, BalusC,
Somehow the FacesContext.getCurrentInstance().getRenderResponse() never works for me to prevent duplicate data loading. If I use this condition in the getter, my data will never get loaded. I am using JSF 1.1. When I use the lazy loading in my getter, the call to the DB still gets executed multiple times during each request. This is the most annoying part of the JSF. I don't know what would be the best way of preventing it. Any advise would be greatly appreciated.
The problem lies somewhere else.
As to the data loading, the constructor/initblock is the best place, not the getter.
Namaskar Balus, Thanks for a wonderful article that has clearly withstood the test of the greatest opponent; time ! You are an inspiration.
Bhishma.
Hi Balau C,
When i tried to edit the valus in the data table and then save only the first row of the table changes alone getting reflected. Remaining rows are coming as it is.
Please find the code below,
dataTable
id="fr"
value="#{forecastTableBean.list}"
renderedIfEmpty="false"
var="Forecastlist"
bgcolor="#F1F1F1" border="10" cellpadding="5" cellspacing="3"
Please help me to fix this issue.
Editing/saving a datatable is already covered by this article.
I floowed the same approach but only the first row changes is getting reflected. I dono what changes needs to be done to make all the rows gets changed with new value.
When i print from FacesContext request parameter I am able to see all the values but datatable list is not getting updated with the new value.
please provide me some idea to fix this issue.
Thanks in advance.
regards
Senthil
Can you please post the same using rich:datatable, using f:setPropertyActionListener, a4j:support
with backing bean code.
This was really helpful but I am using rich:dataTable.
Great post, great blog.
Tried sorting my dataTable today.
Might there be a little mistake in your DTOComparator? Exchange o2 and o1 in last else-block of sort()?
All the best. I will definitely return to your page.
JSF>>>>>> h:dataTable and h:commandLink not working together
is there any solution for this i have tried some methods
like changing the "managed-bean-scope" to "session" but with little
improvement, the page refreshes itself,any solution, Suppose if i am
executing a querry its resultset has more than 1 result to return, is
this data table the real solution or i have look for some other
method(because i want to link this result(each result) to some other
page), Any Suggestion
but when i am using c:foreach i am not able to pass the value of the field to bean
How to call a action on page load in jsf
i am having a JSF page where i want to display the records from the datatable. I have a method in bean which call database and gets the list of records. If i call this method from a link (action on this link) present on this page i can display the records on this page.
But i want to display these records at the time of page load only. How we can do this? How to call a action at the time of page load?
Thanks in adv.
Use bean constructor or init block, as already mentioned in this article.
Hi,
this post was very helpful for me, but I have still one small problem, that drives me nuts.
In "select multiple rows using checkboxes. ":
I used the second solution, as I shouldn't change the Object.
No matter what I do I always get the values back as false.
So I check in the checkbox in a few rows. Click on the cmd button and get the Map back, but all the values are set to "false".
Do you have an idea, why doesn't that change?
thanks.
Either keys are non-existing or model value phase has been skipped for those values. Debug it.
Hi BalusC,
I am in need of creating dynamic datatables, which I've accomplished, with the help of this this post.
Also, I have some buttons, associated with the datatable.
Now, I need to copy - paste the entire datatable (with all its properties) on an action, which can be done as many times as possible.
Is there any method to copy the object which is already enclosed within a container (eg: panelgrid) and replicate it there itself ?
Thanks a lot for your great blog postings BalcusC. They have been a very helpful aid in learning JSF.
I ran into the request scope dataTable/commandLink issue others have here have bumped into where the action on the commandLink wasn't firing off. As you've pointed, it was a problem with the data loading logic.
In my case I had started with a session scoped bean and had the data loading call inside a switch that checked FacesContext.getCurrentInstance().getRenderResponse() to prevent running the query over and over. When I changed the bean scope to request I did not remove this switch and the command link action mysteriously "broke". Removing that check fixed the issue.
Thought I'd post this in case someone else goes down the same road I went.
FacesContext.getCurrentInstance().getRenderResponse() doesn't work for me for request scoped backing bean either.
Post a Comment