SelectItems of SelectOneMenu always null - java

When I print the selected item in the console , it is always returned as null
here is the method that create the SelectItem in my ManagedBean:
public List<String> getlisteMatricule() throws HibernateException
{
List<String> matricules = new ArrayList<String>();
for (Vehicule v : vehiculedao.getAll())
{
matricules.add(v.getMatricule());
System.out.println(v.getMatricule());
}
return matricules ;
}
public List<SelectItem> getAllMatricules()
{
List<SelectItem> options = new ArrayList<SelectItem>();
List<String> listMatricules = getlisteMatricule();
for (String mat : listMatricules)
{
options.add(new SelectItem(mat));
System.out.println("items = " + new SelectItem(mat));
}
return options ;
}
And here is my variables in my model which contain the getter and the setters and the constructor:
public class Program
{
private int id_progf;
private int nbrHeure;
private float montantGlobal;
private String commentaire;
private int cin_mon;
private String matricule;
private int cin_cand;
///gettersand setters
.... }
The methode that bring the variables from the database (List)
#Override
public Vehicule getMatricule(String matricule) {
Session session = HibernateUtil.currentSession();
Vehicule v=(Vehicule)session.get(Vehicule.class, matricule);
return v;
}
And finally my xhtml file, it contains the form:
<h:panelGrid columns="2" >
<h:outputText value="Moniteur :" />
<h:selectOneMenu id="listeNomPrenom" title="Nom et Prenom" value="{#programMB.np}">
<f:selectItems value="#{moniteurMB.allNomPrenom}" />
</h:selectOneMenu>
<h:outputText value="Vehicule :" />
<h:selectOneMenu id="ListeMatricules" title="Matricules" value="{#programMB.program.matricule}">
<f:selectItems value="#{vehiculeMB.allMatricules}" />
</h:selectOneMenu>
<h:outputText value="Nombre heures:" />
<p:inputText value="#{programMB.program.nbrHeure}" />
</h:panelGrid>
<p:commandButton value="Save" action="#{programMB.ajouterProg}" />

In the first look, I saw the problem is in the value attribute of both of your selectOneMenu bellow:
<h:selectOneMenu id="listeNomPrenom" title="Nom et Prenom" value="{#programMB.np}">
<f:selectItems value="#{moniteurMB.allNomPrenom}" />
</h:selectOneMenu>
and:
<h:selectOneMenu id="ListeMatricules" title="Matricules" value="{#programMB.program.matricule}">
<f:selectItems value="#{vehiculeMB.allMatricules}" />
</h:selectOneMenu>
In both of them you just putted # in the wrong place. change value="{#programMB.np}" to value="#{programMB.np}" and value="{#programMB.program.matricule}" to value="#{programMB.program.matricule}" and it should work for you!

Related

How to add h:inputText dynamically in jsf without losing previously dynamically added h:inputText value? [duplicate]

This question already has answers here:
How to dynamically add JSF components
(3 answers)
Closed 6 years ago.
I am trying to add h:inputText and a selectOneMenu dynamically in jsf2. And I got success in that. Now the new problem is that, when I clicked "Add New" button, previously dynamically added h:inputText value is erased. And I don't want that to happen. My code is below. Please help. :)
<h:form>
<h:dataTable id="bankAccountDataTable" value="#{kycBeanJSF.kycDataModelJSF.kycdto.bankAccountInfoDTOs}" var="item" binding="#{kycBeanJSF.htmlDataTable}">
<h:column>
<label class="label-control"><p:outputLabel value="आबेदकको बैंक खाता नम्बर:"/><span class="required">*</span></label>
<h:inputText value="#{item.bankAccountNumber}"/>
</h:column>
<h:column>
<h:selectOneRadio id="radio1" value="#{item.bankAccountType}" layout="lineDirection" >
<f:selectItem itemLabel="चल्ती खाता" itemValue="chalti" />
<f:selectItem itemLabel="बचत खाता" itemValue="bachat" />
<f:selectItem itemLabel="कॉल खाता" itemValue="call" />
</h:selectOneRadio>
</h:column>
<h:column>
<h:commandButton value="Remove" action="#{kycBeanJSF.kycWebCoreBean.remove(item)}" immediate="true"/>
</h:column>
</h:dataTable>
<h:commandButton value="Add New" action="#{kycBeanJSF.kycWebCoreBean.addNew()}" immediate="true"/>
</h:form>
My KycBeanJSF is in Request Scope. KycDataModelJSF is in SessionScope.
KycBeanJSF.java
public class KycBeanJSF {
#ManagedProperty(value = "#{kycDataModelJSF}")
private KycDataModelJSF kycDataModelJSF;
private KycWebCoreBean kycWebCoreBean;
#PostConstruct
public void initWebCore(){
kycWebCoreBean = new KycWebCoreBean();
}
//getters and setters
}
KycDataModelJSF.java
#ManagedBean(name = "kycDataModelJSF")
#SessionScoped
public class KycDataModelJSF {
private KYCDTO kycdto;
#PostConstruct
public void init(){
addNew();
}
public KYCDTO getKycdto() {
if (kycdto == null) {
kycdto = new KYCDTO();
}
return kycdto;
}
public void setKycdto(KYCDTO kycdto) {
this.kycdto = kycdto;
}
public void addNew() {
if(getKycdto().getBankAccountInfoDTOs().size()<3){
getKycdto().getBankAccountInfoDTOs().add(new BankAccountInfoDTO());
}
else if(getKycdto().getBankAccountInfoDTOs().size()>=3){
FacesUtil.setErrorFacesMessage("Sorry, cannot add more than three accounts.");
}
}
}
KycWebCoreBean.java
public class KycWebCoreBean {
private KycDataModelJSF kycDataModelJSF;
public KycWebCoreBean() {
kycDataModelJSF = (KycDataModelJSF) Util.getSessionObject("kycDataModelJSF");
kycDependencyInjection = (KycDependencyInjection) Util.getSessionObject("kycDependencyInjection");
applicantRegisterDataModelJSF = (ApplicantRegisterDataModelJSF) Util.getSessionObject("applicantRegisterDataModelJSF");
}
public void addNew() {
if(kycDataModelJSF.getKycdto().getBankAccountInfoDTOs().size()<3){
kycDataModelJSF.getKycdto().getBankAccountInfoDTOs().add(new BankAccountInfoDTO());
}
else if(kycDataModelJSF.getKycdto().getBankAccountInfoDTOs().size()>=3){
FacesUtil.setErrorFacesMessage("Sorry, cannot add more than three accounts.");
}
}
public void remove(BankAccountInfoDTO b) {
kycDataModelJSF.getKycdto().getBankAccountInfoDTOs().remove(b);
}
}
I will provide more Info if required.
Thanks In Advance.
As h:inputText can store only one value, I assume that you want to append new value to old. If so, than in bean which manages that property in setter method use
public void setValue(String value) {
this.value = this.value + " " + value;
}
instead of usual
public void setValue(String value) {
this.value = value;
}
With string "my string" in h:inputText after changing value to "another string" result is "my string another string"
<h:dataTable id="bankAccountDataTable" value="#{kycBeanJSF.kycDataModelJSF.kycdto.bankAccountInfoDTOs}" var="item">
<h:column>
<label class="label-control"><p:outputLabel value="आबेदकको बैंक खाता नम्बर:"/><span class="required">*</span></label>
<h:inputText value="#{item.bankAccountNumber}"/>
</h:column>
<h:column>
<h:selectOneRadio id="radio1" value="#{item.bankAccountType}" layout="lineDirection">
<f:selectItem itemLabel="चल्ती खाता" itemValue="chalti" />
<f:selectItem itemLabel="बचत खाता" itemValue="bachat" />
<f:selectItem itemLabel="कॉल खाता" itemValue="call" />
</h:selectOneRadio>
</h:column>
<h:column>
<h:commandButton value="Remove" actionListener="#{kycBeanJSF.kycWebCoreBean.remove(item)}">
<f:ajax render="#form"/>
</h:commandButton>
</h:column>
</h:dataTable>
<h:commandButton value="Add New" actionListener="#{kycBeanJSF.kycWebCoreBean.addNew()}">
<f:ajax execute="bankAccountDataTable" render="bankAccountDataTable msg"/>
</h:commandButton>

'Show All' button for Primefaces DataTable

I would like to have a 'Show All' button for a primefaces datatable, however, I'm experiencing some trouble. Here's some test code that demostrates the problem:
Test.xhtml:
...
<h:form>
<p:selectBooleanCheckbox value="#{testBean.showAll}" itemLabel="Show All">
<p:ajax update="#form"/>
</p:selectBooleanCheckbox>
<p:panel id="test">
<p:dataTable id="values" var="value" value="#{testBean.fullList}" filteredValue="#{testBean.filteredList}" paginatorAlwaysVisible="false"
paginator="#{!testBean.showAll}" rows="#{!testBean.showAll ? 2 : null }" widgetVar="valuesTable"
emptyMessage="No records found.">
<p:column id="enum" sortBy="#{value.toString()}" filterBy="#{value.toString()}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Enum" />
</f:facet>
<h:outputText value ="#{value.toString()}"/>
</p:column>
<p:column id="name" sortBy="#{value.name}" filterBy="#{value.name}" filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{value.name}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
...
And here's TestBean.java:
import javax.inject.Named;
import javax.enterprise.context.SessionScoped;
import java.io.Serializable;
import java.util.Arrays;
import java.util.List;
#Named(value = "testBean")
#SessionScoped
public class TestBean implements Serializable {
public static enum Value {
ONE, TWO, THREE;
public String getName() {
return name().toLowerCase();
}
#Override
public String toString() {
return this.name();
}
}
/**
* Creates a new instance of TestBean
*/
public TestBean() {
}
private boolean showAll = false;
private List<Value> fullList = Arrays.asList(Value.values());
private List<Value> filteredList;
public boolean isShowAll() {
return showAll;
}
public void setShowAll(boolean showAll) {
this.showAll = showAll;
}
public List<Value> getFullList() {
return fullList;
}
public void setFullList(List<Value> fullList) {
this.fullList = fullList;
}
public List<Value> getFilteredList() {
return filteredList;
}
public void setFilteredList(List<Value> filteredList) {
this.filteredList = filteredList;
}
}
If I don't change tabs, the page works as expected: toggling the 'Show All' button updates the table to show all 3 values, or only two. However, if show all is not checked (only 2 rows are showing), and I click to the 2nd page of the table to view the third record, and then click 'Show All', the table does not update properly. It removes the paginator from the top of the table (as expected), but still only shows the 3rd record. And if I then uncheck show all and navigate back to the first page of the datatable, it is now broken too.
I've tried changing the ajax update statement to the id of the table, but that didn't change the results.
Am I doing something wrong? Thanks in advance.
You have to programmatically set the values you need directly on the DataTable component (I used explicit first and rows bean properties just for more readability):
<h:form>
<p:selectBooleanCheckbox value="#{testBean.showAll}" itemLabel="Show All">
<p:ajax update="#form" />
</p:selectBooleanCheckbox>
<p:panel id="test">
<p:dataTable id="values" var="value" value="#{testBean.fullList}"
filteredValue="#{testBean.filteredList}" paginatorAlwaysVisible="false"
paginator="#{!testBean.showAll}" first="#{testBean.first}" rows="#{testBean.rows}"
widgetVar="valuesTable" emptyMessage="No records found.">
<p:column id="enum" sortBy="#{value.toString()}" filterBy="#{value.toString()}"
filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Enum" />
</f:facet>
<h:outputText value="#{value.toString()}" />
</p:column>
<p:column id="name" sortBy="#{value.name}" filterBy="#{value.name}"
filterMatchMode="contains">
<f:facet name="header">
<h:outputText value="Name" />
</f:facet>
<h:outputText value="#{value.name}" />
</p:column>
</p:dataTable>
</p:panel>
</h:form>
and
#Named
#ViewScoped
public class TestBean implements Serializable
{
private static final long serialVersionUID = 1L;
public static enum Value
{
ONE, TWO, THREE;
public String getName()
{
return name().toLowerCase();
}
#Override
public String toString()
{
return name();
}
}
private boolean showAll = false;
private int first = 0;
private int rows = 2;
private List<Value> fullList = Arrays.asList(Value.values());
private List<Value> filteredList;
public boolean isShowAll()
{
return showAll;
}
public void setShowAll(boolean showAll)
{
this.showAll = showAll;
first = 0;
rows = showAll ? 0 : 2;
filteredList = null;
// get the FacesContext instance
FacesContext context = FacesContext.getCurrentInstance();
// get the current component (p:selectBooleanCheckbox)
UIComponent component = UIComponent.getCurrentComponent(context);
// find DataTable within the same NamingContainer
DataTable table = (DataTable) component.findComponent("values");
// reset first row index
table.setFirst(first);
// reset last row index
table.setRows(rows);
// reset filterd value
table.setFilteredValue(null);
}
// all other getters/setters
}
Tested on Wildfly 10.0.0.Final with JSF 2.3.0-m06 (Mojarra) and PrimeFaces 6.0.2
I had the same problem, was resolved by this steps:
Adding styleClass="all-paginator" to datatable
<p:dataTable id="values" var="value" value="#{testBean.fullList}"
filteredValue="#{testBean.filteredList}" styleClass="all-paginator"..>
Add a getter of number of all records in the controller (TestBean.java) like:
public int getRecordNumber() {
if(CollectionUtils.isEmpty(fullList)){
return 0;
}
return fullList.size();
}
Add the property of the recordNumber at the end of the datatable attribute "rowsPerPageTemplate"
<p:dataTable id="values" var="value" value="#{testBean.fullList}" filteredValue="#{testBean.filteredList}"
styleClass="all-paginator" rowsPerPageTemplate="20,30,40,50,#{testBean.recordNumber}"..>

set related values to null after selecting <f:selectItem>

below is my jsf code,
<h:outputText value="SP Id" styleClass="required"/>
<h:selectOneMenu style="padding-left:60px;" class="input" id="spid" required="true" requiredMessage="Select SP Id"
value="#{applicationController.spid}">
<p:ajax listener="#{applicationController.onFromChange()}"
update="fromnames" />
<f:selectItem itemValue="" itemLabel="--Select--" />
<f:selectItems value="#{applicationController.spids}"></f:selectItems>
</h:selectOneMenu>
<h:message for="spid" class="hmsg" />
<h:outputText value="Sp Name" class="left1"/>
<h:inputText class="input" id="fromnames"
value="#{applicationController.spname}" />
<h:message for="fromnames" />
backing bean code is(method),
public void onFromChange() {
if (spid != null && !spid.equals("")) {
int spId = Integer.parseInt(spid);
spname = baseService.getSalesPersonById(spId);
} else {
}
}
//setter-getters
public String getSpname() {
return spname;
}
public void setSpname(String spname) {
this.spname = spname;
}
public List<Integer> getSpids() {
return spids;
}
public void setSpids(List<Integer> spids) {
this.spids = spids;
}
from above code every thing works fine.
problem:if i select f:selectItems values, relating values(spname)
are displaying. after selecting f:selectItem spname should set to null but it's not setting to null, instead of that previous values are displayed.
change jsf code as below,
<h:selectOneMenu style="padding-left:60px;" class="input" id="spid" required="true" requiredMessage="Select SP Id"
value="#{applicationController.spid}">
<p:ajax listener="#{applicationController.onFromChange()}"
update="myForm1" event="change" process="#this"/>
<f:selectItem itemValue="0" itemLabel="--Select--" noSelectionOption="false" />
<f:selectItems value="#{applicationController.spids}"></f:selectItems>
</h:selectOneMenu>
<h:message for="spid" class="hmsg" />
and modify backing bean method as,
public void onFromChange() {
if (spid != null && !spid.equals("")) {
try{
int spId = Integer.parseInt(spid);
spname = baseService.getSalesPersonById(spId);
}
catch(Exception exception){
spname = null;
}
} else {
spname = null;
}
}

SelectItemsConverter Omnifaces preselected object value?

I have a problem, well when I save my object to the DB it works fine, but when i want to retrieve it from the DB doesn't work, I'm using selectItemsConverter by Omnifaces
I have my Object "Modelo" which has two other objects inside, which are "Marca" and "Gama"
These are my Java entities (the toString() is for Omnifaces):
Modelo:
private Marca marca;
private Gama gama;
getters and setters...
#Override
public String toString() {
return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}
Marca:
getters and setters...
#Override
public String toString() {
return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}
Gama:
getters and setters...
#Override
public String toString() {
return String.format("%s[codigo=%d]", getClass().getSimpleName(), getCodigo());
}
Well and this is my managedBean
ModeloBean
#ManagedBean
#ViewScoped
public class ModeloBean {
private Modelo modelo = new Modelo();
getters and setters ...
//This is for call the DB to retrieve the value, and works fine, but i cant show the preselected value to the xhtml
public void leer(Modelo mo) throws Exception {
ModeloDAO dao = new ModeloDAO();
try {
this.init();
this.modelo = dao.leer(mo);
} catch (Exception e) {
throw e;
} finally {
dao = null;
}
}
This is my xhtml Page
I have a dialog which I used it for save and update an object
<p:dialog id="dlgDatos" widgetVar="wdlgDatos" modal="true" appendToBody="true" header="#{modeloBean.accion}" draggable="false" resizable="false">
<h:form>
<h:panelGrid columns="2">
<p:outputLabel value="Marca" />
<p:selectOneMenu value="#{modeloBean.modelo.marca}" converter="omnifaces.SelectItemsConverter" filter="true" filterMatchMode="startsWith" required="true">
<f:selectItem itemLabel="Seleccione" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{marcaBean.lstMarcasVigentes}" var="marca" itemLabel="#{marca.nombre}" itemValue="#{marca}" />
</p:selectOneMenu>
<p:outputLabel value="Gama" />
<p:selectOneMenu value="#{modeloBean.modelo.gama}" converter="omnifaces.SelectItemsConverter" filter="true" filterMatchMode="startsWith" required="true">
<f:selectItem itemLabel="Seleccione" itemValue="#{null}" noSelectionOption="true" />
<f:selectItems value="#{gamaBean.lstGamasVigentes}" var="gama" itemLabel="#{gama.nombre}" itemValue="#{gama}" />
</p:selectOneMenu>
<p:outputLabel for="txtNombre" value="Modelo" />
<p:column>
<p:inputTextarea id="txtNombre" value="#{modeloBean.modelo.nombre}" />
<p:watermark for="txtNombre" value="Para registrar varios modelos, sepárelos por comas (,)" />
</p:column>
<p:outputLabel value="Vigencia" rendered="#{modeloBean.accion eq 'Modificar'}"/>
<p:selectBooleanCheckbox value="#{modeloBean.modelo.vigencia}" rendered="#{modeloBean.accion eq 'Modificar'}"/>
<p:commandButton value="#{modeloBean.accion}" actionListener="#{modeloBean.operar()}" oncomplete="PF('wdlgDatos').hide(); PF('wdtLista').clearFilters();" update=":frmLista:dtLista, :msj"/>
<p:commandButton value="Cancelar" immediate="true" onclick="PF('wdlgDatos').hide();"/>
</h:panelGrid>
</h:form>
</p:dialog>
The selectOneMenu works fine for save, but for update only retrieve me the Strings value and not the preselected value of my comboBoxes
This is the dialog which only retrieve the String value of "105" cause is a String and my boolean value for the checkbox "Vigencia" but not my comboBoxes values. Where am I wrong?
I solved it adding this to my entites (hashCode and equals)
#Override
public int hashCode() {
int hash = 5;
hash = 83 * hash + this.codigo;
return hash;
}
#Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Modelo other = (Modelo) obj;
if (this.codigo != other.codigo) {
return false;
}
return true;
}

How to get the selected row of a filtered extendedDataTable?

In my application (RichFaces 4.1) I have an extendedDataTable, in my backing bean I need to track the selected rows. I achieve this with the following code:
JSF:
<rich:extendedDataTable id="freie"
selectionMode="multipleKeyboardFree"
selection="#{myBean.tableSelection}"
...
<a4j:ajax execute="#this" event="selectionchange"
listener="#{myBean.tableSelection}"
render="indicatorPanel" />
Java:
UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
Object originalKey= dataTable.getRowKey();
_tableSelectedEntries.clear();
for (Object selectionKey: _tableSelection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
_tableSelectedEntries.add((Entry) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
This works fine, as long as the table is not filtered. I use the standard RichFaces way to filter the table:
<rich:column sortBy="#{mitarbeiter.vorname}"
filterValue="#{mitarbeiterFilterBean.firstNameFilter}"
filterExpression="#{fn:containsIgnoreCase(mitarbeiter.vorname, mitarbeiterFilterBean.firstNameFilter)}">
When the table is filtered and I select for instance the first row, I get the rowKey for the first row of the unfiltered table in the backing bean. How can I get the rowData of the selected row when my table is filtered?
I think my code works the same way as in the showcase.
I could solve my problem by making my filter bean SessionScoped. I also don't bind the currently selected rows to my backing bean anymore. I get the selected rows using:
public void tableSelection (AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable= (UIExtendedDataTable) event.getComponent();
for (Object selectionKey: dataTable.getSelection()) {
It could also be achieved using rowKeyVar to get the correct row index.
Maybe you have overlooked something because I tried it and it works.
I copied the source for selectableTable and added the filter method from filterTable
Example usage: To get the selected item/items data just use a get method for selected items list
Source code (xhtml):
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:rich="http://richfaces.org/rich"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:a4j="http://richfaces.org/a4j"
xmlns:fn="http://java.sun.com/jsp/jstl/functions"
xmlns:h="http://java.sun.com/jsf/html">
<h:head>
<title>Richfaces Welcome Page</title>
</h:head>
<h:body>
<h:panelGrid columns="2">
<h:form>
<fieldset style="margin-bottom: 10px;">
<legend>
<h:outputText value="Selection Mode " />
</legend>
<h:selectOneRadio value="#{exTableSelect.selectionMode}">
<f:selectItem itemLabel="Single" itemValue="single" />
<f:selectItem itemLabel="Multiple" itemValue="multiple" />
<f:selectItem itemLabel="Multiple Keyboard-free" itemValue="multipleKeyboardFree" />
<a4j:ajax render="table, res" />
</h:selectOneRadio>
</fieldset>
<rich:extendedDataTable value="#{exTableSelect.inventoryItems}" var="car"
selection="#{exTableSelect.selection}" id="table" style="height:300px; width:500px;"
selectionMode="#{exTableSelect.selectionMode}">
<a4j:ajax execute="#form" event="selectionchange" listener="#{exTableSelect.selectionListener}"
render=":res" />
<f:facet name="header">
<h:outputText value="Cars marketplace" />
</f:facet>
<rich:column filterValue="#{exTableSelect.vendorFilter}"
filterExpression="#{fn:containsIgnoreCase(car.vendor, exTableSelect.vendorFilter)}">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="Vendor " />
<h:inputText value="#{exTableSelect.vendorFilter}">
<a4j:ajax render="table" execute="#this" event="change" />
</h:inputText>
</h:panelGroup>
</f:facet>
<h:outputText value="#{car.vendor}" />
</rich:column>
</rich:extendedDataTable>
</h:form>
<a4j:outputPanel id="res">
<rich:panel header="Selected Rows:" rendered="#{not empty exTableSelect.selectionItems}">
<rich:list type="unordered" value="#{exTableSelect.selectionItems}" var="sel">
<h:outputText value="#{sel.vendor} - #{sel.model} - #{sel.price}" />
</rich:list>
</rich:panel>
</a4j:outputPanel>
</h:panelGrid>
</h:body>
</html>
Managed Bean:
public class ExTableSelect {
private String selectionMode = "multiple";
private Collection<Object> selection;
private List<InventoryItem> inventoryItems;
private List<InventoryItem> selectionItems = new ArrayList<InventoryItem>();
private String vendorFilter;
public void selectionListener(AjaxBehaviorEvent event) {
UIExtendedDataTable dataTable = (UIExtendedDataTable) event.getComponent();
Object originalKey = dataTable.getRowKey();
selectionItems.clear();
for (Object selectionKey : selection) {
dataTable.setRowKey(selectionKey);
if (dataTable.isRowAvailable()) {
selectionItems.add((InventoryItem) dataTable.getRowData());
}
}
dataTable.setRowKey(originalKey);
}
public Filter<?> getFilterVendor() {
return new Filter<InventoryItem>() {
public boolean accept(InventoryItem t) {
String vendor = getVendorFilter();
if (vendor == null || vendor.length() == 0 || vendor.equals(t.getVendor())) {
return true;
}
return false;
}
};
}
#PostConstruct
public void addInventory(){
InventoryItem i = new InventoryItem();
i.setVendor("A");
InventoryItem i2 = new InventoryItem();
i2.setVendor("AB");
InventoryItem i3 = new InventoryItem();
i3.setVendor("AC");
InventoryItem i4= new InventoryItem();
i4.setVendor("E");
InventoryItem i5 = new InventoryItem();
i5.setVendor("F");
InventoryItem i6 = new InventoryItem();
i6.setVendor("G");
InventoryItem i7 = new InventoryItem();
i7.setVendor("H");
InventoryItem i8 = new InventoryItem();
i8.setVendor("I");
InventoryItem i9 = new InventoryItem();
i9.setVendor("J");
inventoryItems= new ArrayList<InventoryItem>();
inventoryItems.add(i);
inventoryItems.add(i2);
inventoryItems.add(i3);
inventoryItems.add(i4);
inventoryItems.add(i5);
inventoryItems.add(i6);
inventoryItems.add(i7);
inventoryItems.add(i8);
inventoryItems.add(i9);
}
public Collection<Object> getSelection() {
return selection;
}
public void setSelection(Collection<Object> selection) {
this.selection = selection;
}
public List<InventoryItem> getInventoryItems() {
return inventoryItems;
}
public void setInventoryItems(List<InventoryItem> inventoryItems) {
this.inventoryItems = inventoryItems;
}
public InventoryItem getSelectionItem() {
if (selectionItems == null || selectionItems.isEmpty()) {
return null;
}
return selectionItems.get(0);
}
public List<InventoryItem> getSelectionItems() {
return selectionItems;
}
public void setSelectionItems(List<InventoryItem> selectionItems) {
this.selectionItems = selectionItems;
}
public String getSelectionMode() {
return selectionMode;
}
public void setSelectionMode(String selectionMode) {
this.selectionMode = selectionMode;
}
public void setVendorFilter(String vendorFilter) {
this.vendorFilter = vendorFilter;
}
public String getVendorFilter() {
return vendorFilter;
}
}

Categories

Resources