Jump to content

Formatting an array of values


jeller

Recommended Posts

I am setting up a business card for a digital storefront using FusionPro. The customer has certain optional certifications that could appear after their name.

 

The customer wants the DSF user to be able to select from a list of certifications rather than enter the certifications manually, so I have set up a pick list as a multi-select box in my FusionPro HTML Form.

 

I have entered all of the possible certifications (AAMS®, CFP®, CFA, etc.) in the pick list as well as a “none” choice in case they don’t have any certifications. The FusionPro fields are as follows:

<Fullname><Certifications>

<Title1>

<Title2>

Q1: Does anyone know how the data is output from the storefront if multiple entries are selected from a multi-select box? Is it an array?

 

Q2: Assuming the data is an array, can anyone suggest a javascript that will extract each entry from the array, then format the certifications so they have a comma and space after each one.

 

* Keep in mind the options are none, one selection, or multiple selections.

 

The first line of the business cards should look like:

Joe Smith, AAMS®, CFA

I am envisioning a script that basically says, If the field Certifications = “none” then return “”, else return the field Certifications, but with formatting “entry1, entry2, entry3, etc.”

 

I can create basic javascripts using the FusionPro building blocks, but this may be out of my league. I don’t know how to handle the formatting part.

 

Can someone help me with this please?

Link to comment
Share on other sites

I have entered all of the possible certifications (AAMS®, CFP®, CFA, etc.) in the pick list as well as a “none” choice in case they don’t have any certifications. The FusionPro fields are as follows:

<Fullname><Certifications>

<Title1>

<Title2>

Q1: Does anyone know how the data is output from the storefront if multiple entries are selected from a multi-select box? Is it an array?

Well, this is really a question for EFI support, but I'm pretty sure that if you define a pick list with the HTML Form Definition (Web DataCollect) dialog in FusionPro, that it becomes a simple drop-down list in the web form, so multiple selections are not possible. Unless EFI has some other way of setting up a multi-select list.

 

At any rate, the DSF application can't really provide anything other than an input data file to FusionPro, with data fields and values in per-record key-value pairs. I don't see how it could possibly inject a JavaScript array into a rule.

 

So if DSF did have a multi-select list, it would probably put the selections in the data in one of two ways: It either (A) creates a list of fields with indices, something like "Certification1", "Certification2", etc., or (B) it sets the value of the Certifications field to some kind of delimited list, with the values separated by some delimiter other than the main data file delimiter. (So if the data file is, say, tab-delimited, it might put semicolon-delimited values in the data for the pick list.)

 

I suspect that you'll have to just set up multiple fields yourself, such as "Certification1", "Certification2", etc., and make them all pick lists, so that the end user can select multiples.

 

But you don't have to guess what the data field values coming out of DSF are. You have full access to all of the data in the FusionPro template. So you can write a rule like so:

var result = [];
for (var f in FusionPro.Fields)
   result.push(f + ": " + Field(f));
return result.join("\n");

And put that rule into a text box, then you'll see all the data field values for each record in the output.

 

Or you can see the field values in the composition log (.msg) file by changing the last line to this:

Print(result.join("\n"));

 

Once we know exactly whether (A) it's possible to have a multi-select pick list in DSF, or if you have to create multiple fields, and (B) exactly how the data is presented to FusionPro via the input data file, then we can answer your second question. Although, you'll note that the code above does use an array, so that's a bit of a hint as to what's possible.

Q2: Assuming the data is an array, can anyone suggest a javascript that will extract each entry from the array, then format the certifications so they have a comma and space after each one.

 

* Keep in mind the options are none, one selection, or multiple selections.

 

The first line of the business cards should look like:

Joe Smith, AAMS®, CFA

I am envisioning a script that basically says, If the field Certifications = “none” then return “”, else return the field Certifications, but with formatting “entry1, entry2, entry3, etc.”

 

I can create basic javascripts using the FusionPro building blocks, but this may be out of my league. I don’t know how to handle the formatting part.

So, regardless of how the data is presented to FusionPro, we can certainly put it into an array. Let's go back to the assumption that we have fields like "Certification1", "Certification2", etc. We can do a simple iteration to add these to an array, like so:

var result = [];
// iterate Certification*** fields
var numCertificationFields = 10; // set this to however many you defined
for (var i = 1; i < numCertificationFields ; i++)
 result.push(Field("Certification" + i));

Or, if you don't want to hard-code the maximum number of fields into the rule:

var result = [];
// iterate Certification*** fields
for (var i = 1; i < 10 ; i++)
{
   try
   {
       result.push(Field("Certification" + i));
   }
   catch (e)
   {
       // no more Certification*** fields
       break;
   }
}

Either way, now we have an array with all of the selected Certification field values. We want to remove any empty entries (where the user didn't make a selection; that's easy enough to do:

result = result.filter(String); // remove empty values

Then we just need to implement your logic:

]I am envisioning a script that basically says, If the field Certifications = “none” then return “”, else return the field Certifications, but with formatting “entry1, entry2, entry3, etc.”

That's pretty easy too:

if (!result)
   return ""; // no certifications
//else
return ", " + result.join(", ");

So, the whole rule could be this:

var result = [];
// iterate Certification*** fields
var numCertificationFields = 10; // set this to however many you defined
for (var i = 1; i < numCertificationFields ; i++)
 result.push(Field("Certification" + i))
result = result.filter(String); // remove empty values
if (!result)
   return ""; // no certifications
//else
return ", " + result.join(", ");

Link to comment
Share on other sites

Dan,

 

Thanks for your response.

 

In FusionPro I have set my certifications field up as a "pick list" when defining my HTML form, then as "check box list" in DSF so that the user is able to make multiple selections. The data that gets returned is then a list of the selected items separated by commas but no spaces (AAMS,CAP,CFA).

 

That's good, but now I need to add spaces between them and some of the entries need register R marks. I have included the ® marks in the value of my pick list in the FusionPro HTML form, but I am not getting that mark in the returned data from DSF. Again, a DSF issue, not a FusionPro issue.

 

I have learned from the customer that there will be a maximum of three certifications for each business card so I think I will try a different tactic on this and go with multiple fields as you suggested; Certification1, Certification2, etc. and make each one a dropdown list.

 

I think I can make this work on my own that way, but now I don't know how to handle the register R marks. I will try create a rule with tagged text for the register mark and make it superscript. Hopefully I can get something to work.

 

I'm still learning and experimenting.

Link to comment
Share on other sites

In FusionPro I have set my certifications field up as a "pick list" when defining my HTML form, then as "check box list" in DSF so that the user is able to make multiple selections. The data that gets returned is then a list of the selected items separated by commas but no spaces (AAMS,CAP,CFA). That's good, but now I need to add spaces between them

Ah, okay, I forgot that you can indeed specify a multi-select pick list. Although how any individual web application, such as DSF, chooses to implement such a multi-select list, and how it chooses to represent the selections in the data file it generates, are up to that web application.

 

But yes, this is good! Since we know it's a comma-delimited list, we can just call String.split on it to get our array, and use the Array.join to put them back into a string delimited by commas and spaces. The rule now becomes much simpler:

var result = Field("Certification").split(',').filter(String); // remove empty values
if (!result)
   return ""; // no certifications
//else
return ", " + result.join(", ");

 

and some of the entries need register R marks. I have included the ® marks in the value of my pick list in the FusionPro HTML form, but I am not getting that mark in the returned data from DSF. Again, a DSF issue, not a FusionPro issue.

Well, it's probably an encoding problem with how DSF handles the data. What you could do is just put ® in the pick list values (leaving the actual ® mark in the pick list prompts), and then replace the instances of ® with the actual mark in the rule, like so:

var result = Field("Certification").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
if (!result)
   return ""; // no certifications
//else
return ", " + result.join(", ");

I have learned from the customer that there will be a maximum of three certifications for each business card so I think I will try a different tactic on this and go with multiple fields as you suggested; Certification1, Certification2, etc. and make each one a dropdown list.

Well, actually, since we know the data will just have them as a comma-delimited list, you can use the rule above with a single multi-select field.

Link to comment
Share on other sites

Great script Dan.

 

It almost works perfectly. The problem I am having now is that even when the DSF user doesn't select any of the certifications, there is still a comma after the name: Fname Lname,

 

I tried to resolve that by adding a "none" choice and editing the script code you provided:

if (Field("Certifications")!="none")
{   
  var result = Field("Certifications").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
if (!result)
   return ""; // no certifications
//else
return ", " + result.join(", ");
}
else return “”;

 

That only works if the user selects ONLY the "none" choice, but since these are mulit-select check boxes, they could select "none" along with other choices which also presents a problem.

 

How do I edit your original code to eliminate the comma after the name when the user makes NO selection at all?

 

Your help is greatly appreciated! Happy Holidays.

Link to comment
Share on other sites

Hi Dan.

 

I finally got my rule to work. I ended up combining my "Name" field and my "Certifications" field into one FusionPro Rule. With this rule, the user can:

(1) make no selections at all for the Certifications; the rule returns ""

(2) they can choose "none" and any number of other selections for the Certifications; the rule returns "" since the first string in the array will be "none"

(3) they can make multiple selections for the Certifications that do NOT include "none"; the rule returns the certifications they selected

 

Here's the final code I ended up with:

 

var result = Field("Certifications").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
var firstChoice = Field("Certifications").split(',').shift()

if (firstChoice=="none"||result=="")
return Field("Name");
else
return Field("Name")+", " + result.join(", ");

I will share this code with everyone. Hopefully it will help someone else with a similar problem.

 

Dan, thanks for your help. I couldn't have done it without you.

Link to comment
Share on other sites

It almost works perfectly. The problem I am having now is that even when the DSF user doesn't select any of the certifications, there is still a comma after the name:

...

How do I edit your original code to eliminate the comma after the name when the user makes NO selection at all?.

Sorry, it should be testing the length of the array:

var result = Field("Certification").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
if (!result[color="Green"].length[/color])
   return ""; // no certifications
//else
return ", " + result.join(", ");

Or simply:

var result = Field("Certification").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
return result.length ? ", " + result.join(", ") : "";

Or, you could just put the name at the beginning of the array:

var result = Field("Certification").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
result.unshift(Field("Name"));
return result.join(", ");

Or, perhaps a bit more straightforward:

var s = Field("Name") + "," + Field("Certification");
return s.split(',').filter(String).map(function(s){return s.replace("(R)","®");}).join(", ");

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...