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 March 17th, 2009, 02:14 PM
EricC EricC is offline
Regular Contributor
 
Join Date: Nov 2008
Posts: 87
Red face Alphabetize a list

I am trying to create an on-line FusionPro template of a "phone book".

I want to offer the following functionality:

- User enters (into one large text box?) a list of names and corresponding phone numbers.

- when they click the 'Refresh Preview' button (i.e. onRecordStart?), the list they entered is returned, sorted alphabetically.

Is it possible to write a JavaScript rule that does this?

Here's one thing to keep in mind...
For each NAME, I need to associate a PHONE NUMBER

So for example, if I enter the following data:
John Smith 858-123-4567
Adam Rodriguez 986-456-7894
Robert Hoo 201-457-7847

... I should get this back:
Adam Rodriguez 986-456-7894
John Smith 858-123-4567
Robert Hoo 201-457-7847

(The list was sorted alphabetically, and the phone numbers "moved" with the corresponding name)

Can I bribe someone with an amazon.com gift certificate if they can help me pull this off?
__________________
--
Eric Carbone
IT/Project Manager, MOR Printing Inc.

Windows 10 |Adobe Acrobat XI | FusionPro 9
Reply With Quote
  #2  
Old March 18th, 2009, 10:42 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,366
Default Re: Alphabetize a list

Sure, sorting data alphabetically is absolutely possible. JavaScript is great for this kind of stuff.

Exactly how is the data presented to FusionPro? Is it all in one field with some kind of intra-field delimiter? Or an external data file? Are the phone numbers already associated with the names in the data?

At any rate, if you can get the data into an array, you can just use the native Array.sort method in JavaScript:
https://developer.mozilla.org/En/Cor...cts/Array/Sort

Sorts are done by simple string comparison by default, so if you're simply sorting by first name, there's not much to do. (Although I'm not sure I would sort by first name in a real-world application, but that's just me.)
Code:
var myArray = [ "John Smith 858-123-4567", "Adam Rodriguez 986-456-7894", "Robert Hoo 201-457-7847" ];
return myArray.sort().join("<br>\n");
Again, though, how you populate the array initially depends on how the data is being imported. If it's in an external data file, you could use ExternalDataFileEx to read it in. If it's in something like a comma-delimited list, you can use String.split to turn it into an array, for instance:
Code:
var myData = "John Smith 858-123-4567,Adam Rodriguez 986-456-7894,Robert Hoo 201-457-7847";
var myArray = myData.split(",");
return myArray.sort().join("<br>\n");
__________________
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 March 18th, 2009, 11:35 AM
EricC EricC is offline
Regular Contributor
 
Join Date: Nov 2008
Posts: 87
Default Re: Alphabetize a list

Hi Dan,
Good stuff!
Well to answer your question regarding HOW the user would input the list ...

This is for a printOne site, so my options are:

1. Create 50 blank fields, and allow the user to enter (up to) 50 names. (Then I suppose I'd aso have to have 50 more fields, for the phone numbers). This makes for a very large form that the user has to fill out. And what happens when the user has MORE than 50 entries? I'd have to add even more fields. So I guess this method is not practical.

2. The second option (as far as I know) is to present the user with one large multi-line text box and say "Type all of your names and phone numbers in here, separated by a comma" (or some other type of delimiter). It would probably look messy but this is the only other input method I can think of. (Having the user upload a data file of some sort would not be practical (not for this client anyway))

So some questions...

1. Instead of asking the user to enter one long, continuous line of names and phone numbers separated by commas, CAN the user simply press the return key after each name/number pair is entered?

In other words can they enter this...
John Smith 858-123-4567
Adam Rodriguez 986-456-7894
Robert Hoo 201-457-7847

... instead of this...
John Smith 858-123-4567, Adam Rodriguez 986-456-7894, Robert Hoo 201-457-7847

2. Assuming this multi-line text box can accept this data, and assuming the name of the text box is txtAccounting, can you help me construct the correct JavaScript code?

3. Where exactly do I put this code? Do I place it inside of OnRecordStart ?
__________________
--
Eric Carbone
IT/Project Manager, MOR Printing Inc.

Windows 10 |Adobe Acrobat XI | FusionPro 9
Reply With Quote
  #4  
Old March 18th, 2009, 02:24 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,366
Default Re: Alphabetize a list

Quote:
Originally Posted by EricC View Post
This is for a printOne site, so my options are:
...
1. Instead of asking the user to enter one long, continuous line of names and phone numbers separated by commas, CAN the user simply press the return key after each name/number pair is entered?
That would be a question for the FP Web forum.
Quote:
Originally Posted by EricC View Post
2. Assuming this multi-line text box can accept this data, and assuming the name of the text box is txtAccounting, can you help me construct the correct JavaScript code?
It depends on how the data is presented to FusionPro at composition time. If you have the input file that FP Web generates, we can take a look at it and see what's in it and then figure out how to process it.
Quote:
Originally Posted by EricC View Post
3. Where exactly do I put this code? Do I place it inside of OnRecordStart ?
It can be in any regular rule that returns its results to a text frame in your template. Again, it depends on what you're trying to do.
__________________
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 July 25th, 2013, 12:52 PM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 469
Default Re: Alphabetize a list

Dan, I tested the array rule you have here using the data typed into the rule and it works great. I am wondering how I would use an external data file to accomplish this same thing. I have attached a sample data file, these are in alphabetical order right now but won't be when I run the job. How would I write the rule with each option being in it's own field. I would want to ignore the first field in the external data file though, that field is what I am going to try and link the source data to. TIA

I tried this but get an error "myData.split is not a function"

Code:
var myData = new ExternalDataFileEx("BeerListHeader.txt", "\,");
var myArray = myData.split("\,");
return myArray.sort().join("<br>\n");
Attached Files
File Type: txt BeerListHeader.txt (854 Bytes, 7 views)
__________________
Don Reimer - JavaScript Novice

MAC OS 10.12.5 Acrobat XI 11.0.0 FP Creator 10.0.16
Windows 7 Acrobat X 10.1.4 FP Creator 9.3.9

Last edited by dreimer; July 25th, 2013 at 02:11 PM..
Reply With Quote
  #6  
Old July 25th, 2013, 02:27 PM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Alphabetize a list

Are you composing your template using only the external data file? Or are you referencing the external data file's kit number to determine which beer list to pull in?

If it's the former:
OnRecordStart()
Code:
var exData = new ExternalDataFileEx("BeerListHeader.txt",','); // Initialize External Data file
var ordered = [];  //Initialize the array
var frameName = "BeerList"; // Name of the text frame you want the list to appear in

// Populate the array with columns 1-8 of your external data list
for (var i=1; i<=8; i++){
    ordered.push(exData.GetFieldValue(FusionPro.Composition.inputRecordNumber,i));
    }

// Remove blanks from the array
for (var n=0; n<ordered.length; n++){
    if (ordered[n] == "") {
        ordered.splice(n,1);
        n--;
    }
}

// Put the contents of the array in alphabetical order
// Join the contents with break tags
ordered = ordered.sort().join("<br>");

FindTextFrame(frameName).content = ordered;
If it's the latter:
New Text Rule
Code:
var exData = new ExternalDataFileEx("BeerListHeader.txt",','); // Initialize External Data file
var ordered = [];  //Initialize the array
var kitField = "Kit#"; // Name of field in data to match the kit number in the external data file

// Populate the array with columns 1-8 of your external data list
for (var i=1; i<=8; i++){
    ordered.push(exData.GetFieldValue(exData.FindRecord("Kit",Field(kitField)),i));
    }
    
// Remove blanks from the array
for (var n=0; n<ordered.length; n++){
    if (ordered[n] == "") {
        ordered.splice(n,1);
        n--;
    }
}

// Put the contents of the array in alphabetical order
// Join the contents with break tags
ordered = ordered.sort().join("<br>");

return ordered;
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
  #7  
Old July 25th, 2013, 02:34 PM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 469
Default Re: Alphabetize a list

Wow, thanks.

Not sure at this point, could go either way. So for now I tried using the first one not having to match source data. I get an error "ordered is not defined". Is that function for newer versions of FP?
__________________
Don Reimer - JavaScript Novice

MAC OS 10.12.5 Acrobat XI 11.0.0 FP Creator 10.0.16
Windows 7 Acrobat X 10.1.4 FP Creator 9.3.9

Last edited by dreimer; July 25th, 2013 at 02:49 PM..
Reply With Quote
  #8  
Old July 25th, 2013, 02:49 PM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 469
Default Re: Alphabetize a list

Scratch that, didn't use it as a OnRecordStart rule. Works great. One thing, I want to have it populate into three columns of my text frame. I have that set and it works, but how would I get the text centered and not left justified since it is a OnRecordStart Rule. Guess I would have to figure out how to change the font as well. Thanks for the help!!!!
__________________
Don Reimer - JavaScript Novice

MAC OS 10.12.5 Acrobat XI 11.0.0 FP Creator 10.0.16
Windows 7 Acrobat X 10.1.4 FP Creator 9.3.9
Reply With Quote
  #9  
Old July 25th, 2013, 02:56 PM
dreimer dreimer is offline
Senior Contributor
 
Join Date: Oct 2010
Location: Shakopee, MN
Posts: 469
Default Re: Alphabetize a list

Second one works great too! I just used the same external file as my source file to test it. This one is going to work best I think since this way I have the text box options available as opposed to the OnRecordStart rule.

And once again thank you for your help with many of my questions over the years. Glad to have the people we do on this forum!!!
__________________
Don Reimer - JavaScript Novice

MAC OS 10.12.5 Acrobat XI 11.0.0 FP Creator 10.0.16
Windows 7 Acrobat X 10.1.4 FP Creator 9.3.9
Reply With Quote
  #10  
Old July 26th, 2013, 07:02 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Alphabetize a list

Quote:
Originally Posted by dreimer View Post
but how would I get the text centered and not left justified since it is a OnRecordStart Rule. Guess I would have to figure out how to change the font as well.
One thing you could try is to put some text in the text box and use the text editor to format it the way you want. When the OnRecordStart rule runs, it will override the content of that frame with the beer list but will hold the formatting.
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn
Reply With Quote
Reply

Tags
alphabetically, alphabetize, array, sort

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 08:37 PM.


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