alfajango / jquery-dynatable

A more-fun, semantic, alternative to datatables
http://www.dynatable.com
Other
2.77k stars 363 forks source link

Calculation on a column #258

Open agrepe opened 7 years ago

agrepe commented 7 years ago

If it helps . Summing the elements of a column after sorting . First you must create an INPUT for sorting and then create the piece of script to process this INPUT.

For example, for a column of my table called TA. Dans le HTML:

    <div class="control">
        <select id="search-ta" name="TA" type="text">
          <?php
           $tab = $selection->fetchAll();
            foreach($tab as $col){
            echo '<option value="'.$col['nom'].'">'.$col['nom'].'</option>';
            }
          ?>
        </select>
    </div>

And encompass a span and assign an ID to the relevant column. ( On the lines of the table not the header)

I'll add all the elements of the column " Ajustment " My table is called: my-table

Dans le script: $(function(){ function additionTime(){ var temps = $('#my-table').find('.ajustement'); var totalTemps = 0;

 temps.each(function(){ //Pour chaque span          
           var CeTemps = parseInt($(this).text()); 
           totalTemps += CeTemps;
        });
  //On sort de la boucle car chaque valeur est traitée

var tempsDate = new Date(totalTemps); // Convertir "totalTemps" en Date hh:mm var heureFinal = ("0" + tempsDate.getHours()).slice(-2); var minuteFinal = ("0" + tempsDate.getMinutes()).slice(-2); //ID à afficher dans l'INPUT var TempsCumule = (heureFinal -1) + ":" + minuteFinal;$(function(){ //Je définis les action de ma fonction et je lui donne un nom }); jQuery('#LeResultat').val(TempsCumule);
var resultMSContainer = $('#resultatMS'); var resultDateContainer = $('#resultatDate');
resultMSContainer.text(totalTemps); resultDateContainer.text(tempsDate);
} var button = $('#calcul'); button.click(function(){ additionTime(); //J'appelle ma fonction });
$('#reset').click(function(){ //Au click $('#resultatMS, #resultatDate').text(''); //Je vide les résultats }); }); //FILTERS // Recherche var dynatable = $('#my-table').dynatable({ features: { paginate: false, recordCount: false, sorting: false } }).data('dynatable');

// Filtre TA
    $('#search-ta').change( function() { // ID du filter (INPUT)
        var value = $(this).val();
            if (value === "") {
            dynatable.queries.remove("TA"); 
            } else {
        dynatable.queries.add("TA",value); // En-tête de la colonne
        }
        //dynatable.process();
        dynatable.process().bind('dynatable:afterProcess, additionTime');
    });