count duplicate value from table column using jquery or javascript and pass to rowspan

前端 未结 4 1830
予麋鹿
予麋鹿 2020-12-22 14:58

I want to count duplicate value from table column and pass to rowspan using javascript or jQuery.

$(document).ready(fu         


        
4条回答
  •  死守一世寂寞
    2020-12-22 15:29

    you can do this by using selectors

    $('#tableId tbody tr td:nth-child(1)');
    

    $(document).ready(function(){
    var counts={},valuesArray=[];
     $('#tableId tbody tr td:nth-child(1)').each( function(){
       //add item to array
       var colValue=$(this).text();
       if($.inArray(colValue,valuesArray)==-1){
          counts[colValue]=1;
          valuesArray.push(colValue);
          
       }
       else{
          var oldCount=counts[colValue];
          counts[colValue]= ++oldCount;
       }
       
    });
    valuesArray=[];
     $('#tableId tbody tr td:nth-child(4)').each( function(){
        var currentObj=$(this);
        if(currentObj.length>0){
           $('#tableId tbody tr td:nth-child(1)').each( function(){
             var colValue=$(this).text();
             debugger;
              if($.inArray(colValue,valuesArray)==-1){
               var rowspanValue=counts[colValue];
               if( currentObj.attr("rowspan")==undefined)
               {
                 currentObj.attr("rowspan",rowspanValue);
                 currentObj.text(rowspanValue);
                 valuesArray.push(colValue);
               }
             }
           })
        }
    });
    
    });
    table {
      font-family: arial, sans-serif;
      border-collapse: collapse;
      width: 100%;
    }
    
    td, th {
      border: 1px solid #ffffdffffd;
      text-align: left;
      padding: 8px;
    }
    
    
    VISA Country Category Total VISA
    123456 abcc Plumber
    123456 abcc Plumber
    123456 abcc Plumber
    8787 ffffd Plumber
    8787 ffffd Plumber
    8787 ffffd Plumber
    8787 ffffd Plumber
    8787 ffffd Plumber

提交回复
热议问题