This week's book giveaway is in the OCAJP 8 forum.
We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line!
See this thread for details.
The moose likes JSF and the fly likes DataModel in Jsf Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of OCA Java SE 8 Programmer I Study Guide this week in the OCAJP 8 forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "DataModel in Jsf" Watch "DataModel in Jsf" New topic

DataModel in Jsf

Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55

Hi All,

How can I refer to a column in DataModel object?
Rok Koncina

Joined: Nov 30, 2011
Posts: 5

DataModel represents only rows. Each row is an entry, usually an instance of a class or array of data.
Column represents certain data (usually field) of every entry.

What do you want to do with a column?
Do you want to refer to the whole column or just one column in one row?
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17160

You cannot refer to a DataModel column as a unit, because the major dimension for dataModel is row, nor column.

However an individual cell within a DataModel row is accessed as a property of the row object.

An IDE is no substitute for an Intelligent Developer.
Nipun Bahr
Ranch Hand

Joined: Jul 09, 2008
Posts: 55

Thanks Rok and Tim for the reply.

Rok I want to refer to a column in the row.
David Cheng-Hk

Joined: Dec 02, 2011
Posts: 2
Define your own data table like this:

DataColumn : class represents a single column in a rowset
DataRow: ArrayList<DataColumn> will represent a rowset
DataTable: ArrayList<DataRow> will represent a list of rowset, i.e. a data table

in each class, have those getter/setter ready to provide access to each column/row

assuming you have MyFaces Tomahawk:
then bind the <t:dataTable> to the datatable implementation in your backing bean
<t:dataTable id="mydataTable" border="1" cellspacing="5"
binding="#{myBean.myDataTable}" var="myDT"
rowStyleClass="#{(rowIndex % 2 == 1 ? 'odd' : 'even')}">
<!-- first column -->
<!-- header of first column -->
<f:facet name="header">
<t:selectBooleanCheckbox id="cbxSelectAll"
onclick="selectAll(this, 'cbxChecked')" />
<!-- value of first column -->
<t:selectBooleanCheckbox id="cbxChecked"
value="#{myBean.selectedIds[myDT.myColumnName]}" />
<!-- 2nd column -->
<!-- header of 2nd column -->
<f:facet name="header">
<hutputText value="2nd Column Header" />
<!-- value of 2nd column -->
<hutputText value="#{myDT.myColumnName}" />

in the backing bean:
HtmlSelectBooleanCheckbox cbxSelectAll; to keep track if the "select all" checkbox has been checked
Map<Integer, Boolean> selectedIds; Map to keep track which item has been selected
HtmlDataTable myDataTable = new HtmlDataTable();
I agree. Here's the link:
subject: DataModel in Jsf
It's not a secret anymore!