MarcomCentral (PTI) and FusionPro User Communities

MarcomCentral (PTI) and FusionPro User Communities (http://forums.pti.com/index.php)
-   The JavaScript Library (http://forums.pti.com/forumdisplay.php?f=23)
-   -   Alphabetize a list (http://forums.pti.com/showthread.php?t=516)

EricC March 17th, 2009 01:14 PM

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?

Dan Korn March 18th, 2009 09:42 AM

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");


EricC March 18th, 2009 10:35 AM

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 ?

Dan Korn March 18th, 2009 01:24 PM

Re: Alphabetize a list
 
Quote:

Originally Posted by EricC (Post 1588)
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 (Post 1588)
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 (Post 1588)
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.

dreimer July 25th, 2013 11:52 AM

Re: Alphabetize a list
 
1 Attachment(s)
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");


step July 25th, 2013 01:27 PM

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;


dreimer July 25th, 2013 01:34 PM

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?

dreimer July 25th, 2013 01:49 PM

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!!!!

dreimer July 25th, 2013 01:56 PM

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!!!

step July 26th, 2013 06:02 AM

Re: Alphabetize a list
 
Quote:

Originally Posted by dreimer (Post 13378)
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.


All times are GMT -7. The time now is 06:26 AM.

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