Pass multiple selected values as a single field, part 2: Support value repetitions
To the basic solution: Pass multiple selected values as a single field – 7.1+ version
Use case description
1. The manger runs a data service returning a list of orders.
2. According to presented details he selects some entries where the performance was exceptional. The contact person in each of these may be rewarded.
3. A second data service gets the name (or id) of each selected contact. In our case as one string field.
Limitation of the former solution
As commented in the first document, it does not support value repetitions. For example:
Step 1: The user may select several entries to reach the following string: Louie;Huey;Louie;Louie;
As seen in the screen shot, this leaves us with Huey;
This happens because the replace() function we use in the Action of deselect replaces each Louie; with an empty string. We now pass the next step a wrong string, one that does not reflect the user selections.
A possible solution
In general, we could use brackets . This could serve two purposes:
1. Separating between items.
2. Nesting items with the same value.
Adding an entry
If there is already x – replace x with x[x].
Otherwise – add x to the string.
If we already have: Huey
Adding Louies will be like this:
We nest each new Louie in the brackets of the previous one.
Removing an entry
Replace each x with an empty string.
Removing Louies shows is as follows (the deepest nested is the one removed):
Replace each ] with an empty string.
Optional: replace each [ with the desired delimiter, like ;
For the string: HueyLouie[Louie[Louie]]
We now get: Huey[Louie[Louie[Louie[
Or possibly: Huey;Louie;Louie;Louie;
This gets us back to the normal flat string representation with a delimiter.
Implementation in Visual Composer
The running application might look like this:
Step 1 – selection:
Step 2: Deselect one Louie entry.
Step 3: Pass the result string to the Data Service.
1. Add relevant Data Service to the model. Connect input and output:
2. To the table add a Boolean field in the Define Data dialog:
3. Add a Data Share element with a field to contain the user selections:
4. Adding an entry: In the Layout Board, add an Assign Action to the check box control.
=IF(CONTAINS(share1@selection, Orders@contact & “”),
REPLACE(share1@selection, Orders@contact & “”, Orders@contact & “[” & Orders@contact & “]”),
share1@selection & Orders@contact & “”)
5. Removing an entry: Add another Assign Action to the check box Control.
Assigned value: =REPLACE(share1@selection, Orders@contact & “”, “”)
6. Add a button on the table to trigger the activation of the second data service.
7. Add the second data service to the model – the one getting the selection result. In this example I used a Service Component model to echo the input.
8. Link the Data Share and the second data service. Set the event name of the link to *selection. The * is needed since the event is not coming from the link source – so that it’s a global event listening:
9. Finalization: In the mapping to the second Data Service we modify the selection string:
=REPLACE(REPLACE(@selection, “]”, “”), “[“, “;”)
This gets us the “normal” flat string representation.