简体   繁体   中英

How to count the rows in a DataTable based on the row values

I have a datatable in which i am storing some data.Now my need is that i want to count the rows on the basis of the row values.How to achieve that.I am posting my dataTable code

$("div.loader").show();
$("#backgroundPopup").show();
$("#backgroundPopup").css("opacity", "0.7");
var url = "<%=byopenjobResourceURL%>";
var type = "onLoadForByOpenJob";
var createTable = '<table id="mainTable" class="display"><thead><tr><th>Create Date</th><th>Job Order</th><th>ID</th><th>Owner</th><th>Client</th><th>Value</th><th>Product</th></thead><tbody>';
var totalActivities = '0';

jQuery.getJSON(url+"&type="+type, function(data) {

    for(var z=0; z<data.searchResultArray.length;z++){
        searchResultArray = data.searchResultArray[z].split("$$##$$##");    
        createTable = createTable + "<tr><td>"+searchResultArray[0]+"</td><td>"+searchResultArray[1]+"</td><td>"+searchResultArray[2]+"</td><td>"+searchResultArray[3]+"</td><td>"+searchResultArray[4]+"</td><td>"+searchResultArray[5]+"</td><td>"+searchResultArray[6]+"</td></tr>";         


    }



$('#mainTable').dataTable({
        "scrollY":        300,
        "scrollCollapse": true,
        "jQueryUI":       true,
        "aaSorting": []
    });

在此输入图像描述

How to achieve that?Lets say i want to count the product column by its content.ie i want to how many Search - Contigent is there

This is how you can sum up rows in a table

<table id="mainTable" class="display">
  <thead>
    <tr>
      <th>Create Date</th>
      <th>Job Order</th>
      <th>ID</th>
      <th>Owner</th>
      <th>Client</th>
      <th>Value</th>
      <th>Product</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
    <tr>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
      <td>1</td>
    </tr>
  </tbody>
</table>
<script type="text/javascript">
  function sumTable() {
    var rows = $('tbody tr'),
      result = [],
      sumLine = $('<tr>');
    rows.each(function(index, item){
      $(item).find('td').each(function(index, cell){
        if(!result[index]) { result[index] = 0; }
        result[index] += cell.innerText *1; 
      });
    });
    $.each(result, function(index, item) {
      var cell= $('<td>'); 
      cell.text(item);
      sumLine.append(cell);     
    });
    $('tbody').append(sumLine);
  }
  sumTable();
</script>

first cycle through all lines in the tbody and then for each line cycle through each td and sum the contents into an array. Which is added at the end. You could also just get the sum of the array instead of adding it as a row at the end of the table.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM