Display List<List> in <p:dataTable><p:columns> - jsf

I am trying to display below list of list of object in datatable. But nothing is showing up. Help is very much appreciated!
public class TimrsDisplayBean {
private static final long serialVersionUID = 1L;
private String teamName = "";
private String teamType = "";
private boolean reported;
private boolean noProd;
private boolean missing;
public String getTeamName() {
return teamName;
}
public void setTeamName(String teamName) {
this.teamName = teamName;
}
public String getTeamType() {
return teamType;
}
public void setTeamType(String teamType) {
this.teamType = teamType;
}
public boolean getReported() {
return reported;
}
public void setReported(boolean reported) {
this.reported = reported;
}
public boolean getNoProd() {
return noProd;
}
public void setNoProd(boolean noProd) {
this.noProd = noProd;
}
public boolean getMissing() {
return missing;
}
public void setMissing(boolean missing) {
this.missing = missing;
}
}
XHTML FILE
<p:dataTable value="#{dashboardMBean.timrsDisplayDataList}" var="var" rowIndexVar="row"
styleClass="large-card-datatable alternatingRowTable no-border nowrap">
<f:facet name="header">
<span class="updateDate"> </span>
</f:facet>
<p:column headerText="Type" value=" #{dashboardMBean.timrsDisplayDataList[0]}" columnIndexVar="i">
#{var[i].teamType}
</p:column>
<p:column headerText="Type" value=" #{dashboardMBean.timrsDisplayDataList[0]}" columnIndexVar="i">
#{var[i].teamName}
</p:column>

Please support full code parts if possible. Your datatable tag doesn't even finish.
Beside that, it seems like you've done too much work. Did you try to calculate the row indices by yourself? Not needed. The basic column definiton is more comfortable. Use your defined variable var to name each element/row as shown in the Primefaces showcase:
http://www.primefaces.org/showcase/ui/data/datatable/basic.xhtml
In your case, it will be something like
<p:column headerText="Type">
<h:outputText value="#{var.teamType}"/>
</p:column>
Plus, as Jasper De Vries said correctly, there is no attribute called columnIndexVar. Remove it to prevent strange behaviour.
If this is not enough, you need to share some more code of your ManagedBean. Not sure if your posted java class represent your Bean, but if so, you need to declare it as a ManagedBean like
#ManagedBean(name = "timrsDisplayBean)
#SessionScoped
public class TimrsDisplayBean {
I hope this helps!

Related

How to update p:dataTable in p:rowExpansion

In my project, I have intertwined 2 datatables. I want to update second datatable when first datatable's rows expanded.
<h:form id="form2">
<p:dataTable var="modules" value="#{EVMView.allModules}" id="mydatatable">
<f:facet name="header">
Modules
</f:facet>
<p:column style="width:2rem">
<p:rowToggler/>
</p:column>
<p:column headerText="Module">
<h:outputText value="#{modules}"/>
</p:column>
<p:rowExpansion>
<div class="product">
<p:dataTable var="questions" value="#{EVMView.allQuestions}" id="mydatatable2">
<p:column style="width:2rem">
<p:rowToggler/>
</p:column>
<p:column headerText="Question">
<h:outputText value="#{questions}"/>
</p:column>
<p:rowExpansion>
</p:rowExpansion>
</p:dataTable>
</div>
</p:rowExpansion>
</p:dataTable>
</h:form>
When I expand mydatatable's row, I don't see the values in mydatatable2.
So I know I have to update mydatatable2 too, but I couldn't find how to do it.
EDİT
According to #Jasper's answer, I added ViewScoped in my bean and I added this ajax in my first datatable.
<p:ajax event="rowToggle" listener="#{EVMView.updateTable2()}"/>
My bean is like this
package com.utc.pw.ui;
import java.util.List;
import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import org.primefaces.PrimeFaces;
import com.utc.pw.domain.EVMOptions;
import com.utc.pw.domain.EVMQuestions;
import com.utc.pw.domain.SalesOrder;
import com.utc.pw.service.EVMService;
import com.utc.pw.service.PublicService;
#ManagedBean(name="EVMView")
#ViewScoped
public class EVMView {
#EJB
private PublicService publicService;
#EJB
private EVMService evmService;
private EVMQuestions evmQuestions=new EVMQuestions();
private String strSO;
private List<SalesOrder> salesOrders;
private List<String> allModules;
private List<EVMQuestions> allQuestions;
private String selectedOption;
private List<EVMOptions> optionList;
private String answer;
public String getAnswer() {
return answer;
}
public void setAnswer(String answer) {
this.answer = answer;
}
public List<EVMOptions> getOptionList() {
return optionList;
}
public void setOptionList(List<EVMOptions> optionList) {
this.optionList = optionList;
}
public String getSelectedOption() {
return selectedOption;
}
public void setSelectedOption(String selectedOption) {
this.selectedOption = selectedOption;
}
public List<EVMQuestions> getAllQuestions() {
return allQuestions;
}
public void setAllQuestions(List<EVMQuestions> allQuestions) {
this.allQuestions = allQuestions;
}
public PublicService getPublicService() {
return publicService;
}
public void setPublicService(PublicService publicService) {
this.publicService = publicService;
}
public EVMService getEvmService() {
return evmService;
}
public void setEvmService(EVMService evmService) {
this.evmService = evmService;
}
public EVMQuestions getEvmQuestions() {
return evmQuestions;
}
public void setEvmQuestions(EVMQuestions evmQuestions) {
this.evmQuestions = evmQuestions;
}
public String getStrSO() {
return strSO;
}
public void setStrSO(String strSO) {
this.strSO = strSO;
}
public List<SalesOrder> getSalesOrders() {
return salesOrders;
}
public void setSalesOrders(List<SalesOrder> salesOrders) {
this.salesOrders = salesOrders;
}
public List<String> getAllModules() {
return allModules;
}
public void setAllModules(List<String> allModules) {
this.allModules = allModules;
}
#PostConstruct
public void init() {
this.salesOrders = publicService.getAllSalesOrders();
}
public void loadModules()
{
this.allModules=evmService.getAllModules(strSO);
this.allQuestions=evmService.getAllQuestions(strSO);
PrimeFaces.current().ajax().update("form2:mydatatable");
//PrimeFaces.current().ajax().update("form3:mydatatable2");
}
public void updateTable2()
{
PrimeFaces.current().ajax().update(":mydatatable2");
}
public List<EVMOptions> options(String module,int questionID)
{
this.optionList=evmService.getOptions(strSO, module, questionID);
return optionList;
}
}
You don't need to update the table in the p:rowExpansion when it is expanded as it is lazy loaded by default. What you do need to do is make sure that you child p:dataTable value="#{...}" is set correctly. To do so, add a listener on the outer p:dataTable and set the model based on the expanded row. You will need to use the rowToggle event, which takes a ToggleEvent as the parameter.
As your table has multiple rows of modules, you should have model in your bean which can contain multiple modules, like a Map with the module ID as the key and the questions as the value. Add module entries in your rowToggle handler.
Also make sure your bean is at least ViewScoped.
See also:
https://primefaces.github.io/primefaces/10_0_0/#/components/ajaxbehavior
How to choose the right bean scope?

JSF ui repeat : Array index out of range: 0 [duplicate]

I would like to know if it possible to push a value from inside a <ui:repeat> to a map, a set or a list?
I would like to pass the value of the <h:inputtext> to a set.
Code:
<ui:repeat var="_par" value="#{cmsFilterParameterHandler.normaleSuchParameter()}">
<p:outputLabel value="#{_par.bezeichnung}" />
<p:spacer width="5px" />
<p:inputText id="me" value="#{??? push me to a set ???}"/>
<br /><br />
</ui:repeat>
With a Set, it is not possible as it doesn't allow referencing items by index or key. It's however possible with a List and a Map by just specifying the list index and map key in the input value.
With a List:
private List<String> list; // +getter (no setter necessary)
#PostConstruct
public void init() {
list = createAndFillItSomehow();
}
<ui:repeat value="#{bean.list}" varStatus="loop">
<h:inputText value="#{bean.list[loop.index]}" />
</ui:repeat>
With a Map (only if your environment supports EL 2.2 or JBoss EL):
private Map<String, String> map; // +getter (no setter necessary)
#PostConstruct
public void init() {
map = createAndFillItSomehow();
}
<ui:repeat value="#{bean.map.entrySet().toArray()}" var="entry">
<h:inputText value="#{bean.map[entry.key]}" />
</ui:repeat>
Noted should be that the canonical approach is to use a List of fullworthy javabeans. Let's assume a Javabean class named Par with properties id and value which maps exactly to a par table in DB with columns id and value:
private List<Par> pars; // +getter (no setter necessary)
#PostConstruct
public void init() {
pars = createAndFillItSomehow();
}
<ui:repeat value="#{bean.pars}" var="par">
<h:inputText value="#{par.value}" />
</ui:repeat>
Either way, it works as good when using <p:inputText>, it's in no way related to PrimeFaces, it's in the context of this question merely a jQuery based JSF UI component library. Just replace h: by p: to turn it on.
I'm not sure, if I understood your requirements correctly.
I suppose the following: You need a List of Strings in some backend and an ui:repeat tag to iterate over those strings with input-fields to edit them. Maybe there are some syntax-issues, but my idea should be clear:
public class Backend {
private List<String> myStrings;
public MyStringWrapper getMyStringWrapper(int index) {
return new MyStringWrapper(index);
}
public class MyStringWrapper {
private final int index;
public MyStringWrapper(int index) { this.index = index; }
public String getContent() { return myStrings.get(index); }
public void setContent(String newContent) { myStrings.add(index, newContent); }
}
}
In the frontend you use as follows:
<ui:repeat var="_index" value="#{backend.getIndexSequence()}">
<p:inputText value="#{backend.getMyStringWrapper(_index).content}"/>
</ui:repeat>
Of course, you have to provide a getIndexSequence-method which produces a list of ints ranging from 0 to the size of the strings.
Do you mean like this?
<p:inputText id="me" value="#{_par.input}"/>
in BackBean:
public class Par implements Serializable {
private String inputText;
private String bezeichnung;
public Par()
{
}
public void setInput(String input)
{
this.inputText = input;
}
public String getInput()
{
return this.inputText
}
public void setBezeichnung(String bezeichnung)
{
this.bezeichnung = bezeichnung;
}
public String getBezeichnung()
{
return this.bezeichnung
}
}

datamodel must implement when selection is enabled.?

I wanted to remove rows from the data table when the checkbox is ticked and remove button is pressed..
This is the datatable snippet :
<p:dataTable id="cartTable" lazy="true" scrollable="true"
scrollHeight="115" selection="#{Cart_Check.selectedItems}"
value="#{Cart_Check.cart}" var="cart" rowKey="#{cart.sheetno}"
style="widht:100%;margin-top:10%;margin-left:1%;margin-right:30px ;box-shadow: 10px 10px 25px #888888;">
<f:facet name="header">
Checkbox Based Selection
</f:facet>
<p:column selectionMode="multiple" style="width:2%">
</p:column>
//Here the columns are metion
<f:facet name="footer">
<p:commandButton id="viewButton" value="Remove" />
</f:facet>
</p:dataTable>
This is the backing bean
public class checkcart {
private int items;
private ArrayList<User_Cart> cart;
private ArrayList<User_Cart> selectedItems;
public checkcart() {
getvalues();
}
//getter and setter
public void getvalues() {
FacesContext context = FacesContext.getCurrentInstance();
HttpSession session = (HttpSession) context.getExternalContext()
.getSession(false);
System.out.println("Cart Request ::::" + session.getAttribute("regid"));
try {
Connection connection = BO_Connector.getConnection();
String sql = "Select * from cart_orderinfo where usrregno=?";
PreparedStatement ps = connection.prepareStatement(sql);
ps.setString(1, (String) session.getAttribute("regid"));
ResultSet rs = ps.executeQuery();
cart = new ArrayList<>();
while (rs.next()) {
User_Cart user_cart = new User_Cart();
user_cart.setSheetno(rs.getString("sheetno"));
user_cart.setState_cd(rs.getString("state_cd"));
user_cart.setDist_cd(rs.getString("dist_cd"));
user_cart.setLicensetype(rs.getString("license_type"));
user_cart.setFormat(rs.getString("sheet_format"));
user_cart.setQuantity(rs.getInt("quantity"));
cart.add(user_cart);
}
} catch (Exception ex) {
System.out.println(ex);
}
}
}
and when i run this page i get the following error
datamodel must implement org.primefaces.model.selectabledatamodel when selection is enabled.
But when i remove the checkbox then their is no error but it is without a checkbox.
What to do and how to resolve the following error ..Kindly help..
I want something like this :
http://www.primefaces.org/showcase/ui/datatableRowSelectionRadioCheckbox.jsf
You just need to define ListDataModel as shown below,
public class SD_User_Cart extends ListDataModel<User_Cart> implements SelectableDataModel<User_Cart> {
public SD_User_Cart() {
}
public SD_User_Cart(List<User_Cart> data) {
super(data);
}
#Override
public User_Cart getRowData(String rowKey) {
//In a real app, a more efficient way like a query by rowKey should be implemented to deal with huge data
List<User_Cart> rows = (List<User_Cart>) getWrappedData();
for (User_Cart row : rows) {
if (row.getCartId.toString().equals(rowKey)) {//CartId is the primary key of your User_Cart
return row;
}
}
return null;
}
#Override
public Object getRowKey(User_Cart row) {
return row.get.getCartId();
}
}
Change your "cart" object into SD_User_Cart as shown below,
private SD_User_Cart cart;
Then define selection in p:datatable, and add a column as shown below,
<p:column selectionMode="multiple" style="width:18px"/>
Hope this helps:)
You need to define a your private ArrayList<User_Cart> selectedItems; data member in back class public class checkcart like this private User_Cart[] selectedItems; and give setter and getter method for the same it will work.
I had also faced same problem.

How to set submitted values of <h:inputText> inside <ui:repeat> into map, set or list

I would like to know if it possible to push a value from inside a <ui:repeat> to a map, a set or a list?
I would like to pass the value of the <h:inputtext> to a set.
Code:
<ui:repeat var="_par" value="#{cmsFilterParameterHandler.normaleSuchParameter()}">
<p:outputLabel value="#{_par.bezeichnung}" />
<p:spacer width="5px" />
<p:inputText id="me" value="#{??? push me to a set ???}"/>
<br /><br />
</ui:repeat>
With a Set, it is not possible as it doesn't allow referencing items by index or key. It's however possible with a List and a Map by just specifying the list index and map key in the input value.
With a List:
private List<String> list; // +getter (no setter necessary)
#PostConstruct
public void init() {
list = createAndFillItSomehow();
}
<ui:repeat value="#{bean.list}" varStatus="loop">
<h:inputText value="#{bean.list[loop.index]}" />
</ui:repeat>
With a Map (only if your environment supports EL 2.2 or JBoss EL):
private Map<String, String> map; // +getter (no setter necessary)
#PostConstruct
public void init() {
map = createAndFillItSomehow();
}
<ui:repeat value="#{bean.map.entrySet().toArray()}" var="entry">
<h:inputText value="#{bean.map[entry.key]}" />
</ui:repeat>
Noted should be that the canonical approach is to use a List of fullworthy javabeans. Let's assume a Javabean class named Par with properties id and value which maps exactly to a par table in DB with columns id and value:
private List<Par> pars; // +getter (no setter necessary)
#PostConstruct
public void init() {
pars = createAndFillItSomehow();
}
<ui:repeat value="#{bean.pars}" var="par">
<h:inputText value="#{par.value}" />
</ui:repeat>
Either way, it works as good when using <p:inputText>, it's in no way related to PrimeFaces, it's in the context of this question merely a jQuery based JSF UI component library. Just replace h: by p: to turn it on.
I'm not sure, if I understood your requirements correctly.
I suppose the following: You need a List of Strings in some backend and an ui:repeat tag to iterate over those strings with input-fields to edit them. Maybe there are some syntax-issues, but my idea should be clear:
public class Backend {
private List<String> myStrings;
public MyStringWrapper getMyStringWrapper(int index) {
return new MyStringWrapper(index);
}
public class MyStringWrapper {
private final int index;
public MyStringWrapper(int index) { this.index = index; }
public String getContent() { return myStrings.get(index); }
public void setContent(String newContent) { myStrings.add(index, newContent); }
}
}
In the frontend you use as follows:
<ui:repeat var="_index" value="#{backend.getIndexSequence()}">
<p:inputText value="#{backend.getMyStringWrapper(_index).content}"/>
</ui:repeat>
Of course, you have to provide a getIndexSequence-method which produces a list of ints ranging from 0 to the size of the strings.
Do you mean like this?
<p:inputText id="me" value="#{_par.input}"/>
in BackBean:
public class Par implements Serializable {
private String inputText;
private String bezeichnung;
public Par()
{
}
public void setInput(String input)
{
this.inputText = input;
}
public String getInput()
{
return this.inputText
}
public void setBezeichnung(String bezeichnung)
{
this.bezeichnung = bezeichnung;
}
public String getBezeichnung()
{
return this.bezeichnung
}
}

DataModel must implement org.primefaces.model.SelectableDataModel when selection is enabled.

I'm trying to create a DataTable with Multiple Row Selection but i'm getting an error here's the link of the tutorial http://www.primefaces.org/showcase/ui/datatableRowSelectionMultiple.jsf :
Here's my xhtml:
<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}"
var="userObj"
selection="#
{projectAdminisrationMB.selectedUsers}"
selectionMode="multiple" rowIndexVar="rowIndex"binding="#{table2}">
<p:column id="column3">
<f:facet name="header">
<h:outputText value=" user "></h:outputText>
</f:facet>
<h:outputText value="#{userObj.name}"/>
/
<h:outputText value="#{userObj.lastName}"></h:outputText>
<h:outputText value="#{userObj.firstName}"></h:outputText>
</p:column>
<f:facet name="footer">
<p:commandButton id="addProjectUser" value=" Add " onclick="dlg1.show()" />
<p:commandButton id="deleteProjectUser" value=" Delete " />
</f:facet>
</p:dataTable>
Managed Bean :
#ManagedBean
#SessionScoped
public class ProjectAdminisrationMB implements Serializable {
private static final long serialVersionUID = 1L;
private String projectName;
private List <User> projectUsersList;
private List<User> projectNoUsersList;
private List<User> selectedUsers;
private String projectAdmin;
public ProjectAdminisrationMB() {
super();
AdministrationProjectFinal administrationProjectFinal =new
AdministrationProjectFinal();
this.projectUsersList=administrationProjectFinal.getUserList();
this.projectNoUsersList=administrationProjectFinal.getNotUserList();
}
public String getProjectName() {
return projectName;
}
public void setProjectName(String projectName) {
this.projectName = projectName;
}
public List<User> getProjectUsersList() {
return projectUsersList;
}
public void setProjectUsersList(List<User> projectUsersList) {
this.projectUsersList = projectUsersList;
}
public String getProjectAdmin() {
return projectAdmin;
}
public void setProjectAdmin(String projectAdmin) {
this.projectAdmin = projectAdmin;
}
public List<User> getProjectNoUsersList() {
return projectNoUsersList;
}
public void setProjectNoUsersList(List<User> projectNoUsersList) {
this.projectNoUsersList = projectNoUsersList;
}
public List<User> getSelectedUsers() {
return selectedUsers;
}
public void setSelectedUsers(List<User> selectedUsers) {
this.selectedUsers = selectedUsers;
}
}
i'm getting this error:
javax.faces.FacesException: DataModel must implement
org.primefaces.model.SelectableDataModel when selection is enabled.....
just add this attribute rowKey to the datatable tag :
<p:dataTable border="1" value="#{projectAdminisrationMB.projectNoUsersList}"
var="userObj"
rowKey="#{userObj.name}"selection="#{projectAdminisrationMB.selectedUsers}"
selectionMode="multiple" rowIndexVar="rowIndex"
binding="#{table2}">
You can get this error if you try to add a new item to the underlying list and forget to assign a value to that new item's rowKey.
Alternatively to rowKey you can wrap your data in a custom model which really implements org.primefaces.model.SelectableDataModel. This is helpful if
all of your your classes have the same kind of #Id (e.g. a long) and can implement the same interface (e.g. EjbWithId)
you want to add additional functionalities to your data which are not domain specific and don't belong e.g. User.
The interface may be something like this:
public interface EjbWithId
{
public long getId();
public void setId(long id);
}
Then a generic implementation of SelectableDataModel for all your classes can be used:
public class PrimefacesEjbIdDataModel <T extends EjbWithId>
extends ListDataModel<T> implements SelectableDataModel<T>
{
public PrimefacesEjbIdDataModel(List<T> data)
{
super(data);
}
#Override public T getRowData(String rowKey)
{
List<T> list = (List<T>) getWrappedData();
for(T ejb : list)
{
if(ejb.getId()==(new Integer(rowKey))){return ejb;}
}
return null;
}
#Override public Object getRowKey(T item) {return item.getId();}
}
In your #ManagedBean:
private PrimefacesEjbIdDataModel<User> dmUser; //+getter
dmUser = new PrimefacesEjbIdDataModel<User>(administrationProjectFinal.getUserList());
first check whether you've added
rowKey="#{userObj.id}"
then you need to have the data table List set in filteredValue attribute of your data table in xhtml, instead of value.

Resources