I am using PrimeFaces DataTable with Radiobuttons inside a one wizard tab. Is possible to somehow set Radiobuttons like required?
User shouldn't go to the next wizard tab until he will choose one option in DataTable with Radiobuttons.
Or have you any ideas how to resolve this problem? Thanks for any replies!
JSP page
<p:tab id="test" title="Test">
<p:panel header="Term page">
<p:dataTable id="collection" value="#{register.dataList}" var="dl" rowKey="#{dl.c_id}" selection="#{register.selectedTerm}"">
<p:column selectionMode="single" style="width:2%" />
<p:column>
#{dl.c_id}
</p:column>
</p:dataTable>
</p:panel>
</p:tab>
You can check for data selection in flowListener of <p:wizard> tag that is triggered when next/previous buttons are clicked and conditionally add FacesMessage:
public String onFlowProcess(FlowEvent event) {
String current = event.getOldStep();
String next = event.getNewStep();
boolean proceed = true;
if(current.equals("first") && next.equals("second") && (selectedData == null)) {
//proceed only when data was selected and user is moving to the next step
FacesMessage facesMessage = new FacesMessage("You need to make a selection in a datatable to proceed!");
FacesContext.getCurrentInstance().addMessage("form:selection", facesMessage);
proceed = false;
}
return proceed ? next : current;
}
The full example is provided below.
The view:
<h:form id="form">
<p:wizard widgetVar="wiz" flowListener="#{q16439053Bean.onFlowProcess}">
<p:tab id="first" title="First">
<p:message for="selection"/>
<p:panel id="selection" header="Term page">
<p:dataTable id="collection" value="#{q16439053Bean.list}" var="data" rowKey="#{data.name}" selection="#{q16439053Bean.selectedData}">
<p:column selectionMode="single" style="width:2%" />
<p:column>
#{data.name}
</p:column>
</p:dataTable>
</p:panel>
</p:tab>
<p:tab id="second" title="Second">
Done!
</p:tab>
</p:wizard>
</h:form>
The bean:
#ManagedBean
#ViewScoped
public class Q16439053Bean implements Serializable {
private List<Data> list;
private Data selectedData;
public List<Data> getList() {
return list;
}
public void setList(List<Data> list) {
this.list = list;
}
public Data getSelectedData() {
return selectedData;
}
public void setSelectedData(Data selectedData) {
this.selectedData = selectedData;
}
public class Data {
private String name;
private String value;
public Data() {
}
public Data(String name, String value) {
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
}
public Q16439053Bean() {
list = new ArrayList<Data>();
Data d;
d = new Data("name", "value");
list.add(d);
d = new Data("name1", "value1");
list.add(d);
d = new Data("name2", "value2");
list.add(d);
d = new Data("name3", "value3");
list.add(d);
}
public String onFlowProcess(FlowEvent event) {
String current = event.getOldStep();
String next = event.getNewStep();
boolean proceed = true;
if(current.equals("first") && next.equals("second") && (selectedData == null)) {
FacesMessage facesMessage = new FacesMessage("You need to make a selection in a datatable to proceed!");
FacesContext.getCurrentInstance().addMessage("form:selection", facesMessage);
proceed = false;
}
return proceed ? next : current;
}
}
Related
Hi community I have a query with radio button selection in datatable of primefaces 5, when I click to register and invoke the event to edit, not capturing the registry value returns me null object.
My code xhtml:
<p:dataTable id="tblProductos" var="producto"
value="#{cBusquedaProducto.lazyDataProductos}"
rowIndexVar="rowIndex" lazy="true" rows="10"
paginator="true" paginatorPosition="top"
rowKey="#{producto.icodProducto}"
selectionMode="single"
selection="#{cBusquedaProducto.producto}"
emptyMessage="#{msg['tabla.noExistenRegistros']}"
paginatorTemplate="{CurrentPageReport} {FirstPageLink} {PreviousPageLink} {PageLinks} {NextPageLink} {LastPageLink} {RowsPerPageDropdown}"
currentPageReportTemplate="Registros: {totalRecords} - [ {currentPage} de {totalPages} ]"
rowsPerPageTemplate="10,20,30,40,50,200">
<p:column selectionMode="single" />
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="#{msg['form.item']}" />
</f:facet>
<h:outputText value="#{rowIndex + 1}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="#{msg['form.codigo']}" />
</f:facet>
<h:outputText value="#{producto.icodProducto}" />
</p:column>
<p:column style="text-align: center;">
<f:facet name="header">
<h:outputText value="#{msg['form.descripcion']}" />
</f:facet>
<h:outputText value="#{producto.vdesProducto}" />
</p:column>
.......
</p:dataTable>
Controller:
#ManagedBean(name = "cBusquedaProducto")
#ViewScoped
public class CBusquedaProducto extends BaseController {
#ManagedProperty(value = "#{iProductoService}")
private IProductoService iProductoService;
private LazyDataModel<ProductoBean> lazyDataProductos;
#PostConstruct
#Override
public void inicializarObjetos() {
try {
/* Cargamos las listas */
Map<String, Serializable> filters = new HashMap<String, Serializable>();
filters.put("param.icodEmpresa", Constantes.ONE_INT);
this.lazyDataProductos = new ProductoLazyDataModel(iProductoService, filters);
this.opcProducto = Constantes.ONE_SHORT;
} catch (Exception e) {
LOGGER.error(getGenerarError(
Thread.currentThread().getStackTrace()[1].getMethodName(),
Constantes.NIVEL_APP_CONSTROLLER,
this.getClass().getName(), e.getMessage()));
}
}
... get and set
}
LazyDataModel:
public class ProductoLazyDataModel extends LazyDataModel<ProductoBean> {
private static final long serialVersionUID = 1L;
public Logger logger = LoggerFactory.getLogger(this.getClass());
private List<ProductoBean> datasource;
Map<String, Serializable> extFilters = new HashMap<String, Serializable>();
private int pageSize;
private int rowIndex;
private int rowCount;
private IProductoService iProductoService;
/**
* #param iProductoService - El servicio para obtener datos desde base de datos.
* #param extFilters - Los filtros a aplicar a las columnas, acepta nulo.
*/
public ProductoLazyDataModel(IProductoService iProductoService,
Map<String, Serializable> extFilters) {
this.iProductoService = iProductoService;
this.extFilters = extFilters;
}
#Override
public List<ProductoBean> load(int first, int pageSize, String sortField,
SortOrder sortOrder, Map<String, Object> filters) {
try {
SortCriteria sort = new SortCriteria();
if (sortField == null) {
sort.add("icodProducto", false);
} else {
if (sortOrder == SortOrder.ASCENDING) {
sort.add(sortField, true);
} else {
sort.add(sortField, false);
}
}
WrappedData<ProductoBean> wd = iProductoService.obtenerProductos(filters,
extFilters, sort, first, pageSize);
datasource = wd.getData();
setRowCount(wd.getRowCount().intValue());
} catch (Exception e) {
logger.error(e.toString());
}
return datasource;
}
#Override
public boolean isRowAvailable() {
if (datasource == null)
return false;
int index = rowIndex % pageSize;
return index >= 0 && index < datasource.size();
}
#Override
public Object getRowKey(ProductoBean gr) {
return gr.getIcodProducto();
}
#Override
public ProductoBean getRowData() {
if (datasource == null)
return null;
int index = rowIndex % pageSize;
if (index > datasource.size()) {
return null;
}
return datasource.get(index);
}
#Override
public ProductoBean getRowData(String rowKey) {
if (datasource == null)
return null;
for (ProductoBean gr : datasource) {
String cod = String.valueOf(gr.getIcodProducto());
if (cod.equals(rowKey)) {
return gr;
}
}
return null;
}
#Override
public void setPageSize(int pageSize) {
this.pageSize = pageSize;
}
#Override
public int getPageSize() {
return pageSize;
}
#Override
public int getRowIndex() {
return this.rowIndex;
}
#Override
public void setRowIndex(int rowIndex) {
this.rowIndex = rowIndex;
}
#Override
public void setRowCount(int rowCount) {
this.rowCount = rowCount;
}
#Override
public int getRowCount() {
return this.rowCount;
}
#SuppressWarnings("unchecked")
#Override
public void setWrappedData(Object list) {
this.datasource = (List<ProductoBean>) list;
}
#Override
public Object getWrappedData() {
return datasource;
}
}
Hope you can help me, because I find the reason, I've tried with chekbox and with that if it works but with radiobutton not working ....
My problem is that after I've selected a few items on the 1st page, if I paginate to another page and come back, my initial selections are not shown. I've tried to implement the SelectableDataModel as well as using the rowKey attribute but the problem persists.
This is my test bean:
#ManagedBean
#ViewScoped
public class MrBean {
private List<Item> chosenItems;
private LazyDataModel lazyModel;
#PostConstruct
public void prepareTest() {
this.lazyModel = new LazyItemDataModel();
}
public void countItems() {
System.out.println("TEST 3: chosenItems's size: " + chosenItems.size());
}
private class LazyItemDataModel extends LazyDataModel<Item> implements SelectableDataModel<Item> {
#Override
public Item getRowData(String rowKey) {
System.out.println("TEST 1: getRowData");
Iterator<Item> iter = ((List<Item>) this.getWrappedData()).iterator();
while (iter.hasNext()) {
Item item = iter.next();
if (item.getId().equals(rowKey)) {
return item;
}
}
return null;
}
#Override
public Object getRowKey(Item item) {
return item.getId();
}
#Override
public List<Item> load(int first, int pageSize, String sortField, SortOrder sortOrder, Map filters) {
System.out.println("TEST 2: load");
// Code to retrieve items from database
}
}
// Getters and Setters
}
This is my test page:
<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:p="http://primefaces.org/ui">
<h:head>
<title>Test page</title>
</h:head>
<h:body>
<h:form>
<p:dataTable id="itemTable" var="item" value="#{mrBean.items}" rows="5"
paginator="true" selection="#{mrBean.chosenItems}" lazy="true" >
<p:ajax event="rowSelectCheckbox" listener="mrBean.countItems" />
<p:column selectionMode="multiple" />
<p:column headerText="ID">
<h:outputText value="#{item.id}" />
</p:column>
<p:column headerText="Name">
<h:outputText value="#{item.name}" />
</p:column>
</p:dataTable>
</h:form>
</h:body>
</html>
I'd be very grateful if you could show me what I've done wrong here.
UPDATE: After I added more System.out.println("TEST") to the above code, I observed the following things:
On the console, every time I paginate, TEST 1: getRowData is always printed before TEST 2: load. As a consequence, I believe the method #LazyDataModel.getWrappedData() may return data from the old page. At first, I thought this method's goal was to retrieve the selected rows to highlight on the table. However, if this method is called before load, there's no way it can do the job right?
After I selected the 1st 2 items on the 1st page, on the console, I saw TEST 3: chosenItems's size: 2. If I paginate to the 2nd page and then back to the 1st page, the selections are lost as mentioned. However, if I continued to select another item, on the console, I saw TEST 3: chosenItems's size: 3. Obviously, the chosenItems list still kept my old selections but they're not rendered on the table.
In webPage just add a event for when page switch:
<p:ajax event="page" listener="#{listingBean.updateSelected()}" />
In the listingBean, just save the selected:
private List<Entity> selectedInstances;
private List<Entity> selectedInstancesSaved;
public List<Entity> getSelectedInstances()
{
return selectedInstancesSaved;
}
public void setSelectedInstances(List<Entity> selectedInstances)
{
this.selectedInstances = selectedInstances;
}
public void updateSelected()
{
if (selectedInstances != null && !selectedInstances.isEmpty()) {
for (Entity inst : lazyModel.getDatasource()) {
if (selectedInstances.contains(inst)) {
selectedInstancesSaved.add( inst);
} else {
selectedInstancesSaved.remove( inst);
}
}
}
}
This is because when SelectionFeature is decoded a new list is created.
And if table.getRowData(rowKeys[i]) (related to your LazyDataModel implementation) returns null your old selectıons in the previous page are gone.may try to solve it by changing your LazyDataModel implementation I didn't try these but take a look at this and this
Had the same problem and I think this solution is easier if you have a lot of different tables implementing LazyDataModel.
This is what I did: check if it is lazy first then add currently selected rows to the selectionList.
For primefaces 4.0
1)Override DataTableRenderer
In faces-config.xml
<render-kit>
<renderer>
<component-family>org.primefaces.component</component-family>
<renderer-type>org.primefaces.component.DataTableRenderer</renderer-type>
<renderer-class>com.package.LazyDataTableRenderer</renderer-class>
</renderer>
</render-kit>
And
public class LazyDataTableRenderer extends DataTableRenderer {
static Map<DataTableFeatureKey,DataTableFeature> FEATURES;
static {
FEATURES = new HashMap<DataTableFeatureKey,DataTableFeature>();
FEATURES.put(DataTableFeatureKey.DRAGGABLE_COLUMNS, new DraggableColumnsFeature());
FEATURES.put(DataTableFeatureKey.FILTER, new FilterFeature());
FEATURES.put(DataTableFeatureKey.PAGE, new PageFeature());
FEATURES.put(DataTableFeatureKey.SORT, new SortFeature());
FEATURES.put(DataTableFeatureKey.RESIZABLE_COLUMNS, new ResizableColumnsFeature());
FEATURES.put(DataTableFeatureKey.SELECT, new LazySelectionFeature());
FEATURES.put(DataTableFeatureKey.ROW_EDIT, new RowEditFeature());
FEATURES.put(DataTableFeatureKey.CELL_EDIT, new CellEditFeature());
FEATURES.put(DataTableFeatureKey.ROW_EXPAND, new RowExpandFeature());
FEATURES.put(DataTableFeatureKey.SCROLL, new ScrollFeature());
}
#Override
public void decode(FacesContext context, UIComponent component) {
DataTable table = (DataTable) component;
for(Iterator<DataTableFeature> it = FEATURES.values().iterator(); it.hasNext();) {
DataTableFeature feature = it.next();
if(feature.shouldDecode(context, table)) {
feature.decode(context, table);
}
}
decodeBehaviors(context, component);
}
}
2)Override SelectionFeature's decode
Updated: edited to allow deselecting
public class LazySelectionFeature extends org.primefaces.component.datatable.feature.SelectionFeature{
#Override
public void decode(FacesContext context, DataTable table) {
String clientId = table.getClientId(context);
Map<String,String> params = context.getExternalContext().getRequestParameterMap();
String selection = params.get(clientId + "_selection");
if(table.isSingleSelectionMode())
decodeSingleSelection(table, selection);
else
decodeMultipleSelection(context, table, selection);
}
void decodeSingleSelection(DataTable table, String selection) {
if(ComponentUtils.isValueBlank(selection))
table.setSelection(null);
else
table.setSelection(table.getRowData(selection));
}
void decodeMultipleSelection(FacesContext context, DataTable table, String selection) {
Class<?> clazz = table.getValueExpression("selection").getType(context.getELContext());
boolean isArray = clazz.isArray();
if(!isArray && !List.class.isAssignableFrom(clazz)) {
throw new FacesException("Multiple selection reference must be an Array or a List for datatable " + table.getClientId());
}
if(ComponentUtils.isValueBlank(selection)) {
if(isArray) {
table.setSelection(Array.newInstance(clazz.getComponentType(), 0));
}
else {
table.setSelection(new ArrayList<Object>());
}
}
else {
String[] rowKeys = selection.split(",");
List<Object> selectionList = new ArrayList<Object>();
boolean lazy=table.isLazy();
if (lazy) {
List<String> currentRowKeys = new ArrayList<String>(Arrays.asList(rowKeys));
if (table.getSelection() != null) {
List<Object> alreadySelected = (List<Object>) table.getSelection();
for (Object object : alreadySelected) {//For deselecting
Object rowKeyFromModel = table.getRowKeyFromModel(object);
if (currentRowKeys.contains(rowKeyFromModel)) {
selectionList.add(object);
currentRowKeys.remove(rowKeyFromModel);
}
}
}
for (String key : currentRowKeys) {//For selecting
Object rowData = table.getRowData(key);
if (rowData != null && !selectionList.contains(rowData)) {
selectionList.add(rowData);
}
}
}else{
for(int i = 0; i < rowKeys.length; i++) {
Object rowData = table.getRowData(rowKeys[i]);
if(rowData != null)
selectionList.add(rowData);
}
}
if(isArray) {
Object selectionArray = Array.newInstance(clazz.getComponentType(), selectionList.size());
table.setSelection(selectionList.toArray((Object[]) selectionArray));
}
else {
table.setSelection(selectionList);
}
}
}
}
Might not be the best solution but should work, let me know if there is a better way. Hope this helps someone.
Just implement the property bound to selection property of DataTable (selection="#{pageBackingForm.selectedEntityList}") like this and it will work :
private Map<Integer, List<Entity>> selectedEntityListMap = new Hashtable<>();
public List<Entity> getSelectedEntityList() {
return selectedEntityListMap.get(getCurrentEntitySelectionPage());
}
public void setSelectedEntityList(List<Entity> selectedEntityList) {
if (selectedEntityList == null) {
selectedEntityListMap.remove(getCurrentEntitySelectionPage());
return;
}
selectedEntityListMap.put(getCurrentEntitySelectionPage(), selectedEntityList);
}
public Integer getCurrentEntitySelectionPage() {
DataTable dataTable = (DataTable) FacesContext.getCurrentInstance().getViewRoot().findComponent("formId:dataTableId");
return dataTable.getPage();
}
While Bruno's solution works for keeping selections across paginations, it doesn't account for retaining selections on an individual page (i.e. when never changing pages).
This problem can be resolved more simply by using the rowSelectCheckbox and rowUnselectCheckbox ajax events, in addition to having a separate "saved" row list.
JSF:
<p:dataTable selection="#{myBean.selectedRows}" ... >
<p:ajax event="rowSelectCheckbox" process="#this" listener="#{myBean.onSelectRow}" />
<p:ajax event="rowUnselectCheckbox" process="#this" listener="#{myBean.onUnselectRow}" />
<p:column selectionMode="multiple" ... />
...
</p:dataTable>
Backing Bean:
private List<MyRowClass> selectedRows;
private List<MyRowClass> selectedRowsSaved;
...
public void onSelectRow(SelectEvent event){
MyRowClass row = (MyRowClass) event.getObject();
selectedRowsSaved.add(row);
}
public void onUnselectRow(UnselectEvent event){
MyRowClass row = (MyRowClass) event.getObject();
selectedRowsSaved.remove(row);
}
public List<MyRowClass> getSelectedRows(){
return selectedRowsSaved;
}
public void setSelectedRows(List<MyRowClass> selectedRows){
this.selectedRows = selectedRows;
}
This way the list of saved rows is always kept up to date without needing a "page" ajax event.
I had the same problem with my data table. Although my case is a bit different because I am using selectBooleanCheckbox instead. I found a simple solution that works for me. It hit me when you said "old selection are not rendered in the table".
strap the checkbox with a a4j:support event
code:
<h:selectBooleanCheckbox value="#{batch.toPortfolio}">
<a4j:support event="onchange" />
</h:selectBooleanCheckbox>
I filled my rightList of the PickList with Objects from a web service, but when i select some elements, i want to get these elements in myManagedBean, because i'll affect them to an Object.
JSF :
<h:form>
<rich:panel>
<h:panelGrid columns="2" styleClass="criteresSaisie"
rowClasses="critereLigne" columnClasses="titreColonne,">
<h:outputLabel for="libelleCoplement" value=" "
size="20" />
<rich:pickList d="libelleCoplement" sourceCaption="Compléments"
targetCaption="Compléments sélectionnés"
value="#{listeCmpltDispoModel.listeCmpltSelect}" size="15"
addText=">" addAllText=">>" removeText="<"
removeAllText="<<" listWidth="270px" listHeight="110px"
orderable="true">
<f:converter converterId="cmpltsTitresConcerter" />
<f:selectItems value="#{listeCmpltDispoModel.listeCmpltDispo}"
var="liste" itemLabel="#{liste.libelleComplement}"
itemValue="#{liste.cdComplement}"/>
</rich:pickList>
</h:panelGrid>
<h:panelGroup>
<div align="right">
<h:panelGrid columns="8">
<h:commandButton value="Valider"
action="#{saisieCmpltsTitreCtrl.valider}" />
</h:panelGrid>
</div>
</h:panelGroup>
</rich:panel>
</h:form>
The bean :
#ManagedBean(name = "listeCmpltDispoModel")
#SessionScoped
public class ListeCmpltDispoModel implements Serializable {
private static final long serialVersionUID = 1L;
private Long cdComplement;
private String libelleComplement;
private int nbCompl;
private List<ComplementsDispoSortieDTO> listeCmpltDispo ;
private List<ComplementsDispoSortieDTO> listeCmpltSelect ;
public ListeCmpltDispoModel() {
}
public Long getCodeComplement() {
return cdComplement;
}
public void setCodeComplement(Long cdComplement) {
this.cdComplement = cdComplement;
}
public String getLibelleComplement1() {
return libelleComplement;
}
public void setLibelleComplement1(String libelleCoplement) {
this.libelleComplement = libelleCoplement;
}
public Long getCdComplement() {
return cdComplement;
}
public void setCdComplement(Long cdComplement) {
this.cdComplement = cdComplement;
}
public String getLibelleComplement() {
return libelleComplement;
}
public void setLibelleComplement(String libelleComplement) {
this.libelleComplement = libelleComplement;
}
public List<ComplementsDispoSortieDTO> getListeCmpltDispo() {
return listeCmpltDispo;
}
public void setListeCmpltDispo(List<ComplementsDispoSortieDTO> listeCmpltDispo) {
this.listeCmpltDispo = listeCmpltDispo;
}
public int getNbCompl() {
return nbCompl;
}
public void setNbCompl(int nbCompl) {
this.nbCompl = nbCompl;
}
public List<ComplementsDispoSortieDTO> getListeCmpltSelect() {
return listeCmpltSelect;
}
public void setListeCmpltSelect(List<ComplementsDispoSortieDTO> listeCmpltSelect) {
this.listeCmpltSelect = listeCmpltSelect;
}
}
Converter :
#FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter {
#SuppressWarnings("null")
public Object getAsObject(FacesContext context, UIComponent component,
String value){
ComplementsDispoSortieDTO cmpltSelect = null;
cmpltSelect.setCdComplement(Long.parseLong(value));
return cmpltSelect;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj) {
return String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
}
Any help is greatly apprectiated!
Roughtly you need 3 things :
Custom converter for your object (Object to String, String to Object)
Getter/Setter with the List of your Objects choices
Getter/Setter with the List of your Objects selected
Everything is perfectly described here : RichFaces Showcase - pickList
EDIT :
Adding this should fix your problem :
<rich:pickList ...>
<f:converter converterId="cmpltsTitresConcerter" />
</rich:pickList>
also the converter property in <f:selectItems /> is not valid : f:selectItems
EDIT :
You should modify your converter like that to remove converting exceptions :
#FacesConverter(value="cmpltsTitresConcerter")
public class CmpltsTitresConcerter implements Converter
{
public Object getAsObject(FacesContext context, UIComponent component, String value)
{
ComplementsDispoSortieDTO cmpltSelect = null;
if(value != null)
{
cmpltSelect = new ComplementsDispoSortieDTO();
cmpltSelect.setCdComplement(Long.parseLong(value));
}
return cmpltSelect;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object obj)
{
String result = null;
if(obj != null)
{
result = String.valueOf(((ComplementsDispoSortieDTO) obj).getCdComplement());
}
return result;
}
}
Your selected objects are bound to the value attribute which must have a getter and setter.
i' m developing my first jsf2 richfaces application , and now i am faced with the following problem :
i have the following menù
<rich:panelMenu >
<rich:panelMenuGroup >
<rich:panelMenuItem label="Users" name="Users " />
<rich:panelMenuItem label="Orders" name="Orders" />
</rich:panelMenuGroup>
</rich:panelMenu>
I want that when click on the panelMenuItem is created a new tab and within of this new tab must insert a table that contains all users of the my application
i saw a few example of this type
<rich:tabPanel switchType="client" id="tabPanel">
<c:forEach items="#{handlerTab.tabsName}" var="tabName">
<rich:tab name = ... >
</rich:tab>
</c:foreach>
but don't know as insert a table in my new tab
how can i do?
tanks for your reply, but i don't want declare all tabs in the view , i want add and remove tab dynamically , now i have managed to do this
<rich:panelMenu >
<rich:panelMenuGroup >
<rich:panelMenuItem label="Users" name="Users" action="#{tabsBean.createTabs()}" render="tabs" />
<rich:panelMenuItem label="Orders" name="Orders" action="#{tabsBean.createTabs()}" render="tabs" />
</rich:panelMenuGroup>
<h:panelGrid id="tabs" binding="#{tabsBean.panelGrid}"/>
then i have a bean that manage the tabs
#ManagedBean
#SessionScoped
public class TabsBean {
private HtmlPanelGrid panelGrid;
private Integer numOfTabs;
#PostConstruct
public void init(){numOfTabs=1;}
public Integer getNumOfTabs() {
return numOfTabs;
}
public void setNumOfTabs(Integer numOfTabs) {
this.numOfTabs = numOfTabs;
}
public TabsBean() {
}
public HtmlPanelGrid getPanelGrid() {
return panelGrid;
}
public void setPanelGrid(HtmlPanelGrid panelGrid) {
this.panelGrid = panelGrid;
}
public void createTabs (){
FacesContext context = FacesContext.getCurrentInstance();
Application application = context.getApplication();
UITabPanel tabPanel = (UITabPanel)application.createComponent(UITabPanel.COMPONENT_TYPE);
tabPanel.setSwitchType(SwitchType.ajax);
for (int i=0; i<numOfTabs; i++){
UITab tab = new UITab();
tab = (UITab)application.createComponent(UITab.COMPONENT_TYPE);
tab.setName("User Count "+i);
tabPanel.getChildren().add(tab);
}
numOfTabs++;
panelGrid.getChildren().clear();
panelGrid.getChildren().add(tabPanel);
}
}
now my problem is that i must add of components a this tabs (datatable that contains all user , form for insert a user and other)
how can i do?
If you want the menu to open tabs with specific content it's better if you pre-create the tab and hide it, showing it (rendering) on request. To create the Users do the following:
Create a User class (if you don't have one)
public class User {
private String name;
private String country;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getCountry() {
return country;
}
public void setCountry(String country) {
this.country = country;
}
}
Create a UserTab managed bean
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class UserTab implements Serializable {
private List<User> users;
private boolean rendered;
public UserTab() {
//Initialize list
users = new ArrayList();
//Initialize rendered attribute
rendered = false;
//Replace this for your User data retrieving method
createDummyUsers();
}
private void createDummyUsers() {
User user = new User();
user.setName("John Doe");
user.setCountry("USA");
users.add(user);
user = new User();
user.setName("Bill Doe");
user.setCountry("Canada");
users.add(user);
user = new User();
user.setName("Winston Doe");
user.setCountry("England");
users.add(user);
}
public void showTab() {
rendered = true;
}
public void hideTab() {
rendered = false;
}
public List<User> getUsers() {
return users;
}
public void setUsers(List<User> users) {
this.users = users;
}
public boolean isRendered() {
return rendered;
}
public void setRendered(boolean rendered) {
this.rendered = rendered;
}
}
Create a tab handler:
import java.io.Serializable;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
#ManagedBean
#ViewScoped
public class TabHandler implements Serializable {
private String activeTab;
public TabHandler() {
activeTab = "none";
}
public String getActiveTab() {
return activeTab;
}
public void setActiveTab(String activeTab) {
this.activeTab = activeTab;
}
}
Create the view
<h:body>
<h:form>
<rich:panelMenu >
<rich:panelMenuGroup >
<rich:panelMenuItem label="Users" name="Users"
actionListener="#{userTab.showTab()}">
<f:ajax event="select"
execute="#this"
render="tabPanel"
listener="#{tabHandler.setActiveTab('usersTab')}"/>
</rich:panelMenuItem>
<rich:panelMenuItem label="Orders" name="Orders" />
</rich:panelMenuGroup>
</rich:panelMenu>
<rich:tabPanel id="tabPanel"
switchType="client"
activeItem="#{tabHandler.activeTab}">
<rich:tab id="mainTab">
</rich:tab>
<rich:tab id="usersTab"
rendered="#{userTab.rendered}">
<f:facet name="header">
<h:outputLabel value="Users"/>
<h:commandLink value=" X" actionListener="#{userTab.hideTab()}"/>
</f:facet>
<rich:dataTable value="#{userTab.users}"
var="user">
<rich:column>
#{user.name}
</rich:column>
<rich:column>
#{user.country}
</rich:column>
</rich:dataTable>
</rich:tab>
</rich:tabPanel>
</h:form>
</h:body>
Do the same steps and add the required tags for the Order class.
See:
RichFaces dynamic TabPanel
c:forEach is not an option if you want to add the tabs by click.
What you want is to add components dynamically:
How to dynamically add JSF components
Dynamically added input field in ui:repeat is not processed during form submit
How to Dynamically adding fields in JSF?
I'm implementing a PrimeFaces picklist in a dialog. Everytime the dialog is shown the contents of the target of the picklist should change depending on a list entry shown before. Before I open the dialog I fill the target of the picklist in ProdukteBean.onEditProdukt(..) with the appropriate values. Unfortunately these target values do not show up in the target container. Here are the relevant code pieces:
list.xhtml:
<p:commandLink id="editButton" update=":dialogForm:editDialogPanel" title="Edit"
oncomplete="produktDialog.show();"
process="#this" partialSubmit="true"
action="#{produkteBean.onEditProdukt}">
<h:outputText value="Bearbeiten" />
<f:setPropertyActionListener value="#{p}" target="#{produkteBean.produkt}" />
</p:commandLink>
dialog.xhtml:
<!-- ... -->
<p:dialog id="dialog" header="Produkt-Details" widgetVar="produktDialog" appendToBody="true" showEffect="explode" hideEffect="explode" modal="true" width="500">
<p:messages id="msgs"/>
<h:form id="dialogForm">
<!-- ... -->
<p:pickList id="produkteDatenList" var="proddat" value="#{produkteBean.datenList}"
itemLabel="#{proddat.bezeichnung}" itemValue="#{proddat}"
converter="produktDatConverter"/>
<!-- ... -->
</h:form>
</p:dialog>
ProdukteBean.java:
#Named("produkteBean")
#ViewScoped // #SessionScoped // #ViewScoped
public class ProdukteBean implements Serializable {
#Inject #Transient private ProdukteService produkteService;
#Inject #Transient private DatenService datenService;
#Inject()
private ProdukteDatenBean produkteDatenBean;
private DualListModel<Dat> datenList = new DualListModel<Dat>();
private Dat dat = null;
public ProdukteBean() {
}
#PostConstruct
private void init() {
getAll();
}
private void getAll() {
logger.debug("getAll()");
getAllProdukte();
getAllDaten();
}
private void getAllDaten() {
logger.debug("getAllDaten()");
List<Dat> source = new ArrayList<Dat>();
source.addAll(datenService.list());
List<Dat> target = new ArrayList<Dat>();
if (produkt.getDaten() != null) {
logger.debug("adding " + produkt.getDaten().size() + " daten to produkt " + produkt.getName());
target.addAll(produkt.getDaten());
}
DualListModel<Dat> datenList = new DualListModel<Dat>();
datenList.setSource(source);
datenList.setTarget(target);
setDatenList(datenList);
}
public List<Produkt> getAllProdukte() {
logger.debug("getAllProdukte()");
return produkteService.list();
}
public void onEditProdukt() {
onEditProdukt(null);
}
public void onEditProdukt(ActionEvent actionEvent) {
logger.debug("onEditProdukt: " + ReflectionToStringBuilder.toString(produkt));
if (produkt != null) {
setSelectedEinheit(produkt.getEinheit());
getAllDaten();
}
FacesMessage msg = new FacesMessage("Produkt ausgewählt", produkt.getName());
FacesContext.getCurrentInstance().addMessage(null, msg);
}
/**
* #return the einheitList
*/
public List<Einheit> getEinheitList() {
if (einheitList == null) {
einheitList = produkteService.getEinheiten();
}
return einheitList;
}
}
ProduktDatConverter.java:
#FacesConverter(forClass=Dat.class,value="produktDatConverter")
#ViewScoped
public class ProduktDatConverter implements Converter {
#Inject
#Transient DatenService datenService;
#Transient
protected final Logger logger = Logger.getLogger(getClass().getName());
// gets never called (of course)
public Object getAsObject(FacesContext arg0, UIComponent arg1, String str) {
logger.debug("getAsObject(): " + str);
return null;
}
public String getAsString(FacesContext arg0, UIComponent arg1, Object object) {
if (object instanceof Dat) {
// logger.debug(" returning id " + String.valueOf(((Dat) object).getId()));
return Long.toString(((Dat) object).getId());
}
return null;
}
}
Any ideas? Many thanks in advance!
You are using #ViewScoped along CDI Managed Bean. Change it to JSF Managed Bean. Better, use CODI instead
Also see:
View Scope in CDI Weld
CDI missing #ViewScoped and #FlashScoped
I suppose it's converter + injection problem. Please, click here and refer to the BalusC answer. You can also try to replace:
#FacesConverter(forClass=Dat.class,value="produktDatConverter")
#ViewScoped
with
#ManagedBean
#RequestScoped
and call the converter like this:
converter="#{produktDatConverter}"