-
FEATURED COMPONENTS
First time here? Check out the FAQ!
Dear Support,
I m using chosenbox to save selectedobjects to database and I want to retrieve those db values into chosenbox as selected value. Can you please help how I can achieve this? Thanks!
With Best Regards, Amol
I assume you already know how to save/load data into your DB (since persistence layers are not part of ZK I'll not cover those details here)
If you're using Chosenbox I assume you use a ListModelList
(or another ListModel implementation supporting Selectable
) for the available items. By doing so this ListModelList
can serve both your requirements.
e.g.:
ListModelList<String> namesModel =
new ListModelList<>(Arrays.asList("Adam", "Eve", "Harold", "Spencer"));
namesModel.setMultiple(true);
chosenBox.setModel(namesModel);
Now the user can pick from the available names in the model, the namesModel
will keep track of the current selected names.
In order to retrieve the current selection (saving into DB) you call:
Set<String> selectedNames = namesModel.getSelection();
yourService.saveSelectedNames(selectedNames);
When loading a previous selection you simply add the loaded names to the selection of the names Model.
Collection<String> selectedNames = yourService.loadSelectedNames();
namesModel.setSelection(selectedNames);
or add/remove them one by one:
namesModel.clearSelection();
namesModel.addToSelection("Peter");
namesModel.addToSelection("Harold");
//namesModel.removeFromSelection("Peter");
Does this help? If you needed something different please consider providing some runnable code on https://zkfiddle.org/ and post the link here.
Many Thanks for your help and advise! Yes its helpful and Since, I am new to ZK Please let me digest this and update. Further more info, I am using MVVM pattern and used "selectedobjects" with command @save to pass it to VM and then db. Now I am trying to load those db value into chosenbox as selectedobjects.
With Best Regards, Amol
Asked: 2021-05-16 03:29:26 +0800
Seen: 12 times
Last updated: May 17 '21