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

Notices

Reply
 
Thread Tools Search this Thread Display Modes
  #1  
Old July 26th, 2016, 08:43 AM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Table / Copyfitting for 1 to 6 fields

I have a template that is asking for up to 6 text fields. The first field is mandatory, where the next five are optional.


I am trying to build a table that will format these text fields in 3 columns and 2 rows. However, if only two of the six text fields are entered, I want the table to be formatted with one row and two columns.

I have been reading over creating tables, but I am not positive how I should go about creating this type of table that adds additional cells if additional fields are entered in the form.


My current table is below. The basic form of the table is there, but it doesn't adjust the cells to merge if the last text fields are not used.
Code:
var myTable = new FPTable;
myTable.AddColumns(10776, 10776, 10776);
myTable.HAlign = "Center";


//Line1
var myRow = myTable.AddRow();
myRow.Cells[0].HAlign = "Center"; 
myRow.Cells[1].HAlign = "Center"; 
myRow.Cells[2].HAlign = "Center"; 
myRow.Cells[0].Content = Field("Location 1");
myRow.Cells[1].Content = Field("Location 2");
myRow.Cells[2].Content = Field("Location 3");
        
//Line2
if (Field("Location 4") || Field("Location 5") || Field("Location 6"))
{
var myRow = myTable.AddRow();
myRow.Cells[0].HAlign = "Center"; 
myRow.Cells[1].HAlign = "Center"; 
myRow.Cells[2].HAlign = "Center"; 
myRow.Cells[0].Content = Field("Location 4");
myRow.Cells[1].Content = Field("Location 5");
myRow.Cells[2].Content = Field("Location 6");

}


return myTable.MakeTags();


Am I on the right track with something like this?
Reply With Quote
  #2  
Old July 26th, 2016, 09:09 AM
tou tou is offline
Regular Contributor
 
Join Date: Oct 2015
Posts: 107
Default Re: Table / Copyfitting for 1 to 6 fields

Adding a row does not need to be "var":
Code:
var myRow = myTable.AddRow();
should be just (in both cases.....):
Code:
myTable.AddRow(1);
Rows can be specified like the following:
Code:
myTable.Rows[0].Cells[0].Content = Field("Location 1");
myTable.Rows[0].Cells[1].Content = Field("Location 2");
myTable.Rows[0].Cells[2].Content = Field("Location 3");
The locations 4 through 6 should be coded like the following
Code:
myTable.Rows[1].Cells[0].Content = Field("Location 4");
myTable.Rows[1].Cells[1].Content = Field("Location 5");
myTable.Rows[1].Cells[2].Content = Field("Location 6");
I think that should do it.

Good luck.

Last edited by tou; July 26th, 2016 at 09:14 AM..
Reply With Quote
  #3  
Old July 26th, 2016, 10:03 AM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Table / Copyfitting for 1 to 6 fields

Thanks Tou, however the design of the table was not my issue. But I did update my code for simplicity and constancy.

The issues I am having is formatting the code so that if Field("Location 1") is the only entry in the form, than I would get a single column, single row table.

Then I want to code to determine if Field("Location 1") and Field("Location 2") are being entered in the form and if so, than a two column row would be composed.


Here's a sample screenshot for what I'm trying to accomplish:


https://images.printable.com/imageli...Formatting.jpg



Is this type of conditional formatting possible?
Reply With Quote
  #4  
Old July 26th, 2016, 10:21 AM
tou tou is offline
Regular Contributor
 
Join Date: Oct 2015
Posts: 107
Default Re: Table / Copyfitting for 1 to 6 fields

You can always do it with more coding....:P

Assuming that when two fields are populate it's the first 2 location and so on....code it backwards...

if (Field("Location 6") != "") {
YOUR 6 LOCATION CODE GOES HERE
} else if (Field("Location 5") != "") {
YOUR 5 LOCATION CODE GOES HERE
} else if (Field("Location 4") != "") {
YOUR 4 LOCATION CODE GOES HERE
} else if (Field("Location 3") != "") {
YOUR 3 LOCATION CODE GOES HERE
} else if (Field("Location 2") != "") {
YOUR 2 LOCATION CODE GOES HERE
} else if (Field("Location 1") != "") {
YOUR 1 LOCATION CODE GOES HERE
}

If your locations are not always populated from 1 to 6 filled incrementally, you'll have to code more variables to handle it.

Good luck.
Reply With Quote
  #5  
Old July 26th, 2016, 11:05 AM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Table / Copyfitting for 1 to 6 fields

This is perfect!

I was going to see how much extra coding it was to make this work incase 1 - 6 is not filled in incrementally, but this definitely works for me. Thanks for your help.

I'm going to be tweaking the table a bit so I may have another question or two about formatting, but I think I should be good to go!

Thanks again.
Reply With Quote
  #6  
Old July 26th, 2016, 11:12 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,361
Default Re: Table / Copyfitting for 1 to 6 fields

Quote:
Originally Posted by tou View Post
Adding a row does not need to be "var":
Code:
var myRow = myTable.AddRow();
should be just (in both cases.....):
Code:
myTable.AddRow(1);
Rows can be specified like the following:
Code:
myTable.Rows[0].Cells[0].Content = Field("Location 1");
myTable.Rows[0].Cells[1].Content = Field("Location 2");
myTable.Rows[0].Cells[2].Content = Field("Location 3");
I actually prefer the way that jl_arnold had it. It's more concise to say:
Code:
myRow.Cells[0].Content = Field("Location 1");
Also, if you add another row somewhere, you don't then need to change all the hard-coded row numbers. You can just deal with each row as it's added, and you always know you're on the last one.
__________________
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
  #7  
Old July 26th, 2016, 11:40 AM
step's Avatar
step step is offline
Senior Contributor
 
Join Date: Jan 2010
Location: Charlotte, NC
Posts: 955
Default Re: Table / Copyfitting for 1 to 6 fields

Quote:
Originally Posted by tou View Post
Adding a row does not need to be "var":
Code:
var myRow = myTable.AddRow();
should be just (in both cases.....):
Code:
myTable.AddRow(1);
Rows can be specified like the following:
Code:
myTable.Rows[0].Cells[0].Content = Field("Location 1");
myTable.Rows[0].Cells[1].Content = Field("Location 2");
myTable.Rows[0].Cells[2].Content = Field("Location 3");
While you are correct in saying that "adding a row doesn't need to be a var," there is nothing wrong with assigning it to a variable. The "AddRow" function actually returns the newly created row object so, assigning it to a variable can make things a little easier to read as opposed to writing "myTable.Rows[0]." But either syntax is completely fine, I just wanted to make that distinction.

Quote:
Originally Posted by jl_arnold View Post
I have a template that is asking for up to 6 text fields. The first field is mandatory, where the next five are optional.


I am trying to build a table that will format these text fields in 3 columns and 2 rows. However, if only two of the six text fields are entered, I want the table to be formatted with one row and two columns.
I think the way you want to achieve this is by determining how many fields you have populated and using that information to determine how many columns you'll need.

The first part is easy. You can put all of your fields into an array and use the 'filter' method to remove any fields that are empty:
Code:
var fields = [
  Field("Location 1"),
  Field("Location 2"),
  Field("Location 3"),
  Field("Location 4"),
  Field("Location 5"),
  Field("Location 6")
].filter(String);
The nice thing about this method is that it doesn't rely on the fields being populated in a particular order. For example, if only "Location 1" and "Location 5" were populated, the array would be reduced to ["Location 1", "Location 5"] which would generate a 2 column table.

From there, you'll be able to tell how many fields are populated by accessing the 'length' property on the 'fields' array. One thing we'll need to do, though, is insert a "blank" before the last element if the array length is 5 which can be done using the 'splice' method. Then we're ready to figure out how many columns we need:
Code:
// Insert blank to push last element into the 3rd cell
if (fields.length == 5) 
  fields.splice(4, 0, ' ');
// Determine the number of columns
var cols = fields.length == 1 ? 1 : fields.length % 3 ? 2 : 3;
You can determine the number of columns (the 'cols' variable) a number of ways. I chose to use a ternary conditional but you could write it as a switch statement if you wanted to:
Code:
switch (fields.length) {
  case 1:
    var cols = 1;
    break;
  case 2:
  case 4:
    var cols = 2;
    break;
  default:
    var cols = 3;
}
From there, determine the width each column should be (based on the number of columns needed) and create the table and columns:
Code:
var myTable = new FPTable();
var tableWidth = 32328;
for (var i=0; i<cols; i++)
  myTable.AddColumn(tableWidth/cols);
Now that the table has the appropriate number of columns, all we have to do is add the content for each row:
Code:
while(fields.length) {
  var row = myTable.AddRow();
  for (var i=0; i<cols; i++) {
    var cell = row.Cells[i];
    cell.HAlign = 'Center';
    cell.Content = fields.shift();
  }
}
So all together it looks like this:
Code:
var fields = [Field("Location 1"), Field("Location 2"), Field("Location 3"), Field("Location 4"), Field("Location 5"), Field("Location 6")].filter(String);

// Insert blank to push last element into the 3rd cell
if (fields.length == 5) fields.splice(4, 0, ' ');

// Determine the number of columns
var cols = fields.length == 1 ? 1 : fields.length % 3 ? 2 : 3;
var tableWidth = 32328; 

// Create table & add columns
var myTable = new FPTable();
for (var i=0; i<cols; i++)
  myTable.AddColumn(tableWidth/cols);

while(fields.length) {         
  var row = myTable.AddRow(); 
  for (var i=0; i<cols; i++) { 
    var cell = row.Cells[i];   
    cell.HAlign = 'Center';    
    cell.Content = fields.shift(); 
  }
}

return myTable.MakeTags();
__________________
Ste Pennell
FusionPro VDP Creator 9.3.15
Adobe Acrobat X 10.1.1
Mac OS X 10.12

LinkedIn

Last edited by step; July 26th, 2016 at 11:44 AM..
Reply With Quote
  #8  
Old July 26th, 2016, 11:58 AM
tou tou is offline
Regular Contributor
 
Join Date: Oct 2015
Posts: 107
Default Re: Table / Copyfitting for 1 to 6 fields

Nice! Good Stuff Step!
Reply With Quote
  #9  
Old July 26th, 2016, 12:06 PM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Table / Copyfitting for 1 to 6 fields

This is awesome!

Thanks so much Step!
Reply With Quote
  #10  
Old July 26th, 2016, 12:18 PM
jl_arnold jl_arnold is offline
Regular Contributor
 
Join Date: Jun 2016
Posts: 53
Default Re: Table / Copyfitting for 1 to 6 fields

Quick follow up question.

This really would only pertain to the 5 Locations table. I made Tou's suggestion work and was able to set the table properties for 1 location, 2 locations, 3 locations, etc. so I designed each table individually.

I did this and made the table with 5 locations have 4 columns, where I merged column 2 and 3 in row 1, and the I merged 1 and 2 in row 2, as well as 3 and 4 in row 2.

The end result is in this image:



It seems like I should be able to change to var = cols, but I'm not sure how.
Reply With Quote
Reply

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 12:59 PM.


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