Calculation of Javascript rows and columns for PDF

Asked

Viewed 90 times

0

I need to understand this section of js code that is responsible for calculating the positioning of rows and columns, from a html table, to PDF.

$(el).find('tbody').find('tr').each(function(index,data) {
      rowCalc = index+1;                                            
      if (rowCalc % 35 == 0){
            doc.addPage();
            page++;
            startRowPosition=startRowPosition+10;

      }
      rowPosition=(startRowPosition + (rowCalc * 10)) - ((page -1) * 280);

      $(this).filter(':visible').find('td').each(function(index,data) {
            if ($(this).css('display') != 'none'){  
                if(defaults.ignoreColumn.indexOf(index) == -1){
                        var colPosition = startColPosition+ (index * 50);                                   
                        doc.text(colPosition,rowPosition, parseString($(this)));
                }
            }
    }); 
}); 

The greatest riddle is in:

Function(index, date)

I cannot understand what this function represents and what the values of the index and date parameters are.

1 answer

0


Whoa, that’s all right?

So this particular part of the code represents the initialization of a Jquery foreach.

each(function(index,data) {}

That is, each element of the array $(this).filter(':visible').find('td') will be iterated by for each, the parameter index refers to an element counter that is being iterated. Already the date is the value being iterated.

Example:

const test = ['a','b','c']
$test.each(function(index, data){
  console.log(index, data) //vai printar na primeira iteracao (0, a)
  console.log(index, data) //vai printar na segunda iteracao (1, b)
  console.log(index, data) //vai printar na terceira iteracao (2, c)
}

Here is the Jquery documentation link with more examples of using the $each: https://api.jquery.com/each/

  • in the case would have only a console.log(index, data) that would iterate 3 times right?

  • Exactly! The console.log will only be one, I put the three to show all iterations that will happen.

  • show! thank you so much

Browser other questions tagged

You are not signed in. Login or sign up in order to post.