row id of jsf datatable - jsf

I want to do some opertaions in the row of my JSF datatable, and for that i need and id of row. But when i see html generated through firebug. I notice that id is getting generated only for JSF components not of tr and td. Is there someway by which i can define the id of row(tr)
Here is my code
<h:datatable value = "#{bean.list}" var = "row">
<h:column>
#{row.name}
</h:column>
<h:column>
#{row.address}
</h:column>
<h:column>
#{row.phone no}
</h:column>
<h:column>
<h:selectBooleanCheckbox id="checkbox" value="#{row.sportsStudent}" />
</h:column>
</h:datatable>
There is a button below my datatable which says "Show only sports student" and my list should show only sports student and caption will change to "Show all students" . now all students should be displayed. So i was thinking if i have id of tr . I can just hide/show that row using jquery. So can i do that ?
Thanks in advance.
Tarun Madaan

You have several options. I can propose 2 of them:
You can add class with description to some column in datatable. E.g. for sports student class is sport and ordinary for others. And then you can hide/show all rows, which contains elements with ordinary class.
Filter students on server. Send ajax request which will rerender your table, leaving only sports student. You can do this pretty simple if you use some JSF library with ajax support, e.g. primefaces.

Related

JSF datatable/panel display on condition

I have a datatable I display with some value from database etc.
I only want to render the datatable if there is values to display, if not maybe display a message saying there is no values.
I know you can add the datatable to a panelgroup, example:
<h:panelGroup rendered="#{myList > 0}">
Is there a better way of doing this? how would i display the message if the datatable is not rendered?
using jsf 2.2 and richfaces 4.
thanks
You can use opposite conditions for rendering message or result table. For example:
<h:outputText value="No payment receipt to display"
rendered="#{paymentReceiptList.size == 0}"/>
<rich:dataTable id="paymentReceiptTable" var="receipt"
value="#{paymentReceiptList}" rendered="#{paymentReceiptList.size > 0}" >
Or you can display "No result found" info inside rich:dataTable using:
<f:facet name="noData">
<h:outputText value="#{msg.noData}" />
</f:facet>
Or you can display your message inside rich:dataTable using noDataLabel attribute of rich:dataTable.

Two buttons ( that generate inputtexts when clicked ) don't work on the same Facelet

I have two buttons that generate inputtexts when clicked. The problem is, when these two buttons are on the same xhtml, wichever is the first one, it is the only one that works.
If you need, here is an explanation on the Code 1 below:
It represents two inputtexts to get the name
and email of the author of a Document. moreAuthors represents
the method getMoreAuthors() that belongs to the Managed Bean inserirBean.
getMoreAuthors() returns a List<AuthorBean>.
The List<AuthorBean> is a property from the Managed Bean inserirBean.
The class AuthorBean creates the instances of Author, it has a property called
Author author.
AddAuthor() adds one more AuthorBean to the List<AuthorBean> in
Managed Bean inserirBean.
If you need, here is an explanation on the Code 2 below:
It follows the same structure from Code 1. Here, there's only one
inputtext that is used to get a subject of a Document.
moreSubjects returns a List<SubjectBean>. The class SubjectBean
creates the instances of the class Subjects.
SubjectBean has a property Subject sub,
and Subject has a property called String subject.
AddSubject() adds one more SubjectBean to the List<SubjectBean> in
Managed Bean inserirBean.
Code 1- If this is the only one on the xhtml, it works:
<h:form >
<h:dataTable value="#{inserirBean.moreAuthors}" var="authorBean">
<h:column>
Nome do Autor:
<h:inputText value="#{authorBean.author.name}" />
</h:column>
<h:column>
Email do Autor:
<h:inputText value="#{authorBean.author.email}" />
</h:column>
<f:facet name="footer">
<h:panelGroup>
<h:commandButton value="+" action="#{inserirBean.addAuthor()}" />
</h:panelGroup>
</f:facet>
</h:dataTable>
</h:form>
2- If this is the only one on the xhtml, it works too:
<h:form >
<h:dataTable value="#{inserirBean.moreSubjects}" var="subjectBean">
<h:column>
Palavras-chave:
<h:inputText value="#{subjectBean.sub.subject}" />
</h:column>
<f:facet name="footer">
<h:panelGroup>
<h:commandButton value="+" action="#{inserirBean.addSubject}" />
</h:panelGroup>
</f:facet>
</h:dataTable>
</h:form>
BUT, if I put these two blocks on the same xhtml, the one that appears first on the browser, always works, and the second, never - and I've already tried to change their position, but whichever I put first place on screen is the one that works.
What's causing this problem?
Thank you!
I just put the two datatables in code 1 and 2 inside the same <form> block and both worked.

jsf tabledata dynamic inputtext

I have a List in my backing bean. I want to present it in a table. I want table to have two rows - on left side, values from the list, and on the right side inputBoxes. It's the easy part. Here's my code:
<div class="table">
<h:dataTable id="korekty" styleClass="table table-hover"
value="#{searchBean.listX}" var="v">
<h:column>
<f:facet name="header">XXX</f:facet>
#{v.string}
</h:column>
<h:column>
<f:facet name="header">YYY</f:facet>
<h:inputText styleClass="form-control">
</h:inputText>
</h:column>
</h:dataTable>
</div>
The hard part of my problem is : in those inputtext user might put some numbers. Then after hitting SUBMIT button I want to create a list/map/whatever of inputed values. How can I do that? Thanks for help
First of all, prepare an array, or a list, containing placeholders for the given data alongside the list you've got:
List<String> initialData = ...;//initial data of n size
String[] submittedData = new String[initialData.size()];//array of the same size
Then, bind the <h:dataTable> component to the view to get access to the current row index of the iteration. This way you can finish your job by binding value of input elements to the corresponding objects in the array/list:
<h:dataTable binding="#{table}" ... >
<h:column>
<f:facet name="header">YYY</f:facet>
<h:inputText value="#{searchBean.submittedData[table.rowIndex]}" ... />
</h:column>
</h:dataTable>

Generate unique id for h:dataTable in the loop

I am using JSF 2.0. When I am trying to use a variable in the 'id' attribute of the h:dataTable its not taking that variable value.
I have h:dataTable inside ui:repeat with id values as index of ui:repeat as mentioned below.
<ui:repeat var="planMap" value="#{planAccountMap.entrySet().toArray()}" varStatus="planMapStatus">
<h:dataTable id="planTable#{planMapStatus.index}" value="#{planMap.value}" var="accountList">
Does any one know whether we can have dynamic id generated for h:dataTable in loop?
I have a Java script which is making use of table id, but as I am not able to have the unique id for each table in the loop, its breaking.
Any component inside of a repeatable component will always have a dynamic client id. You cannot do this.
Perhaps instead you can assign a unique style class to the dataTable component and use a jQuery selector to return an array of all the dataTable objects in the ui:repeat.
jQuery('.SomeClass');
I do some thing very similar to you. i have a UI:repeat inside a datatable column. tbh, if you check in fire bug, your datatable id would be unique as you put it inside a ui:repaet.
planMap:0:yourdatatableID
This problem can be solve by using datatable inside datatable. Second datatable is kept inside the column of the first datatable as below.
<h:dataTable id="outerTable" value="#{planAccountMap.entrySet().toArray()}" var="planMap" width="100%">
<h:column>
<h:dataTable id="planTable" value="#{planMap.value}" var="accountList"
headerClass="tablehead"
rowClasses="'',altrowcoloropt1"
first="0">
<h:column>
<f:facet name="header">
<h:outputText value="Date" />
</f:facet>
<h:outputText value="#{accountList.date}"/>
</h:column>
<h:column>
<f:facet name="header" >
<h:outputText value="Account Name" />
</f:facet>
<h:outputText value="#{accountList.accountName}"/>
</h:column>
</h:dataTable>
</h:column>
</h:dataTable>
By this you would get unique id as outerTable:0:planTable, outerTable:1:planTable.This even extend the unique id to each element in datatable as outerTable:1:planTable:1:columnid , outerTable:0:planTable:0:columnid

How to set row level rendering in JSF <h:datatable>

How can i set row level rendering in datatable JSF.
<h:dataTable styleClass="tablesub" border="0" value="#{historyQuestBean.answerMasterList[row].inputTextKeySet}" var="option">
<h:column>
<h:outputText value="#{option.sectionShortName}:"/>
</h:column>
<h:column>
<h:outputText value="#{option.type}:"/>
</h:column>
</h:dataTable>
I want to render only those rows who have status true.
How can I do this?
Easier and Simplest thing is to pass the list of values with status = true
In <h:datatable> you can give a style class within rowClasses say renderer and in that class specify a condition like display:#{option.status==true}?'block':'none'. This will evaluate the EL and accordingly place the style of that <tr\> to display or not.
Another option is to use <ui:repeat> instead of <h:datatable>, Here you can place rendered condition for the <tr>.

Resources