String concatenation question

advanced-prototyping

#1

Hi all,

I need your help! :slight_smile:

Just working on something at work so canā€™t give too many details away unfortunately but Iā€™ll try my best! Weā€™re working on an automated generation of a sentence based on user input. I am trying to get checkboxes to generate on the next page.

So, based on what the user has selected above, we need this to be placed into a global variable (or whatever!) that can be printed on the next page. If someone selects Analytics + Heatmaps + Survey then the string would need to output Analytics, Heatmaps, Survey (in any order). All errors must also be accounted for - selection, unselection and also the order in which these checkboxes are selected and unselected. Iā€™ve got the concatenation working by the means of <[[v2_evidenceboxestotal]], Usability testing> and removal by the means of ,[[v2_evidenceboxestotal.substring(0,v2_evidenceboxestotal.length-17)]]>. The problem Iā€™m having is that based on the order I select and unselect, the unselection would remove off the back of the string which is incorrect in some cases.

Does anyone have any ideas? Iā€™ve had a look at some examples of RP files that people have posted but none work properly!

Many thanks in advance,
Anthony


Nested Repeater Alternative?
#2

To clarify:
If I select A, then C, then B, the variable should reflect A+C+B. But if I then uncheck B, the variable should reflect A+B?

If so, this seems pretty simple. Assuming our final variable is called String:

Every checkbox will have the same cases, you can copy/paste it to each one. Or you can copy/paste the checkboxes and just change their text.

OnSelectedChange
(If is selected of This equals true)
Set value of variable String to: [[String + ā€˜$ā€™ + This.text]]
(Else if true)
Set value of variable String to: [[String.replace(ā€™$ā€™ + This.text,ā€™ā€™)]] //Note that the second parameter is two single quotes (an empty string).

The reason we use the dollar sign is so if we concatenated ā€œbath, elm, theā€ we donā€™t end up with ā€œbathelmtheā€ where ā€œtheā€ appears twice now. Instead we get ā€œ$bath$elm$theā€ so we know for sure where the word boundaries are.


#3

Thanks for your reply.

I see what youā€™re saying but we need the output to come out as a perfect string with no $ signs in it as it will be an auto-generated paragraph. Also, I believe your version doesnā€™t allow for unchecking properly - Iā€™ve just tried it and selecting/unselecting multiple checkboxes doesnā€™t always clear the string as expected. Iā€™m currently trying to create multiple variables for each checkbox and then deal with the printing on the resulting page with many if statementsā€¦


#4

I see what youā€™re saying but we need the output to come out as a perfect string with no $ signs in it

You can totally do that! This is only the first step not a complete solution. This gets you: (1) The words the person selected (2) in the order they selected them. From here you can do whatever you want with it.

Hereā€™s a working example attached. It works perfectly on my end so itā€™s possible I just didnā€™t understand exactly what it is you wanted. Iā€™d added a sample output as well that removes the dollar signs.
checkbox-words.rp (54 KB)


#5

Ah, sorry - I misunderstood you too! Thanks for this, it does the same thing as what Iā€™ve currently got but in a more simplified way.

The thing Iā€™m now struggling with is ensuring that commas are added after the first checkbox. So, if the user selects Checkbox and tree then the output needs to be ā€œCheckbox, treeā€. If checkbox is then unselected, the result needs to read ā€œTreeā€. Does that make sense? Essentially ā€˜if itā€™s the first value then it should not have a commaā€™. The output order does not matter, so long as it reads ā€œFirst item, second item, third itemā€. Itā€™s doing my head in, Iā€™ve been working on it for half of today!:confused:


#6

I see two additional features you want:

  1. Add a comma after each item, except if itā€™s the last item (if thereā€™s only 1 item, itā€™s both the first and last item so holds true).
  2. Capitalize the first word only (sentence case).

Since most of the items will have a checkbox after them, letā€™s start by always adding one in by modifying this action to add a comma:

Set value of variable String to: [[String + ā€˜$ā€™ + This.text + ā€˜,ā€™]]
And the removal action needs to account for the comma too:
Set value of variable String to: [[String.replace(ā€™$ā€™ + This.text + ā€˜,ā€™,ā€™ā€™)]]

Now when we display the text, we need to do two things: remove the dollar signs and remove the last comma. This is easy:

Set text: [[String.replace(ā€™$ā€™,ā€™ ').substring(0, String.length - 1)]]

This should work for the commas. The capitalization bit is trickier, but what I would do is normalize everything to lower case in the first two actions above, and then in the last display step capitalize the first letter.


#7

Got it working, thanks for all your help!


#8

I am working on a similar problem, just with days of the week. I have it working with all of the help provided (including the commas), though I am not concerned with the capitalization. However, the new wrinkle I need is sorting. In other words, I always want it to be in the order of: Sun, Mon, Tue, Wed, Thur, Fri, Sat. If one day is removed, it should still follow the same order. If one day is added after, it should insert itself at the appropriate spot. The output could be:

Sun, Wed, Fri
or
Mon, Thur, Sat
or
Tue, Wed, Thur, Sat
or whatever.


checkbox-words.rp (60.6 KB)

Thanks!


#9

Take a look at the attached. I added a second set of checkboxes using a repeater to accomplish what youā€™re looking for.

The repeater has two columns - one with the name, one with a checked indicator.

Within the repeater, when you check the box it sets the indicator to true. When you uncheck, it sets it to false.

Then onItemLoad, all the magic happens (because every time you check or un-check the box the items reload).

Here are the cases:

1 - sets the repeater label to the first column

2 - if itā€™s the first item in the repeater, we reset the text on output

3 - if Checked is true - we set the checkbox to checked and append the text of the checkbox to output as well as a comma

4 - if last item, remove the trailing comma

This will enforce that your order always matches your predefined order.
checkbox-words -repeater.rp (66.6 KB)


closed #10

unlisted #11