Select data Checkbox - DataTable - Primefaces - java

How do I do to get the selected data that dataTable? I use this way because it is the "Edit" of a register.
<p:dataTable id="dataTable" var="valor" style="width:100%; text-align:center"
value="#{beanMensagemXContato.dataModelMsg}"
selection="#{beanMensagemXContato.selectedMensagemAssociada}"
paginator="true" rows="6" >
<f:facet name="header">
Mensagens
</f:facet>
<p:column style="width:5%">
<p:selectBooleanCheckbox value="#{valor.associada}" >
<p:ajax process="#form" event="valueChange" listener="# {beanMensagemXContato.adicionarMensagemContato}">
<f:param name="messageSelecionada" value="#{beanMensagemXContato.msgAssociada}" />
</p:ajax>
</p:selectBooleanCheckbox>
</p:column>
...
</p:dataTable>
I would pick the data by Bean as the event SelectEvent:
public void adicionarMensagemContato (SelectEvent event){
Mensagem mensagem = ((MensagemAssociada) event.getObject()).getMensagem();
MensagemAssociada mensagemAssociada = (MensagemAssociada) event.getObject();
...
}
But I could not take the data with the event ValueChange. I've tried with SelectEvent by tag selectionMode = "multiple", managed to get the data selected at that moment, the data previously selected and read from the database does not appear, use only when the way listed above in xhtml.
Already I appreciate the help.

The selection is stored in your bean field:
selection="#{beanMensagemXContato.selectedMensagemAssociada}"
If the adicionarMensagemContato method is located in the same bean, you can access you selection without a problem:
public void adicionarMensagemContato (SelectEvent event){
doSomething(this.selectedMensagemAssociada);
}
Another way is to use f:setPropertyActionListener - it allows you to store element from current row in a bean field (example below shows how to access current element in action invoked by button click):
<p:dataTable var="objectFromCurrentRow" ...>
...
<p:column ...>
<p:button ... action=#{beanMensagemXContato.performAnActionOnCurrentElement} ...>
<f:setPropertyActionListener value="#{objectFromCurrentRow}" target="#{beanMensagemXContato.selectedMensagemAssociada}" />
</p:button>
...

Related

How to select row of PrimeFaces dataTable via hover/mouseOver event

I have a dataTable, to which I want to add column with a Delete button for each row.
My issue is, that I don't know how to change the currentItem member variable of my bean class, before the delete method is called in the command button's actionListener.
I've tried adding a setPropertyActionListener tag:
<p:column>
<p:commandButton value="Delete" actionListener="#{itemBean.deleteItem}" process="#this"
update="table StartButtonForm">
<f:setPropertyActionListener value="#{item}" target="#{itemBean.currentItem}"/>
</p:commandButton>
</p:column>
but the value only gets changed after itemBean.deleteItem is called in the button's actionListener, so currentItem is not updated in time and the wrong item gets deleted.
Does anyone know how to change the currentItem whenever you hover with your mouse over a row?
Here is the whole table in my xhtml file in case it's important:
<p:dataTable id="table" var="item" value="#{itemBean.unrankedItems}" selection="#{itemBean.currentItem}"
selectionMode="single" rowKey="#{item.name}" rowSelectMode="add">
<p:ajax event="rowSelect" listener="#{itemBean.onRowSelect}"/>
<p:column>
<h:outputText value="#{item.name}"/>
</p:column>
<p:column>
<p:commandButton value="Delete" actionListener="#{itemBean.deleteItem}" process="#this"
update="table StartButtonForm">
<f:setPropertyActionListener value="#{item}" target="#{itemBean.currentItem}"/>
</p:commandButton>
</p:column>
</p:dataTable>
There is an exact Showcase example for your scenario with a Delete button in each row.
See: https://www.primefaces.org/showcase/ui/data/datatable/crud.xhtml

Error p:ajax update

In an edit screen, I'm using p:ajax to grab the selected value in an autocomplete field and fill in other fields in the form. But when ajax is running in question it is updating the information in the database table (update). How do I block this behavior? After all I should only do this when I clicked the save button.
Code:
<h:panelGroup>
<p:autoComplete id="demandaBeanDemandaLogradouroNome" value="#{demandaBean.demanda.logradouro}"
var="_item" minQueryLength="3" maxResults="15" required="true"
itemLabel="#{_item.nome}" itemValue="#{_item}" converter="#{logradouroBean.converter}"
completeMethod="#{logradouroBean.buscaLogradouro}" title="Busca pelo Logradouro">
<p:column>
<h:outputText value="#{_item.categoria.descricao}"/>
</p:column>
<p:column>
<h:outputText value="#{_item.nome}"/>
</p:column>
<p:ajax partialSubmit="true" immediate="true" event="itemSelect" update="demandaBeanDemandaBairro demandaBeanDemandaCruzamento"
listener="#{logradouroBean.buscaComplementoSelectEvent}"/>
</p:autoComplete>
<p:message for="demandaBeanDemandaLogradouroNome" styleClass="error"/>
</h:panelGroup>
I have found that what happens is that Hibernate synchronizes the persistent objects and if you change it it automatically updates in the database. To solve, I used an auxiliary object with the #Transient tag. Thanks.

JSF datatable populate rows based on selection

I have two datatables in one jsf page and both are having two different managed beans.
//master table
<p:dataTable id="dataTable" var="req" lazy="true"
value="#{emp.lazyModel}" paginator="true" rows="10"
selection="#{emp.selectedRequest}">
<p:ajax event="rowSelectRadio" listener="#{emp.onRowSelect}" />
<p:column selectionMode=">
<h:outputText value="#{req.empNo}" />
</p:column>
// detail table
<p:dataTable id="Det" var="det" lazy="true"
value="#{dept.lazyModel}" paginator="true" rows="1">
<p:column>
<f:facet name="header">
<h:outputText value="DeptNo" />
</f:facet>
<h:outputText value="#{det.deptNo}" />
</p:column>
Managed beans respectively
// Master table managed Bean
#Named("emp")
#ViewAccessScoped
public class EmployeeManagedBean implements Serializable {
#PostConstruct
public void init() {
initTable();
}
// Detail table managed Bean
#Named("dept")
#ViewAccessScoped
public class DepartmentManagedBean implements Serializable {
#PostConstruct
public void init() {
initTable();
}
initTable() is a method which populates LazyModel for both managed beans
When I select a row in master datatable, I am able to get selected row value in managed bean for first datatable which is EmployeeManagedBean
My question is what is the best approach to populate the second datatable by passing the selected row value of first datatable to second managed bean and thus populate the second datatable? The triggering point to display values in second datable should be based on the selection of a row in first datatable.
Any help is highly appreciable.
Thanks
I am new with this all, but I try doing like this:
pass selected row to second bean (DepartmentManagedBean)
took departments according to selected row
update second datatable, using p:ajax attribute update

Is it possible to have a form with validation and to use rich:modalPanel for data entry too?

Richfaces 3.3.3, Jsf 1.2:
I have a a4j:form that uses some simple validation, mainly required="true" to ensure the form does not get submitted without some necessary data.
I also have some complex data to add (optionally) to the form, so I thought the best way to do this is to have a a4j:commandButton that displays a rich:modalPanel where the user can create the complex data set.
The created data is also displayed in a h:selectManyListbox that is reRendered when the modalPanel is closed.
That's the plan, at least. I have the following problems:
reRender works, but only if I prevent validation via immediate="true" - which in turn seems to prevent the selected data from the modalPanel to be present in the backing Bean
if I remove the immediate tag, the data gets updated, but only if there are no validation errors
What is the best way to get this to work the way I want? Is there another, better way?
UPDATE:
The Validation that fails is in some different part of the form, not in the data entered via the modalPanel, which is displayed in the Listbox
CODE:
modalPanel:
<rich:modalPanel id="addTrimming" domElementAttachment="parent" width="150" height="130">
<f:facet name="header">
<h:panelGroup>
<h:outputText value="Define Filter" />
</h:panelGroup>
</f:facet>
<f:facet name="controls">
<h:panelGroup>
<h:graphicImage value="/images/close.gif" styleClass="hidelink" id="hidelinkAddTrimming"/>
<rich:componentControl for="addTrimming" attachTo="hidelinkAddTrimming" operation="hide" event="onclick"/>
</h:panelGroup>
</f:facet>
<h:panelGrid id="trimsettings" columns="3">
<h:outputText value="Target:" style="font-weight:bold"/>
<h:inputText id="target" label="XML Filename" required="true" value="#{xmlCreator.trimTarget}">
</h:inputText>
<h:outputText value=""/>
<h:outputText value="Mode:"/>
<h:selectOneMenu value="#{xmlCreator.trimMode}">
<f:selectItem itemLabel="Quality" itemValue="quality"/>
<f:selectItem itemLabel="after Primer" itemValue="afterPrimer"/>
<f:selectItem itemLabel="fixed" itemValue="fixed"/>
<f:selectItem itemLabel="Median length" itemValue="median"/>
<f:selectItem itemLabel="Motif" itemValue="motif"/>
</h:selectOneMenu>
<h:outputText value=""/>
</h:panelGrid>
<h:panelGroup>
<a4j:commandButton value="OK" action="#{xmlCreator.createTrimming}" onclick="from:submit()">
<a4j:support event="oncomplete" ajaxSingle="true" immediate="true" reRender="trimsPanel"/>
</a4j:commandButton>
</h:panelGroup>
</rich:modalPanel>
relevant form part:
<h:outputText value="Trimming:"/>
<a4j:outputPanel id="trimsPanel">
<h:selectManyListbox id="trims" value="#{xmlCreator.selectedTrimmings}">
<f:selectItems value="#{si:toSelectTrimmingList(xmlCreator.trimmings)}"/>
</h:selectManyListbox>
</a4j:outputPanel>
<a4j:commandButton id="addTrimButton" immediate="true" value=" + Trimming">
<rich:componentControl for="addTrimming" attachTo="addTrimButton" operation="show" event="onclick"/>
</a4j:commandButton>
If you do it in one form than you should separate it into two: one will be your main form and another will be form inside modal panel. Thus you'll be able to submit modal panel independently of main form and your submit button in modal panel will look like this:
<a4j:commandButton value="OK" action="#{xmlCreator.createTrimming}" reRender="trimsPanel"/>
you should use process with immediate to send modalpanel's data to bean when closing panel with a a:commandButton or a:commandLink.
for this, modal panel data has to be valid as expected, so you will get valid new data and add it to h:selectManyListBox 's bean value and reRender h:selectManyListBox.
this should work. if not please post your a:form and modalPanel code full to check.

Richfaces 4 Datatable with onclick Event on row

I have a simple Richfaces 4 <rich:dataTable> with some <rich:column>s.
Now I want if I press on one row, that below the table the ID of the row should be displayed.
Here is what I did so far:
<rich:dataTable value="#{placeholder_control.lichtList}" var="licht" width="100%" id="lichtListe" columns="2">
<rich:column>
<f:facet name="header">
<h:outputText value="Beschreibung" />
</f:facet>
<h:outputText value="#{licht['beschreibung'].stringValue}" width="20" />
<a4j:ajax immediate="true" event="onclick" render="testingID" listener="#{placeholder_control.selectActiveLight}">
<f:attribute name="rowKey" value="#{licht['id'].stringValue}" />
</a4j:ajax>
</rich:column>
...
...
<h:outputText value="This is the id : #{placeholder_control.selectedLight}" id="testingID"></h:outputText>
The managed bean placeholder_control looks like this
#ManagedBean (name="placeholder_control")
#SessionScoped
public class ControlPlaceholder {
public void selectActiveLight(ActionEvent evt) {
String selectedRow = (String) evt.getComponent().getAttributes().get("rowKey");
System.out.println("Selected Light : " + selectedRow);
setSelectedLight(selectedRow);
}
Everything will be rendered correctly, but if I click on the row (on this column), nothing happens. I also tried to put a non existing method (on purpose) to the listener. I hoped that I get an error message but nothing happend.
If I look at the HTML source code, there is nothing with onclick at that <td> element.
Anyone has an idea?
hi friend take a look to rich:extended datatable, i used it to do a task that meet your requirements.
here is the showcase for richfaces 4 and explains the use of extended datatable: http://richfaces-showcase.appspot.com/richfaces/component-sample.jsf?demo=extendedDataTable&sample=exTableSelection&skin=blueSky
cheers

Categories