How do you get row numbers to show in a <h:dataTable>? - jsf

I have the following JSF datatable:
<h:dataTable border="1" var="f2" value="#{someEJB.list}">
<h:column>
<f:facet name="header">No</f:facet>
</h:column>
<h:column>
<f:facet name="header">Name</f:facet>#{f2.name}
</h:column>
</h:dataTable>
How can I show the row numbers?

You can use UIData#getRowIndex() for this.
<h:dataTable binding="#{table}" ...>
<h:column>
#{table.rowIndex + 1}
</h:column>
</h:dataTable>

Related

Using inputRowSelect in a dynamic dataTable

Inside the dataTable dynamically creates a dataTable with the records, how can I get the number of rows for each dataTable that was created?
My Table:
<h:dataTable value="#{bean.tableId}"
var="tableIdInfo" rows="10">
<h:column>
<f:facet name="header">
<h:outputText value="id_date"></h:outputText>
</f:facet>
<h:outputText value="#{tableIdInfo.id_date}"
styleClass="outputText">
<hx:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
</h:column>
<h:column>
<h:dataTable value="#{bean.tableList}" var="tInfo">
<h:column>
<f:facet name="header">
</f:facet>
<hx:inputRowSelect value="#{bean.rows}">
</hx:inputRowSelect>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText styleClass="outputText"
value="Date create"></h:outputText>
</f:facet>
<h:outputText value="#{tInfo.cDate}">
<hx:convertDateTime pattern="dd.MM.yyyy" />
</h:outputText>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
I assume tableId is a list of data.
then add a column for getting row numbers.
<h:column>
<!-- display currently selected row number -->
<f:facet name="header">No</f:facet>
#{bean.tableId.rowIndex + 1}
</h:column>

JSF Datatable duplication header

I'm new of JSF, i'm trying to create a datatable but have a problem with this!
This is my code:
JSF file (Datatable):
<h:form>
<h:dataTable value="#{hsinfo.tableContent}" var="o"
styleClass="table"
headerClass="table-header"
rowClasses="table-odd-row,table-even-row">
<h:column>
<f:facet name="header">SBD</f:facet>
<h:outputText value="#{o.sbd}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Họ Tên</f:facet>
<h:outputText value = "#{o.ten}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Ngày Sinh</f:facet>
<h:outputText value = "#{o.ngaysinh}"></h:outputText>
</h:column>
<h:column>
<f:facet name="header">Giới Tính</f:facet>
<h:outputText value = "#{o.gioitinh}"></h:outputText>
</h:column>
</h:dataTable>
</h:form>
I don't know why if i just use #{o.sbd}, the code have error, but if i use <h:outputText value="#{o.sbd}">, the header row is not appeared and the column name duplicated each row like this :

Unable to send mail from jsf using data table,

public void approveVacation() throws AddressException,MessagingException
String emplname=this.getEmployeeName();
session = FacesContext.getCurrentInstance().getExternalContext()
.getSessionMap();
VacationDTO vacation=new VacationDTO();
vacation.setLeaveId(this.getLeaveId());
vacation.setLeaveDescriptions(this.getLeaveDescriptions());
vacation.setLeaveStartDate(this.getLeaveStartDate());
vacation.setEmployee(getEmployeeService().getUserByEmployeeId(getEmployeeId()));
vacation.setIsApproved((byte)1);
getVacationService().approveVacation(vacation);
String to=vacation.getEmployee().getEmployeeEmailAddress();
String name=vacation.getEmployee().getEmployeeName();
getVacationService().approveVacation(vacation);
Util.sendMail(to, "Hi"+name,
"Your vacation have been approved");
And this is my Xhtml page where I am using List....
<h:form>
<h:dataTable headerClass="header" rowClasses="oddRow,evenRow" value="#{approveVacationBean.vacationList}"
var="list">
<h:column>
<f:facet name="header">Employee Id</f:facet>
#{list.employee.employeeId}
</h:column>
<h:column>
<f:facet name="header">Employee Name</f:facet>
#{list.employee.employeeName}
</h:column>
<h:column>
<f:facet name="header">Leave Applied</f:facet>
#{list.leaveStartDate}
</h:column>
<h:column>
<f:facet name="header">Leave Start Date</f:facet>
#{list.leaveStartDate}
</h:column>
<h:column>
<f:facet name="header">Leave End Date</f:facet>
#{list.leaveEndDate}
</h:column>
<h:column>
<f:facet name="header">No of days</f:facet>
#{list.leaveEndDate}
</h:column>
<h:column>
<f:facet name="header">Paid Leave Balence</f:facet>
#{list.leaveBalance}
</h:column>
<h:column>
<f:facet name="header">Leave Description</f:facet>
#{list.leaveDescriptions}
</h:column>
<h:column>
<f:facet name="header">Comment</f:facet>
<h:inputTextarea ></h:inputTextarea>
</h:column>
<h:column>
<f:facet name="header"></f:facet>
<h:commandButton value="Approve"/>
<h:commandButton value="Reject"/>
</h:column>
</h:dataTable>
I am getting problem in selectiong particular row button.
Many times serive method called...I am using getVacationList() for displaying the list but no mail send to particular employee
First of all, place your approveVacation method in the approveVacationBean.
Then change the method to accept the list as a parameter (I guess it is VacationDTO).
Finally you should link the commandButton with the approveVacation method:
<h:commandButton value="Approve" ajax="false" action="#{approveVacationBean.approveVacation(list)}"/>
Your approveVacation method should look like this:
public void approveVacation(VacationDTO vacation) throws AddressException,MessagingException {
vacation.setIsApproved((byte)1);
getVacationService().approveVacation(vacation);
String to=vacation.getEmployee().getEmployeeEmailAddress();
String name=vacation.getEmployee().getEmployeeName();
getVacationService().approveVacation(vacation);
Util.sendMail(to, "Hi"+name,
"Your vacation have been approved");
}

Why does <h:column><f:facet name="header"><h:outputText> render as th instead of td

I have below code:
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<f:facet name="header">
<h:outputText value="Name"/>
</f:facet>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Outstanding"/>
</f:facet>
</h:column>
</h:dataTable>
But it prints Name and Outstanding between <th></th> tags. Why? I need them in <td></td> tags. How can I do that?
Name and Outstanding end up in <th> elements because you placed them inside <f:facet name="header"> tags. Remove those and they'll be output to <td> tags.
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<h:outputText value="Name"/>
</h:column>
<h:column>
<h:outputText value="Outstanding"/>
</h:column>
</h:dataTable>
See the JSF HTML Tag Reference for an example on creating a table.
You have to change your code into this:
<h:dataTable class="pretty" value="#{ftManagedBean.ftDataModel}" >
<h:column>
<h:outputText value="Name"/>
</h:column>
<h:column>
<h:outputText value="Outstanding"/>
</h:column>
</h:dataTable>
If you wrap your output text within the <f:facet name="header"> this corresponds to <th> (table header), otherwise they will be printed in <td>.
I try this solution and ok.
(http://www.coderanch.com/t/431222/JSF/java/dynamically-set-panel-header-condition)
<rich:dataGrid value="#{myMB.student.list}" rendered="#{!empty myMB.student and !empty myMB.student.list}" var="st" rowKeyVar="row">
<rich:panel>
<f:facet name="header">
<h:panelGroup id="panelHeader">
<h:outputText value="Just one student" id="header1" required="true" rendered="#{!myMB.manyStudents}"/>
<h:outputText value="#{row+1}º Student" id="header2" required="true" rendered="#{myMB.manyStudents}"/>
</h:panelGroup>
</f:facet>
<rich:panel>

How can i transfer a column value to my managed bean?

I am using a datatable in JSF:
<h:dataTable var="dataItem" value="#{operationsBean.creneauxMedecin}" border="1">
<f:facet name="header">
<h:outputText value="Rendez-vous de #{operationsBean.medecin.titre} #{operationsBean.medecin.prenom} #{operationsBean.medecin.nom} le #{operationsBean.txtJour}"></h:outputText>
</f:facet>
<h:column>
<f:facet name="header">
<h:outputText value="Créneau horaire"></h:outputText>
</f:facet>
<h:outputText id="id" value="#{dataItem.hdebut}h#{dataItem.mdebut}-#{dataItem.hfin}h#{dataItem.mfin}"/>
</h:column>
<h:column>
<f:facet name="header">
<h:outputText value="Client"/>
</f:facet>
<h:outputText value="#{operationsBean.clt }"/>
</h:column>
</h:dataTable>
Please how can i transfer a column's(((dataItem.id))) value to my managed bean ?
N.B dataItem is a datatable row that contains(id,version,hdebut,mdebut....)
You have to implement an action listener method in your managed bean and call that via a command link and pass a param as part of the tag, if you google you can see many examples.
<h:commandLink value="click me" actionListener="#{managedbean.actionListenerMethod}">
<f:param name="paramInternalId" value="#{dataTableIter.id}" />
</h:commandLink>
http://docs.oracle.com/javaee/5/tutorial/doc/bnaqd.html#bnaqj

Resources