简体   繁体   中英

Jquery Sum Values based on dropdown list filter

i have a dropdown filter that works and filter the data on my jquery datatable, but i was wondering how can i make that each time someone select a value from my dropdown list and this gets filter i need to sum all values from the filtered datatable??? how can i do this?

My table is created using Jquery datatables I need to sum all values from my 'td' 'Total' based on the value of the user selects how can i do this in javascript or jquery?

This is my Dropdown list :

<select id="selectorpdv">
  <option value="">Todos</option>
  <option value="01/02/2016">@item</option> // USER SELECTS THIS VALUE
  <option value="02/02/2016">@item</option>
</select>     

This is my filter code:

$('#selectorpdv').on('change', function () {
  dataTable.columns(2).search(this.value).draw();
});

This is my table code:

 <table class='table datatable-column-search-inputs table-hover table-striped table-bordered'>

    <thead>

        <tr>
            <td>Con</td>
            <td class='thead_search'>Platillo</td>
            <td class='thead_select'>Pdv</td>
            <td class='thead_select'>Rid</td>
            <td >PV</td>
            <td>0</td>

            <td >Total</td>
            <td class='sum'>Venta</td>
            <td class='fechas'>Fecha</td>

        </tr>
    </thead>
    <tfoot>
        <tr>
            <th colspan="4" style="text-align:left">Total:</th>
            <th></th>
        </tr>
    </tfoot>

</table>

There is an api for the same provided below :

    dataTable.column( i ).data().sum();

If above not work in your current version of datatables library please use code below :

  function sumColumn() {
        var dataArray = dataTable._('td:nth-child(4)', {"filter": "applied"});
        var sum = 0;    
        for (var i=0, len=dataArray.length; i < len; i++) {
            sum += +dataArray[i];
        } 
        alert(sum);
}

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