How to get values from ui:repeat in a html form - jsf

I have resolved my previous problem posted in:
ui:repeat, populate list
Now I would like to know how to get the values modified in the form and pass to my bean.
This is how I done:
in the form:
<ui:repeat value="#{myBean.myList}" var="item">
<td class="icePnlGrdCol1" id="nacionI-0-#{item.index-1}">
<input class="iceInpTxt celdaNacionI"
id="I#{item.index gt 9 ? '':0}#{item.index}"
name="I#{item.index gt 9 ? '':0}#{item.index}"
title="I#{item.index gt 9 ? '':0}#{item.index}" type="text"
value="#{item.valor}" />
</td>
</ui:repeat>
in the bean:
private List iniciosMesList = null;
CeldaGrid is a class with their getter/setter
I want the get the value of every inputText in the form an re-create the list before to pass to my database.
How can I access this values???
thanks

Use an <h:input> instead of <input>. Provide a valueChangeListener attribute that binds to your backing bean. For each value in the list that the user has changed, this listener will be called. You can additionally provide or grab the item iteration variable with this call back.
This way you can construct a list of all items that have changed, and send these to your DB. Or, if merge them with your original list and send that to the DB.
(p.s. I advice you not to use a raw List as in private List iniciosMesList = null;, but parameterize it correctly)

Related

Problem with data table selection and how to access var outside of repeating block

I have a screen that I show some records with datatable from primefaces
enter code here
1) In my bean, I have two lists, the first one I enter the records returning from the database,
and the other is used to store the selected screen items.
#Getter #Setter
private List<RecorsDTO> recordsList;
#Getter #Setter
private List<RecorsDTO> selectedRecordsList;
In another screen, I show the IDs selected in the previous screen, for that I used the repeat
function of primefaces
<h:panelGroup>
<h:outputText value="#{selectedRecords.id}" />
</h:panelGroup>
The problem here is that it only shows the first list item, for example:
Records Selected in first screen: 100, 101
Records displayed on second screen: 100
2) I also need to show other attributes of the List, but these outside the repeat block, for example:
<h:panelGroup>
<h:outputText value="#{selectedRecords.id}" />
</h:panelGroup>
</ui:repeat>
<h:panelGroup>
<h:outputText value="#{selectedRecords.nome}" />
</h:panelGroup>
The problem here is that I can't access var = selectedRecords out of the repeating block, I tried to use
the dataGrid component to have is a var visible inside and outside the repeating block, but it didn't work either.
Any ideas that might help me in this case?
thanks for your feedback, I managed to solve the problem as below:
1) This issue was related to duplicate records, my Bean myBBean.selectedRecordsList was returning two records
equal (100, 100) and then within the repeat block he would perform a sort of "DISTINCT" and display only a record (100).
2) The var = selected records that I defined within block repetition are valid only within it and for access outside of
repetition block, I failed not to mention the index as it is a list as below
value="#{myBBean.selectedRecordslist.get(0).name}"

Changing a result from p:autoComplete's completeMethod before using it

I'm implementing a dialog for registering an address. In the street name field, I'm using PrimeFaces' <p:autoComplete> tag to help the user find the name of their street. The completeMethod works as expected, but to avoid confusion between similar street names I would like the drop-down list to also include the municipality the given street is in, for instance on the format "<street name>, <municipality name>".
I don't want the municipality name to be included in the actual field, so I've concluded that I need a method that performs some sort of string manipulation (substring using the position of the first comma, for instance), but I can't figure out where such a method would be called from. I've had a look through the PrimeFaces documentation, but I haven't been able to find anything that would allow me to do this. Is this at all possible in <p:autoComplete>? Alternatively, is there another autocomplete implementation which supports this, or would I have to implement my own javascript component?
EDIT: This is what the xhtml code I'm using looks like:
<div class="form-group row required">
<h:outputLabel value="#{msgs['#common.mailingAddress']}" for="address" styleClass="col-xs-12"/>
<p:autoComplete
id="address"
name="address"
size="50"
maxlength="50"
styleClass="col-xs-12 street-name"
label="#{msgs['#common.search']}"
disabled="#{not configurationController.cardCtrl.editable}"
value="#{configurationController.cardCtrl.selected.address}"
required="true"
completeMethod="#{configurationController.cardCtrl.autoCompleteTest}">
<f:validator binding="#{onlyLettersOrDigitsValidator}"/>
</p:autoComplete>
<h:message id="addressMessage" for="address" styleClass="inline-error inline-error-small"/>
</div>
The autoCompleteTest method in the controller is as follows:
public List autoCompleteTest(String input) {
AddressSearch addressSearch = AddressSearch.builder()
.streetName(input)
.municipality(municipality.getName())
.maxResultsPerPage(10)
.build();
return addressesToStreetNames(mapService.addressSearch(addressSearch).getAddresses());
}
With a helper method addressesToStreetNames which takes a list of Address objects and returns an ArrayList<String> containing those addresses' street names.
EDIT2: Based on suggestions in the comments, I tried setting itemValue and itemLabel to different values, to see if that had any effect. The new xhtml looks like the above, with the addition of the following three lines:
var="address"
itemValue="#{address.streetName}"
itemLabel="#{configurationController.cardCtrl.formatAddress(address.streetName, address.postTown)}"
The autoCompleteTest method now also returns the Address object directly rather than a String representation of the street name, so that these fields are available. The formatAddress method is simply return streetName + ', ' + postTown;
This causes the dropdown list to look how I want it to look, but when I click an item it still inserts the whole string with both street name and post town/municipality into the text field (and in fact, before I've written anything, the text field already contains ", ").

Assign a bean property to a JSF variable through a commandLink [duplicate]

This question already has answers here:
How can I pass selected row to commandLink inside dataTable or ui:repeat?
(4 answers)
Closed 6 years ago.
Basically what I have is a list called "mep", I display its values with commandlink and all i want is once you select a link to assign its value to my bean property "selectMep", the display part works pretty well and I'm struggling with the assignment part as I get an error which says that a String (the type of my bean property) cannot be casted to a UIcomponent, here's my code:
<ui:repeat var="mep" value="#{helloBean.mep}" >
<tr>
<td>#{mep}</td>
<h:commandLink value = "#{mep}" action="" binding="#{helloBean.selectMep}"/>
</tr> <br></br>
</ui:repeat>
Any suggestions?
I would suggest to use the action of the commandLink, like this:
<h:commandLink value="#{mep}" action="#{helloBean.selectMep(mep)}" />
And add a new method to your bean:
public void selectMep(String val) {
System.out.println(val);
}

Get id from current row dataTable to link JSF Primefaces Java [duplicate]

This question already has answers here:
How can I pass selected row to commandLink inside dataTable or ui:repeat?
(4 answers)
Closed 6 years ago.
How can i get Id of current Row p:dataTable primefaces to my link where is used this Id? Describe it (sorry for my English). My dataTable looks like on this picture:
http://www.image-share.com/igif-2333-105.html
When i click the button i want to get Id of row where is this button, for example row 1 have id 1, so when button 1 is clicked i must get ID = 1. Show how looks my link where need id:
http://'serverAddress'/myApplication/PDF?type=document&id="+id"
Variable id have type int and getters, setters in entity and bean. Button in JSF looks like below:
h:commandButton value="Print" action="#{printBean.print()}"/>
Method 'Print' initiate my link. Any ideas how to do this? Thanks for help.
You can use p:dataTable's rowIndexVar attribute.
Name of the iterator to refer each row index
Whose iterator name can be used in EL to get the Row Id
For Example:
if rowIndexVar="myId" then you can access each row Index using that iterator name in EL using #{myID}.
Example:
<p:dataTable rowIndexVar="rowId" value="..." var="...">
<p:column>
<h:commandLink action="#{...}">
<f:param name="id" value="#{rowId}" />
</h:commandLink>
</p:column>
</p:dataTable>
Get id in bean
map<String,String> par_map = new HashMap<String,String>(); par_map=FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
String id=par_map.get("id");

How to save h:inputText values of a h:dataTable? My attempt only saves the value of last row

I'm having trouble making a dataTable where each row has a inputText and a commandLink. When the link is clicked, only it's row's inputText's data is submitted.
Something like this?
<h:dataTable value="#{bean.items}" var="item">
<h:column>
<h:inputText value="#{bean.value}"/>
</h:column>
<h:column>
<h:commandLink action="#{bean.save}" value="save">
<f:setPropertyActionListener target="#{bean.item}" value="#{item}" />
</h:commandLink>
</h:column>
</h:dataTable>
Bean:
#RequestScoped
public class Bean {
private Item item;
private String value;
Right now, as it is, it's using the last row's inputText to fill the value. I wrapped another h:form, but it broke other things and I've learned that nested h:form is not the right way to do it hehe
What's the correct way to do this?
Thanks.
You're binding the value of all HTML input elements to one and same bean property. This is of course not going to work if all those HTML input elements are inside the same form. All values are subsequently set on the very same property in the order as the inputs appeared in the form. That's why you end up with the last value. You'd like to move that form to inside the <h:column> (move; thus don't add/nest another one).
The usual approach, however, would be to just bind the input field to the iterated object.
<h:inputText value="#{item.value}"/>
An alternative, if you really need to have your form around the table, is to have a Map<K, V> as bean property where K represents the type of the unique identifier of the object behind #{item} and V represents the type of value. Let's assume that it's Long and String:
private Map<Long, String> transferredValues = new HashMap<Long, String>();
// +getter (no setter necessary)
with
<h:inputText ... value="#{bean.values[item.id]}" />
This way you can get it in the action method as follows:
String value = values.get(item.getId());
By the way, if you happen to target Servlet 3.0 containers which supports EL 2.2 (Tomcat 7, Glassfish 3, etc), then you can also just pass the #{req} as a method argument without the need for a <f:setPropertyActionListener>.
<h:commandLink ... action="#{bean.save(item)}" />
See also:
How and when should I load the model from database for h:dataTable
How can I pass selected row to commandLink inside dataTable?
How to dynamically add JSF components

Resources