Jump to content

Alphabetize a list


EricC

Recommended Posts

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?

Link to comment
Share on other sites

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/Core_JavaScript_1.5_Reference/Global_Objects/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.)

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:

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

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.

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.

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.

Link to comment
Share on other sites

  • 4 years later...

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"

 

var myData = new ExternalDataFileEx("BeerListHeader.txt", "\,");
var myArray = myData.split("\,");
return myArray.sort().join("<br>\n");

BeerListHeader.txt

Edited by dreimer
Link to comment
Share on other sites

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()

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

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;

Link to comment
Share on other sites

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?

Edited by dreimer
Link to comment
Share on other sites

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!!!!
Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

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