MarcomCentral (PTI) and FusionPro User Communities

MarcomCentral (PTI) and FusionPro User Communities (http://forums.pti.com/index.php)
-   Tables & Charts (http://forums.pti.com/forumdisplay.php?f=36)
-   -   Table / Copyfitting for 1 to 6 fields (http://forums.pti.com/showthread.php?t=4636)

jl_arnold July 26th, 2016 08:43 AM

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?

tou July 26th, 2016 09:09 AM

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.

jl_arnold July 26th, 2016 10:03 AM

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

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



Is this type of conditional formatting possible?

tou July 26th, 2016 10:21 AM

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.

jl_arnold July 26th, 2016 11:05 AM

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.

Dan Korn July 26th, 2016 11:12 AM

Re: Table / Copyfitting for 1 to 6 fields
 
Quote:

Originally Posted by tou (Post 18919)
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.

step July 26th, 2016 11:40 AM

Re: Table / Copyfitting for 1 to 6 fields
 
Quote:

Originally Posted by tou (Post 18919)
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 (Post 18918)
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();


tou July 26th, 2016 11:58 AM

Re: Table / Copyfitting for 1 to 6 fields
 
Nice! Good Stuff Step!

jl_arnold July 26th, 2016 12:06 PM

Re: Table / Copyfitting for 1 to 6 fields
 
This is awesome!

Thanks so much Step!

jl_arnold July 26th, 2016 12:18 PM

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.


All times are GMT -7. The time now is 12:08 PM.

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