Jump to content

Table Formatting Help


bkurzbuch

Recommended Posts

Coding my first table from scratch and need some advice. Mac - 10.14.6, FP 10.1.11 and latest Acrobat DC. I've attached a zip file of the job to help. There is a pdf labeled customer original to show what the output should be. The table is 4 columns and no more than 17 rows. The data file is imported, not XDF and the data for the table is set up in cell by cell and row by row. You'll see in my template that I have the first row formatted, except for adjusting margins and alignment to match the sample. Since I am finding myself repeating a lot of code, thought i would ask for help. This will be a monthly job so I'm trying to cover all the bases right out of the box. Is there a way to copy all that formatting to the rest of the rows? It will be the same as row zero. My coding skills are minimal at best. My other question is if there is no data can i just use the skip row function or is there a better way. For once I have a few days on this and am using it as a good learning tool, so if you can just point me in the right direction instead of coding for me that would be great. As always thanks for the help.

Table_Advise.zip

Link to comment
Share on other sites

Use a loop to build the table, and insert a break tag to break out of the loop if a field is empty.

 

 

for (var i = 0; i <= 16; i++)

 

{

var FieldToUse = (i + 1)

 

if (Field("Item " + FieldToUse) == "" )

 

break;

 

insert your table structure here

 

}

Link to comment
Share on other sites

Trying very hard to not just code it for you... ;)

 

Yes, a "for" loop is your friend here.

 

Instead of a single call to myTable.AddRows() at the top, you want to call myTable.AddRow() in the loop. If there's no row to add, just continue and skip the call to AddRow for that iteration of the loop.

 

In other words, don't think of it as skipping rows that you've already added. Instead, think of it as adding rows one at a time, as needed.

 

So, the whole rule should only be a few lines of code, with a loop that calls myTable.AddRow() and row.SetContents() in it.

Link to comment
Share on other sites

Thank You all for the quick Reply. I'm Familiar with "for loop" concept just not how to code it. Thanks Dan. i know its hard not to code it. i have a couple of question already, but I'm going to work with this today and see what other questions arise. It may come to helping with the code as I can learn just by see what it should be. Also thanks for the JavaScript Table API – The Basics post. This was a huge help in getting me started. thanks again
Link to comment
Share on other sites

Is it just me or does ever complex variable jobs information common in bits and pieces as you work on it. Ugh. Ok - Off the soap box. Uncle, Help!! I've tried unsuccessfully to incorporate the loop. Kinda flying blind when i don't understand the code. My updated code is below. This output the first row perfectly matching the customer originals. Now to get the rest to fill. Now the wrinkle. The data runs out to 20 rows. There 2 different triggers to not include a row. 1. data is blank for that row. 2. The "Disc" field is zero. Now if you cycle through the data you will see that some of the "Disc" lines say "Number 0". This is bad data and we want to show it like this so the customer can see there data needs to be cleaned up. There is also other spelling error and such, all of which they need to see. And of course the due data is moved to Monday. Thanks for all the help. Code away.

 

new FPTable;
var myTable = new FPTable;
myTable.AddColumns(5760, 21168, 4536, 4536);  
myTable.AddRows(17);

myTable.Rows[0].Cells[0].SetBorders("Thin", "Black", "Bottom");
myTable.Rows[0].Cells[0].Margins = new FPTableMargins;
myTable.Rows[0].Cells[0].Margins.Top = 25;
myTable.Rows[0].Cells[0].Margins.Bottom = 25;
myTable.Rows[0].CopyCells(0, 1,2,3);

myTable.Rows[0].Cells[0].Margins = new FPTableMargins;
myTable.Rows[0].Cells[0].Margins.Top = 25;
myTable.Rows[0].Cells[0].Margins.Bottom = 25;
myTable.Rows[0].Cells[0].Margins.Left = 350;
myTable.Rows[0].CopyCells(0, 1);

myTable.Rows[0].Cells[0].HAlign = "Left";
myTable.Rows[0].Cells[1].HAlign = "Left";
myTable.Rows[0].Cells[2].HAlign = "Center";
myTable.Rows[0].Cells[3].HAlign = "Center";

myTable.Rows[0].Cells[0].Font="News Gothic Bold";
myTable.Rows[0].Cells[0].PointSize=6;
myTable.Rows[0].Cells[1].Font="News Gothic Regular";
myTable.Rows[0].Cells[1].PointSize=7;
myTable.Rows[0].Cells[2].Font="News Gothic Bold";
myTable.Rows[0].Cells[2].PointSize=7;
myTable.Rows[0].Cells[3].Font="News Gothic Regular";
myTable.Rows[0].Cells[3].PointSize=7;

myTable.Rows[0].SetContents(Field("Item 1"), Field("Desc 1"), Field("Disc 1"), Field("Price 1"));

return myTable.MakeTags();

Link to comment
Share on other sites

Uncle, Help!! I've tried unsuccessfully to incorporate the loop. Kinda flying blind when i don't understand the code. My updated code is below. This output the first row perfectly matching the customer originals. Now to get the rest to fill. Now the wrinkle. The data runs out to 20 rows.

Like I said:

Instead of a single call to myTable.AddRows() at the top, you want to call myTable.AddRow() in the loop.

Following that, the rule with the loop would look like so:

var myTable = new FPTable;
myTable.AddColumns(5760, 21168, 4536, 4536);  
//myTable.AddRows(17);

for (var i = 1; i <= 20; i++)
{
   var row = myTable.AddRow();

   var cell = row.Cells[0];
   cell.SetBorders("Thin", "Black", "Bottom");
   cell.Margins = new FPTableMargins;
   cell.Margins.Top = 25;
   cell.Margins.Bottom = 25;
   row.CopyCells(0, 1,2,3);

   cell.Margins = new FPTableMargins;
   cell.Margins.Top = 25;
   cell.Margins.Bottom = 25;
   cell.Margins.Left = 350;
   row.CopyCells(0, 1);

   row.Cells[0].HAlign = "Left";
   row.Cells[1].HAlign = "Left";
   row.Cells[2].HAlign = "Center";
   row.Cells[3].HAlign = "Center";

   row.Cells[0].Font = "News Gothic Bold";
   row.Cells[0].PointSize = 6;
   row.Cells[1].Font = "News Gothic Regular";
   row.Cells[1].PointSize = 7;
   row.Cells[2].Font = "News Gothic Bold";
   row.Cells[2].PointSize = 7;
   row.Cells[3].Font = "News Gothic Regular";
   row.Cells[3].PointSize = 7;

   row.SetContents(Field("Item " + i), Field("Desc " + i), Field("Disc " + i), Field("Price " + i));
}

return myTable.MakeTags();

Now the other wrinkle is when to suppress rows:

There 2 different triggers to not include a row. 1. data is blank for that row. 2. The "Disc" field is zero.

Like I also said:

If there's no row to add, just continue and skip the call to AddRow for that iteration of the loop.

So, taking that sentence of pseudocode and putting it into code:

var myTable = new FPTable;
myTable.AddColumns(5760, 21168, 4536, 4536);  

for (var i = 1; i <= 20; i++)
{
   var item = Field("Item " + i);
   var desc = Field("Desc " + i);
   var disc = Field("Disc " + i);
   var price = Field("Price " + i);

   // There 2 different triggers to not include a row.
   // 1. data is blank for that row.
   if (!item && !desc && !disc && !price)
       continue;

   // 2. The "Disc" field is zero.
   if (Int(disc) == 0)
       continue;

   var row = myTable.AddRow();

   var cell = row.Cells[0];
   cell.SetBorders("Thin", "Black", "Bottom");
   cell.Margins = new FPTableMargins;
   cell.Margins.Top = 25;
   cell.Margins.Bottom = 25;
   row.CopyCells(0, 1,2,3);

   cell.Margins = new FPTableMargins;
   cell.Margins.Top = 25;
   cell.Margins.Bottom = 25;
   cell.Margins.Left = 350;
   row.CopyCells(0, 1);

   row.Cells[0].HAlign = "Left";
   row.Cells[1].HAlign = "Left";
   row.Cells[2].HAlign = "Center";
   row.Cells[3].HAlign = "Center";

   row.Cells[0].Font = "News Gothic Bold";
   row.Cells[0].PointSize = 6;
   row.Cells[1].Font = "News Gothic Regular";
   row.Cells[1].PointSize = 7;
   row.Cells[2].Font = "News Gothic Bold";
   row.Cells[2].PointSize = 7;
   row.Cells[3].Font = "News Gothic Regular";
   row.Cells[3].PointSize = 7;

   row.SetContents(item, desc, disc, price);
}

return myTable.MakeTags();

The code to set all the other attributes of the cells, like the font, size, margins, etc., could be reduced and made a bit more succinct as well, but it's fine as is. I can also tease some of the new form rules in the upcoming FP 11.1 release that will enable you to define a lot of that stuff in a UI instead of having to do it in code.

Link to comment
Share on other sites

Thank You Dan. I see where I was inserting my code incorrectly. I wasn't adding the 4 fields in the correct spot. Thanks again for all your help. Invaluable!!

If you don't mind. Can you explain the comments below I've added so i get a better understanding of this code.

 

 

 

 

var myTable = new FPTable;
myTable.AddColumns(5760, 21168, 4536, 4536);  

for (var i = 1; i <= 20; i++)  [color="Red"]Var i=1  What does i mean or denote? 
the next <=20: i++)this tells it to loop trhough 20 lines, but what does i++ mean or denote.[/color]
{
   var item = Field("Item " + i);
   var desc = Field("Desc " + i);
   var disc = Field("Disc " + i);
   var price = Field("Price " + i);

   // There 2 different triggers to not include a row.
   // 1. data is blank for that row.
   if (!item && !desc && !disc && !price)
       continue;

   // 2. The "Disc" field is zero.
   if (Int(disc) == 0)
       continue;

   var row = myTable.AddRow();

   var cell = row.Cells[0];
   cell.SetBorders("Thin", "Black", "Bottom");
   cell.Margins = new FPTableMargins;
   cell.Margins.Top = 25;
   cell.Margins.Bottom = 25;
   row.CopyCells(0, 1,2,3);

   cell.Margins = new FPTableMargins;
   cell.Margins.Top = 25;
   cell.Margins.Bottom = 25;
   cell.Margins.Left = 350;
   row.CopyCells(0, 1);

   row.Cells[0].HAlign = "Left";
   row.Cells[1].HAlign = "Left";
   row.Cells[2].HAlign = "Center";
   row.Cells[3].HAlign = "Center";

   row.Cells[0].Font = "News Gothic Bold";
   row.Cells[0].PointSize = 6;
   row.Cells[1].Font = "News Gothic Regular";
   row.Cells[1].PointSize = 7;
   row.Cells[2].Font = "News Gothic Bold";
   row.Cells[2].PointSize = 7;
   row.Cells[3].Font = "News Gothic Regular";
   row.Cells[3].PointSize = 7;

   row.SetContents(item, desc, disc, price);
}

return myTable.MakeTags();

Link to comment
Share on other sites

Thank You Dan. I see where I was inserting my code incorrectly. I wasn't adding the 4 fields in the correct spot. Thanks again for all your help. Invaluable!!

Glad to help!

If you don't mind. Can you explain the comments below I've added so i get a better understanding of this code.

for (var i = 1; i <= 20; i++)  [color="Red"]Var i=1  What does i mean or denote? 
the next <=20: i++)this tells it to loop trhough 20 lines, but what does i++ mean or denote.[/color]/CODE][/quote]
A [url="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration#for_statement"]JavaScript for statement[/url] has four parts: an initial expression (var i = 1), a condition (i <= 20), an increment expression (i++), and a statement block (the code in brackets, or a single statement, after the "for" line).

The two plus signs are the [url="https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Arithmetic_Operators#Increment"]increment operator[/url].  So "i++" simply means, "add one to i."  In this case, it adds one for every iteration of the loop.

If you didn't have the i++ there to increment i (and no other code in the statement block to change its value), the value of i would never change, and would stay at 1, and since the loop says to continue as long as i is less than or equal to 20, it would effectively be an infinite loop.

While writing a "for" loop this way, with a single "counter" variable and the ++ operator, is by far the most common usage, you aren't just limited to "add one to the counter variable."  You could do something like this to increment backwards:
[code]for (var i = 20; i >= 1; i--)

Where -- is, of course, the decrement operator, just like the increment operator, but it substracts one each time.

 

Or you could do something like increment by two (and only deal with odd or even numbers, depending where you start:

for (var i = 0; i <= 20; i += 2)

 

There are more advanced things you can do also. For instance, you don't have to reference the same variable in the initial, condition, and increment parts of the loop. And sometimes a different kind of loop is better, such as a while or do..while statement, or even a for...in loop. (In fact, any "for" loop could be rewritten as a "while" loop.) You can read more about different kinds of loops and iteration here:

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Loops_and_iteration

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