Primefaces selectCheckboxMenu how to get the last checked item - jsf

I am using selectCheckboxMenu from primefaces, but i need to know which item was checked here is my code:
<p:selectCheckboxMenu
label=""
id="cboBiblioteca"
filter="true" filterMatchMode="contains"
required="false"
widgetVar="cboBiblioteca"
value="#{documentalBean.sbiblioteca}"
style="width:20px">
<p:ajax event="change" listener="#{documentalBean.checkBiblioteca()}"/>
<f:selectItems value="#{bibliotecaBean.bibliotecas}" var="biblioteca" itemLabel="#{biblioteca.NOMBRE_BIBLIOTECA.toLowerCase()}" itemValue="#{biblioteca.ID_BIBLIOTECA_MEDIADOR}"/>
</p:selectCheckboxMenu>
my "sbiblioteca" array is a String array, when i whatch it on the debugger i cant get the last checked, because they arent added to the last they are added according to their position in the list.

Store the 'last -1' list and compare with the 'last'... Simple... Just like you would in plain java... See differences between two arrays

Related

use of cellEditor in facet not working primefaces

In a project, I have to merge two datatables (the first to set the header of the second and the second displaying the informations contained in an uploaded file) into one to simplify the legibility on the page.
Since the first datatable was using a cellEditor I tried using one in the merged one the following way for the header:
<f:facet name="header">
<p:cellEditor>
<f:facet name="output">
<h:outputLabel id="output" value="some text" />
</f:facet>
<f:facet name="input">
<p:selectOneMenu value="#{handler.selectItems}">
<!-- List of selectItems -->
<p:ajax event="change" update="output" />
</p:selectOneMenu>
</f:facet>
</p:cellEditor>
</f:facet>
But when I build the project and click on the header of my columns, I don't have any list of selectItems appearing to give me the possibility to change the header. It was used in <p:column> before without problem so I imagine the <f:facet> is the problem
I succesfully made the possibility to change the value of the header using <p:inplace> instead, but it doesn't behave exactly like I want.
I want that even if I don't modify the value when I click on the header (and make the list appears), I can click somewhere on the page and make the list disappear to have a "clean" header. I don't really want to use the editor="true" option of inplace because the goal is also to try to reduce the number of click needed for the user. I also made it a bit using <p:ajax event="blur" update="output" /> but some value of the list of selectItems make one or two inputText appear. And with that event, when I click in the text box, it closes the field automatically and I can only the value of the field once. After, the moment I click on the list to open it, it closes the field.
How can I fix that ?

Dynamically setting value of a h:selectOneMenu using c:forEach

I'm working on a project that requires me to display and be able to select and store tags to the product. Tags are provided in tree-like structure. I can't assume maximum depth of a tags tree.
I wanted to display tags split by levels, using c:forEach - p:selectManyCheckbox - f:selectItems, and handling selections using p:ajax components.
I use following types to store possible values and selections in Tree object:
HashMap<Long, ArrayList<Tag>> tree;
HashMap<Long, Object[]> selected;
Hashmap keys are equal to "tag level".
In order to display values I use following code for testing:
<p:panelGrid id="tagDisplay" columns="2">
<c:forEach begin="1" end="5" var="idx">
<p:outputLabel value="#{idx}"></p:outputLabel>
<p:selectManyCheckbox value="#{product.tags.selected[1]}">
<f:selectItems value="#{product.tags.tree[1]}" var="tag" itemLabel="#{tag.name}" itemValue="#{tag.id}" />
<p:ajax listener="#{product.selectorListener}" update="tagDisplay" />
</p:selectManyCheckbox>
</c:forEach>
</p:panelGrid>
Code seemed to work fine, though displayed five times.
Now I'm stuck trying to dynamically bind Hashmaps to selectors. As I replaced "1" with "idx", I got no results.
I tried to use ui-repeat with a dummy table, but then I lost panelgrid structure.
Any help will be appreciated!
My environment - Websphere 8.5, JSF 2.2, Primefaces 5.2
The <c:forEach begin end> is only for static iteration, not for dynamic iteration.
You'd better iterate over #{product.tags.tree} itself in <c:forEach items>. Each iteration over a Map will give Map.Entry back which in turn has getKey() and getValue() methods.
<p:panelGrid ...>
<c:forEach items="#{product.tags.tree}" var="entry" varStatus="loop">
<p:outputLabel value="#{loop.index}"></p:outputLabel>
<p:selectManyCheckbox value="#{product.tags.selected[entry.key]}">
<f:selectItems value="#{entry.value}" ... />
...
</p:selectManyCheckbox>
</c:forEach>
</p:panelGrid>
That said, must it really be a HashMap? Don't you rather want a fixed ordered LinkedHashMap?

populate primefaces autocomplete with dropdown

In JSF using primeface
<p:autoComplete minQueryLength="5" value="#{itemManagementMB.itemManagementLazy.code}" forceSelection="true" autocomplete="false"
completeMethod="#{itemManagementMB.autoCompleteUsindCode}" dropdown="true">
<p:ajax event="itemSelect" listener="#{itemManagementMB.handleSelect}" />
</p:autoComplete>
When I enter 5 keywords that working fine. but I want implement in that way , if user enter 4 words and press dropdown it populate autocomplete based on 4 keywords.
One thing is blocking me that when I click on dropdown, autoCompleteUsindCode(String query) method take empty string and itemManagementMB.itemManagementLazy.code also empty.
any update ? / solution?
As per Primefaces 5.1 User Guide page 29, I quote:
Dropdown Mode
When dropdown mode is enabled, a dropdown button is displayed next to
the input field, clicking this button will do a search with an empty
query, a regular completeMethod implementation should load all
available items as a response.
After thinking a lot, I solved in that way
Solution:
<p:autoComplete id="anum" minQueryLength="5" value="#{transferInMB.itemManagementLazy.code}" forceSelection="true"
completeMethod="#{transferInMB.autoCompleteUsingCode}" dropdown="true">
<p:ajax event="itemSelect" listener="#{transferInMB.handleSelect}" />
<p:ajax event="keyup" />
</p:autoComplete>
I just add the following
<p:ajax event="keyup" />

Use Enum for filtering in p:dataTable

How to use an Enum to filter in p:dataTable?
My approach looks like this:
<p:column filterBy="#{item.EMyEnum}" filterMatchMode="in" >
<f:facet name="filter">
<p:selectCheckboxMenu label="Select" onchange="PF('datatable').filter()">
<f:selectItems value="#{MyEnum.values()}" />
<p:ajax event="toggleSelect" onsuccess="PF('datatable').filter()"/>
</p:selectCheckboxMenu>
</f:facet>
<h:outputText value="#{item.EMyEnum}"/>
</p:column>
Everything looks fine, but when I select one item from the drop down, the whole content is filtered. When I deselect everything in the drop down, then the content comes up again, so basically the filter seems to work, but every comparison is evaluated to false. I do not understand why, as JSF has build in Enum-converters hasn't it? But what am I missing or doing wrong here?
I am working with PF 5.1 on Mojarra 2.1.29
I solved my problem with usage of a List of my Enum types in combination with the OmniFaces GenericEnumConverter. This works like charm, but I'd still like to know what exactly the problem with the previous solution is and how it could be fixed.

JSF Selectitems Format Label Number

I've got a List of Numbers (range 500 - 5000, steps of 500).
I would like to add a decimal dot 1000 -> 1.000; 2500 -> 2.500 etc. but just for the labels not to be saved as a value.
I tried the following but it didnt work:
<h:selectOneMenu value="#{bean.selectedValue}">
<f:convertNumber type="currency" locale="de-DE" pattern="#,###" />
<f:selectItems itemValue="#{bean.selectItemslist}" var="item" itemLabel="#{item.label}" itemValue="#{item.value} />
</h:selectOneMenu>
But this didnt do anything :(
Tried several patterns and included integerOnly="true" but nothing seems to work :(
Thanks for your help!
The converter applies on the item value only, not on the item label. That explains why it "fails". In this particular case, your best bet is to create a custom EL function, so that you end up something like this:
<f:selectItems ... itemLabel="#{my:formatNumber(item.label, '#,###')}" />
The JSF utility library OmniFaces has several, see also OmniFaces functions/Numbers showcase.

Resources