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

Asked

Viewed 105 times

0

I created a script to scroll through the table and return me a console.log of the content in it, but in the first few lines it doesn’t take the name of the Course, only the prices.

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

Link to the table I want to browse with jQuery:

http://www.ciesa.br/details-menu/107-indicadores-de-qualidade-2.html

My code is as follows:

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);
  • Getting the name of the course is even easy, the problem is knowing what price is of what. For example, how will you know that Law is 1,100 in the morning and evening and 1,200 in the evening if they are in different rows and in different columns?

  • So, what’s going on is this. I just don’t know how to create a specific if for this line in the beginning that is out

  • Well eh. You have to do an if cabuloso to find the respective values. I’ll see if I can here.

  • I’m trying too, but still unsuccessful.

2 answers

1


I did this way where I separate variables for each turn, checking if the column has rowspan to be able to pick up the corresponding turn value. To pick up the course name, you can put in the if one .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

  • Man, that’s exactly what it was! I was hours trying to put together something like this. But as I am very beginner still I break myself to assemble codes and research enough to make a functional structure. Thanks!

0

The text you want is on a line you are not traversing :/ with the instruction table tbody tr:gt(4) you are starting with the row that has nothing in the first column due to colspan, if you start with table tbody tr:gt(3) you will be able to catch the "Right" but you will have to make a specific if for this case because the way it is does not enter and does not push the array.

  • Because then, this specific if that I do not know exactly how to do, as I am beginner I urge in this. This is the way I managed to get 90% of the table, but this beginning is breaking me.

Browser other questions tagged

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