How do I get my jQuery to scroll through this table correctly?

0

I created a script to go through the table and return a console.log of the content in it, but in the first few lines does not take the name of the Course, just the prices.

I saw that in the table TD where it says RIGHT, it has a rowspan = 2 in the css. But I do not know what condition I should use for my script to read correctly.

Link of table that I want to go through with jQuery:

link

My code looks like this:

var c = [];
            jQuery('table tbody tr:gt(4)').each(function(ii, tr){
                if($(tr).find('td:eq(1)').text().indexOf("R$")>-1){
                    var price = {
                        course_name: $(tr).find('td:eq(0)').text(),
                        price: $(tr).find('td:eq(1)').text(),
                        turn: 'Matutino'
                    };
                    c.push(price);

                    var price = {
                        course_name: $(tr).find('td:eq(0)').text(),
                        price: $(tr).find('td:eq(1)').text(),
                        turn: 'Vespertino'
                    };
                    c.push(price);

                    var price = {
                        course_name: $(tr).find('td:eq(0)').text(),
                        price: $(tr).find('td:eq(1)').text(),
                        turn: 'Noturno'
                    };
                    c.push(price);

                    }
            }); console.log(c);
    
asked by anonymous 08.11.2018 / 17:21

2 answers

1

I made this way where I separate variables for each shift, checking if the column has rowspan to get the corresponding value of the turn. To get the name of the course, you can put it in if a .prev() .

var c = [];
jQuery('table tbody tr:gt(4)').each(function(ii, tr){

   if($(tr).find('td:eq(0)').text().indexOf("R$") > -1){

      var curso = $(tr).prev().find('td:eq(0)').text();
      var colspan0 = $(tr).find('td:eq(0)').attr('colspan');
      var colspan1 = $(tr).find('td:eq(1)').attr('colspan');

      if(colspan0 == 2){
         var price_m = price_v = $(tr).find('td:eq(0)').text();
         var price_n = $(tr).find('td:eq(1)').text();
      }else if(colspan1 == 2){
         var price_m = $(tr).find('td:eq(0)').text();
         var price_v = price_n = $(tr).find('td:eq(1)').text();
      }else{
         var price_m = price_v = price_n = $(tr).find('td:eq(0)').text();
      }

   }else if($(tr).find('td:eq(1)').text().indexOf("R$") > -1){
      var curso = $(tr).find('td:eq(0)').text();
      var price_m = price_v = price_n = $(tr).find('td:eq(1)').text();
   }

   // só vai fazer o .push se o nome do curso for true
   if(curso){

      var price = {
         course_name: curso,
         price: price_m,
         turn: 'Matutino'
      };
      c.push(price);

      var price = {
         course_name: curso,
         price: price_v,
         turn: 'Vespertino'
      };
      c.push(price);

      var price = {
         course_name: curso,
         price: price_n,
         turn: 'Noturno'
      };
      c.push(price);
   }

}); console.log(c);

JSFIDDLE

    
08.11.2018 / 19:17
0

The text you're looking for is on a line you're not following: / with the statement table tbody tr:gt(4) you are starting with the line that has nothing in the first column due to colspan, if you start with table tbody tr:gt(3) you will get the "Right" but you will have to make a specific if for this case because the form is not entered and does not push in the array.

    
08.11.2018 / 18:19