Go Back   MarcomCentral (PTI) and FusionPro User Communities > Software-Related Talk > The JavaScript Library

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old December 21st, 2016, 04:34 PM
jeller jeller is offline
Forum n00b
 
Join Date: Jul 2013
Posts: 4
Default Formatting an array of values

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?
Reply With Quote
  #2  
Old December 22nd, 2016, 11:21 AM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,359
Default Re: Formatting an array of values

Quote:
Originally Posted by jeller View Post
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:
Code:
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:
Code:
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.
Quote:
Originally Posted by jeller View Post
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:
Code:
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:
Code:
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:
Code:
result = result.filter(String); // remove empty values
Then we just need to implement your logic:
Quote:
Originally Posted by jeller View Post
]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:
Code:
if (!result)
    return ""; // no certifications
//else
return ", " + result.join(", ");
So, the whole rule could be this:
Code:
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(", ");
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #3  
Old December 22nd, 2016, 01:39 PM
jeller jeller is offline
Forum n00b
 
Join Date: Jul 2013
Posts: 4
Default Re: Formatting an array of values

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.
Reply With Quote
  #4  
Old December 22nd, 2016, 03:07 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,359
Default Re: Formatting an array of values

Quote:
Originally Posted by jeller View Post
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:
Code:
var result = Field("Certification").split(',').filter(String); // remove empty values
if (!result)
    return ""; // no certifications
//else
return ", " + result.join(", ");
Quote:
Originally Posted by jeller View Post
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 (R) in the pick list values (leaving the actual ® mark in the pick list prompts), and then replace the instances of (R) with the actual mark in the rule, like so:
Code:
var result = Field("Certification").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
if (!result)
    return ""; // no certifications
//else
return ", " + result.join(", ");
Quote:
Originally Posted by jeller View Post
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.
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
  #5  
Old December 22nd, 2016, 04:23 PM
jeller jeller is offline
Forum n00b
 
Join Date: Jul 2013
Posts: 4
Default Re: Formatting an array of values

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:
Code:
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.
Reply With Quote
  #6  
Old December 27th, 2016, 10:00 AM
jeller jeller is offline
Forum n00b
 
Join Date: Jul 2013
Posts: 4
Default Re: Formatting an array of values

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:

Code:
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.
Reply With Quote
  #7  
Old December 27th, 2016, 03:52 PM
Dan Korn's Avatar
Dan Korn Dan Korn is offline
FusionPro Senior Engineer / Forum Moderator
 
Join Date: Aug 2008
Location: Chicago, IL
Posts: 4,359
Default Re: Formatting an array of values

Quote:
Originally Posted by jeller View Post
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:
Code:
var result = Field("Certification").split(',').filter(String).map(function(s){return s.replace("(R)","®");});
if (!result.length)
    return ""; // no certifications
//else
return ", " + result.join(", ");
Or simply:
Code:
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:
Code:
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:
Code:
var s = Field("Name") + "," + Field("Certification");
return s.split(',').filter(String).map(function(s){return s.replace("(R)","®");}).join(", ");
__________________
Dan Korn
FusionPro Developer / JavaScript Guru / Forum Moderator
PTI Marketing Technologies | Printable | MarcomCentral
LinkedIn

I am a not a Support engineer, and this forum is not a substitute for Support. My participation on this forum is primarily as a fellow user (and a forum moderator). I am happy to provide help and answers to questions when I can; however, there is no guarantee that I, or anyone else on this forum, will be able to answer all questions or fix any problems. If I ask for files to clarify an issue, I might not be able to look at them personally. I am not able to answer private messages, emails, or phone calls unless they go through proper Support channels. Please direct any sales or pricing questions to your salesperson or inquiries@marcom.com.

Complex template-building questions, as well as all installation and font questions or problems, should be directed to FusionProSupport@marcom.com. Paid consulting work may be required to fulfill your template-building needs.

This is a publicly viewable forum. Please DO NOT post fonts, or other proprietary content, to this forum. Also, please DO NOT post any "live" data with real names, addresses, or any other personal, private, or confidential data.

Please include the specific versions of FusionPro, Acrobat, and your operating system in any problem reports or help requests. I recommend putting this information in your forum signature. Please also check your composition log (.msg) file for relevant error or warning messages.

Please post questions specific to the MarcomCentral Enterprise and Web-to-Print applications in the MarcomCentral forum. Click here to request access. Or contact your Business Relationship Manager (BRM/CPM) for assistance.

Please direct any questions specific to EFI's Digital StoreFront (DSF) to EFI support.

How To Ask Questions The Smart Way

The correct spellings are JavaScript, FusionPro, and MarcomCentral (each with two capital letters and no spaces). Acceptable abbreviations are JS, FP, and MC (or MCC). There is no "S" at the end of "Expression" or "Printable"! The name of the product is FusionPro, not "Fusion". "Java" is not is not the same as JavaScript.

Check out the JavaScript Guide and JavaScript Reference! FusionPro 8.0 and newer use JavaScript 1.7. Older versions use JavaScript 1.5.

return "KbwbTdsjqu!spdlt\"".replace(/./g,function(w){return String.fromCharCode(w.charCodeAt()-1)});
Reply With Quote
Reply

Tags
array, fusionpro, javascript, mulit-select box

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 10:50 AM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
(c) 2011, PTI Marketing Technologies™, Inc.