Transpose array of TDs into Columns jQuery

后端 未结 2 1259
庸人自扰
庸人自扰 2021-01-23 07:43

Please help me put the cells with a class that corresponds to the column header, in the appropriate column. The iteration should be done per column and then loop through the

2条回答
  •  独厮守ぢ
    2021-01-23 08:14

    To do so. Initially an array of objects formed as wrongly sorted html-elements in your code.

    (Сделал так. Изначально сформировал массив объектов, так как в вашем коде неправильно сортированы html-элементы.)

    $(function() {
      var $tempScanner = $('table.temp tr td');
      var tempArry = [];
    
      $tempScanner.each(function(i, el) {
        var d = {};
        d.text = $(el).text();
        d.html = $(el).html();
        d.class = $(el).attr('class');
        tempArry.push(d);
      });
    
      function compareObj(o1, o2) {
        return o1.text > o2.text;
      }
    
      tempArry = tempArry.sort(compareObj);
      console.log(tempArry);
    
      for (var i = 0; i < tempArry.length; i++) {
        var tdClass = tempArry[i].class;
        $('#tblGrid td.' + tdClass + ':empty:first').html(tempArry[i].html);
      }
    
    });
    td,
    th {
      border: 1px solid #111;
      padding: 6px;
    }
    th {
      font-weight: 700;
    }
    span.pull-right {
      float: right;
      text-align: right;
    }
    .a,
    .A {
      background-color: #ACE;
    }
    .b,
    .B {
      background-color: #FAF;
    }
    .c,
    .C {
      background-color: #BAB;
    }
    .d,
    .D {
      background-color: #ECA;
    }
    .targetFound {
      border: solid 2px red;
    }
    
    
    Oookr.8 Ppppkr.10 Kkkkkr.6 Ffffkr.8 Sssskr.10 Vvvvkr.6 Iiiikr.10 Llllkr.6 Mmmmkr.9 Bbbbkr.12 Eeeekr.8 Ggggkr.6 Cccckr.6 Aaaakr.6 Nnnnkr.10 Zzzzkr.8 Mmmmkr.6 Rrrrkr.6 Hhhhkr.5 Uuuukr.5 Qqqqkr.5 Xxxxkr.5
    A B C D

提交回复
热议问题