Trouble saving selected values in selectManyListbox - jsf-2

We're having some problems with saving the selected values in a h:selectManyListbox.
What happens is that only the last value selected gets saved.I've placed a breakpoint in the components setValue(List) method.
When just selecting/deselecting everything seems fine, setValue is called, the list of strings it receives as argument is filled with one or more strings.
But, when pressing our "Save" button the setValue method gets called an additional time, this time with the list argument consisting of only ONE element, effectivly overwriting any previous values!
This additional call to setValue() occurs before even reaching our save button code.
Our system setup is Liferay 6.1.1 bundle with Tomcat7 with jsf 2.1.
Anyone has a clue what the problem might be ?

have you tried to declare in your backbean a list like String[] selectedValues?
for example:
<h:selectManyListbox value="#{backBean.selectedValues}">
<f:selectItem itemValue="" itemLabel="" />
.....
</h:selectManyListbox>
ZTB.

Related

paper-menu with multiple selections, howto deselect all

I use paper-menu with multiple selections (multi). Everything works fine so fare, but with a deselect all method things seems more complicated.
With html
<paper-menu multi selected-values="{{selectedValues}}">...
Dart
#property
List<String> selectedValues = [];...
Things got binded, and every iron-select/iron-deselect event results in a correct update of the selectedValues list in dart.
With clear('selectedValues') the list empties and the logic behaves like no selection is done, but in Dartium the items that previous was
selected remains marked as selected.
I have also tried with the selectedItems List or with the foreach deselect with the select method to PaperMenu, but still not successful update
in Dartium.
Anyone with ideas how to implement this?
Found a workaround for the issue with the select method. The menu with selected values can be replaced with a new similar element created with the Dom api. One drawback is the bindings can't be set up, so these needs to be hacked with get and set methods at the element. Otherwise this seems to work ok. The calls to the Dom api are shown below.
ParticipantMenu oldPm = $$('#id_filterselection') as ParticipantMenu;
ParticipantMenu newPm = document.createElement('participant-menu');
Polymer.dom(parentNode).insertBefore(newPm, oldPm);
Polymer.dom(parentNode).removeChild(oldPm);
PolymerDom.flush();

Sorting of primefaces dataTable with p:columns and converter

I have a primefaces data table with dynamic colums:
<p:dataTable value="#{curSearch.getSearchResults()}" var="curSearchResult" sortMode="multiple" rowKey="#{curSearchResult.getUniqueId()}">
<p:columns value="#{curSearch.determinePrimaryPropertyNames()}" var="curPrimaryPropName" sortBy="#{curSearchResult[curPrimaryPropName].getValue()}">
...
</p:columns>
</p:dataTable>
In most cases the value of sortBy expressions fits (e. g. for a date or a string), but I have one special data type which must be translated. This means sortBy="#{curSearchResult[curPrimaryPropName].getValue()}" delivers a string which must be converted in a i18n string. This i18n string should be sorted then. Unfortunately the attribute sortBy throws an IndexOutOfBoundsExcpetion if the expression does not contain brackets []. My idea was to call a method and distinguish there between the "normal" values and the values which must be translated. So my 2nd idea is to define a converter for the value which has to be translated. But is converted value evaluated by primefaces? I guess not. Is there maybe another approach for this?
I'm using something like the following code snippet in a Lazy Loading data table and it's working fine.
<p:columns value="#{listController.ColumnModel}"
width="#{column.width}" var="column" field="#{column.field.fieldName}" sortBy="#{ticket[column.field.fieldName]}"
columnIndexVar="colIndex">
I'm storing the field name in the column model as you can see. In the backing bean method used for loading table data you will receive a list of sortmeta so you can sort your result set. I hope this can help you.

Activate xforms button when instance is valid, inactivate otherwise

I have an xforms instance that I have a number of binds set up for so I can warn a user about input errors.
When he is done he needs to be able to submit the data from the instance. I would like to toggle 'active' on the button depending on whether or not the instance is valid.
What's the best way to 'attack' this problem? I'm currently using a group around the button that basically repeats what the model bindings already said which feels redundant and is error prone because of out of sync logic.
Also: this instance has 3 bindings, but I have others with 30-40 bindings.
Current code:
<xforms:group ref=".[instance('new-terminology-association')[matches(#code,'^\S+$')][matches(#codeSystem,'^[0-2](\.(0|[1-9][0-9]*))*$')][string-length(#displayName)>0]]">
<fr:button>
<xforms:label ref="$resources/create-association"/>
<xforms:action ev:event="DOMActivate">
...
</xforms:action>
</fr:button>
</xforms:group>
You could use the xxf:valid() function, pointing to the nodes you want to be valid. You can also point that function to a "parent" node, and ask it to check everything "under" that node is valid.
I think that function does what you're looking for, but because field values are only sent when users stab out of the field, this can create a somewhat unexpected user experience. For instance, imagine that the last field of your form, showing just before your button, is required. The user focuses on that field, and types a value. At this point the button is still disabled, since the value hasn't been sent to the server yet. Now the user hits tab, the value is sent to the server, the button would become enabled when the Ajax responses received, but since the button wasn't enabled at the time tab was pressed, the focus goes on something other than the button, which is somewhat unexpected. So, this is something to keep in mind.
There is absolutely no way in Orbeon 4.7 to make the button/trigger respond directly to xxf:valid. This looks like a bug to me. Workaround code: add observer for xxforms-valid and xxforms-invalid that set true|false in a new instance. Add readonly binding based on ".='false'" for that instance and use ref="instance" on the trigger. On a busy form with lots of buttons that is a bit of a waste, but it'll have to make due. Thanks for you help, appreciated!
<xf:instance id="button-control">
<button btn-term-add="false"/>
</xf:instance>
<xf:action ev:event="xxforms-invalid" ev:observer="new-terminology-association">
<xf:setvalue ref="instance('button-control')/#btn-term-add" >false</xf:setvalue>
</xf:action>
<xf:action ev:event="xxforms-valid" ev:observer="new-terminology-association">
<xf:setvalue ref="instance('button-control')/#btn-term-add">true</xf:setvalue>
</xf:action>
<xf:bind nodeset="instance('button-control')">
<xf:bind ref="#btn-term-add" readonly=".='false'"/>
</xf:bind>
....
<xf:trigger ref="instance('button-control')/#btn-term-add">
....
</xf:trigger>

PrimeFaces: How do I declare and bind an AJAX "rowEdit" event to p:dataTable programmatically?

I'm still developing my data table UI application, and finally I'm about the final stage of the development of this component: inline cell editing.
First of all, the data table is built fully dynamically at the Java side, and no facelet declarations are used to describe the table. If I'd have a static table declaration, the editing could be specified like this (see the In-Cell Editing chapter):
<p:dataTable>
...
<p:ajax event="rowEdit" listener="#{tableBean.onEdit}"/>
...
</p:dataTable>
I can easily specify the data table editable with dataTable.setEditable(true) in the Java code - and it works, please note, the editing Save/Cancel icons are working nice but have no effect at the back end. Since I cannot it declare in the way specified at the PF ShowCase Labs page (must I always use the listeners there?) because of the data tables are rendered dynamically, I'm trying to use the following:
public static AjaxBehavior createAjaxBehavior(MethodExpression expression) {
final AjaxBehavior behavior = new AjaxBehavior();
behavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(expression));
return behavior;
}
...
dataTable.addClientBehavior("rowEdit", createAjaxBehavior(createMethodExpression(TableBean.class, "onEdit", void.class, new Class<?>[] {RowEditEvent.class})));
But as soon as I add the rowEdit listener, like I'm trying to do above, and wow I suddenly got: mojarra is not defined and no Save/Cancel row edit buttons are working. Ok, I've found a similar problem described and resolved here, included the necessary script manually, and now the client-side JavaScript error is gone, however I still cannot exit the row editing mode, and the row is still not updated.
I wasted all day trying to figure out what's going on, and I'm blind to see the correct way. Do I simply miss something behind (like identifying a certain row, or probably specifying something else somewhere -- but my Java code does not generate anything more than specified in the PF example), or anything whatever?
Thanks in advance.
Well, I've just figured out the real reason in the following method:
public static AjaxBehavior createAjaxBehavior(MethodExpression expression) {
final AjaxBehavior behavior = new AjaxBehavior();
behavior.addAjaxBehaviorListener(new AjaxBehaviorListenerImpl(expression));
return behavior;
}
In fact, the method actually returned javax.faces.component.behavior.AjaxBehavior (h:ajax?) instead of org.primefaces.component.behavior.ajax.AjaxBehavior (p:ajax) -- this happened because of quick auto-complete so I simply missed that fact.
I'm frustrated that the PrimeFaces library didn't reply any error.
Just to complete the Q & A:
Mojarra 2.1.7
PrimeFaces 3.2
Should now (at least for PF 6.0) be org.primefaces.behavior.ajax.AjaxBehavior.

What is the correct code pattern to fetch data base entries?

In two other questions (here and here) BalusC makes a straight up declaration:
Getters are solely there to access bean properties, not to do some business logic. There you have the bean constructor, initialization blocks or event methods for. All are executed only once during bean's life and that's exactly what you want.
Well gee -- this just invalidated a gazillion lines of code I have already written. Ok, then, what is the correct way of implementing a backing bean that fills a data table? I understand his point and the concept, but not the practice. My question is twofold:
Why is the way I am doing it wrong?
How do I fix it?
I use PrimeFaces p:dataTable a lot, and it's value attribute resolves to a collection. For reasons I don't go into here, I do not use PrimeFaces' lazy table loading feature. Instead I implement my own filter/sort controls, but they trigger AJAX events, which then results in the table being filled with records fetched from the data base.
The table is marked up like this:
<p:panel id="mqTable">
<h:outputText value="Sort/Filter: #{maintCategory.tableQueryParameters}" />
<p:dataTable
id="mqDataTable"
rows="#{maintCategory.pageSize}"
value="#{maintCategory.dataModel}"
selection="#{maintCategory.selected}"
var="cat"
selectionMode="single"
emptyMessage="No Categories Found">
Now the INCREDIBLY BAD UN-JSFish (or so I just found out) getter for dataModel goes like this:
public ATMDataModel getDataModel() {
TableQueryParameters p = getTableQueryParameters();
if (p.isChangePending()) clearDataModel();
p.setChangePending(false);
if (dataModel != null) return dataModel;
List<ET> list = getDAO().runQuery(p);
if (p.isNeedResultSize()) p.setResultSize(getDAO().runQueryCount(p));
dataModel = new ATMDataModel(list);
return dataModel;
}
A few explanations.
This is from an abstract super-class where ET is the "Entity Type." All my CRUDs use this same routine.
The class ATMDataModel is a wrapper for the list which implements SelectableListModel. The row selection logic in PrimeFaces requires this. (It is a pain that appeared in PF 3 but it makes row selection work more reliably.)
The class TableQueryParameters is something I wrote to encapsulate the current state of the table on the user's screen. It includes what sort parameters, what filter parameters, what page we are on, etc. Because this needs to be preserved, the backing bean is ViewAccesScoped (via MyFaces CODI) and the TableQueryParameters is a property within it.
The TableQueryParameters are updated in response via AJAX events, which also update the form causing getDataModel to be called. The method isChangePending goes true when anything changes. So the getDataModel method uses this to generate only one fetch from the DAO between changes no matter how many times it is called.
BUT if the TableQueryParameters do change, I have to call runQuery with those parameters to fetch the new set of records the user wants to see. If I don't call it in getDataModel where do I call it?
Please advise.
You're basically lazily loading the data in the getter. You're not hitting the DB on every getter call within the same request (or view) scope. This is affordable. I don't use CODI, but I'd imagine that the getTableQueryParameters() call is also particularly cheap and nothing to worry about.
As to the concrete question, you'd normally do the DB/business job in an action(listener) method which is attached to the UICommand component and/or the ajax event tag.
For example (works also as <p:commandButton action> though)
<p:ajax listener="#{bean.deleteSelectedRow}" />
...
<p:ajax listener="#{bean.saveRowDetail}" />
with
public void deleteSelectedRow() {
someService.delete(selectedRow);
dataModel = loadDataModel();
}
public void saveRowDetail() {
someService.save(selectedRow);
dataModel = loadDataModel();
}
Depending on the meaning of p.isChangePending(), I think you could also get rid of it this way, it look like that you were setting it in the action(listener) methods.

Resources