2017-02-11 1 views
0

동일한 행의 다른 열에있는 셀을 기반으로 각 열의 조건부 합계를 계산하는 DataTables에 footerCallback을 구현하려고합니다. https://jsfiddle.net/rantoun/552y9j90/13/DataTables footerCallback - 다른 셀의 조건부

HTML :

<table id="table1"> 
    <thead> 
    <tr> 
     <th>Fruit</th> 
     <th>sumCondition</th> 
     <th># Eaten</th> 
     <th># Remaining</th> 
    </tr> 
    </thead> 
    <tfoot> 
    <tr> 
     <th></th> 
     <th align="center">Count</th> 
     <th align="left"></th> 
     <th align="left"></th> 
    </tr> 
    </tfoot> 
    <tbody> 
    <tr> 
     <td>Apples</td> 
     <td>Use</td> 
     <td>3</td> 
     <td>8</td> 
    </tr> 
    <tr> 
     <td>Oranges</td> 
     <td>Use</td> 
     <td>6</td> 
     <td>5</td> 
    </tr> 
    <tr> 
     <td>Bananas</td> 
     <td>Ignore</td> 
     <td>2</td> 
     <td>9</td> 
    </tr> 
    </tbody> 
</table> 

jQuery를 :

$("#table1").DataTable({ 
    "paging": false, 
    "searching": false, 
    "info": false,  
    "footerCallback": function (row, data, start, end, display) { 

     var columns = [2, 3]; 
     var api = this.api(); 

     _.each(columns, function(idx) { 

      var total = api 
       .column(idx) 
       .data() 
       .reduce(function (a, b) { 
        return parseInt(a) + parseInt(b); 
       }, 0)   

       $('tr:eq(0) th:eq('+idx+')', api.table().footer()).html(total); 
     }) 

    } 
}); 

는 특히, 내 목표는 "무시"만을 요약 행에 footerCallback입니다하지 않을 여기 내 설치의 데모입니다 조건 열 바라기를 이것은 분명하고 어떤 도움을 주셔서 감사합니다.

답변

0

나는 reduce 함수에서 합계 값의 현재 인덱스를 가져 와서이를 해결하고 인덱스를 사용하여 조건 셀의 각 값에 액세스했습니다. 아래는 새로운 jQuery 코드입니다 :

$("#table1").DataTable({ 
    "paging": false, 
    "searching": false, 
    "info": false,  
    "footerCallback": function (row, data, start, end, display) { 


    var columns = [2, 3]; 
    //console.log(data); 
    var api = this.api(); 

    // Get sumCondition and put in array  

    _.each(columns, function(idx) { 

     var total = api 
      .column(idx) 
      .data() 
      .reduce(function (a, b) { 
       // Find index of current value for accessing sumCondition value in same row 
       var cur_index = api.column(idx).data().indexOf(b); 
       if (api.column(1).data()[cur_index] != "Ignore") { 
       return parseInt(a) + parseInt(b); 
       } 
       else { return parseInt(a); } 
      }, 0)   

      $('tr:eq(0) th:eq('+idx+')', api.table().footer()).html(total); 
    }) 

} 은});

작업 바이올린 :https://jsfiddle.net/rantoun/552y9j90/14/

관련 문제