IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Angular Discussion :

[Angular 2] Totaux d'un tableau


Sujet :

Angular

  1. #1
    Membre actif
    Avatar de didate
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2010
    Messages
    90
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Guinée

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2010
    Messages : 90
    Points : 238
    Points
    238
    Par défaut [Angular 2] Totaux d'un tableau
    Bonjour,

    Je veux calculer des totaux d'un tableau obtenu avec *ngFor d'angular 4. Je me demande s'il n'y a pas un truc sur angular 4 qui me permet de le faire sans forcement passé par une lib externe.


    Voici un extrait du tableau.

    Nom : Capture.PNG
Affichages : 1661
Taille : 14,0 Ko


    Voici le template HTML.
    Code HTML : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    <div>
        <h2>
            <span jhiTranslate="appTransfetApp.transaction.home.title">Transactions</span>
            <button class="btn btn-primary float-right create-transaction" [routerLink]="['/', { outlets: { popup: ['transaction-new'] } }]">
                <span class="fa fa-plus"></span>
                <span  jhiTranslate="appTransfetApp.transaction.home.createLabel">
                Create new Transaction
                </span>
            </button>
        </h2>
        <jhi-alert></jhi-alert>
        <div class="row">
        </div>
        <br/>
        <div class="" *ngIf="transactions">
            <table class="table table-striped table-bordered table-responsive">
                <thead>
                    <tr>
                        <th colspan="5"></th>
                        <th [colSpan]="unities?.length" class="text-center"><span>Entré</span></th>                   
                        <th [colSpan]="unities?.length" class="text-center"><span >Sortie</span></th>
                        <th [colSpan]="unities?.length" class="text-center"><span >Frais</span></th>
                        <th [colSpan]="unities?.length" class="text-center"><span >Payement Provenance</span></th>
                        <th [colSpan]="unities?.length" class="text-center"><span >Payement Destination</span></th>
                        <th [colSpan]="unities?.length" class="text-center"><span >Part du Bureau</span></th>
                        <!-- <th [colSpan]="unities?.length" class="text-center"><span >Payement</span></th> -->
                        <th colspan="2" class="text-center"><span >Agences</span></th>
     
                    </tr>
     
                <tr>
                    <th><span jhiTranslate="appTransfetApp.transaction.dateCreated">Date Created</span></th>
                    <th><span jhiTranslate="appTransfetApp.transaction.codeEntre">Code Entre</span></th>
                    <th><span jhiTranslate="appTransfetApp.transaction.clientEnvoyeur">Client Envoyeur</span></th>
                    <th><span jhiTranslate="appTransfetApp.transaction.clientReceveur">Client Receveur</span></th>
                    <th><span jhiTranslate="appTransfetApp.transaction.codeSortie">Code Sortie</span></th>
     
                    <th *ngFor="let u of unities ;trackBy: trackId">
                        <span>{{u.uniteCode}}</span>
                    </th>
                    <th *ngFor="let u of unities ;trackBy: trackId">
                        <span>{{u.uniteCode}}</span>
                    </th>
                    <th *ngFor="let u of unities ;trackBy: trackId">
                        <span>{{u.uniteCode}}</span>
                    </th>
                    <th *ngFor="let u of unities ;trackBy: trackId">
                        <span>{{u.uniteCode}}</span>
                    </th>
     
                    <th *ngFor="let u of unities ;trackBy: trackId">
                        <span>{{u.uniteCode}}</span>
                    </th>
     
                    <th *ngFor="let u of unities ;trackBy: trackId">
                        <span>{{u.uniteCode}}</span>
                    </th>
     
                    <th><span jhiTranslate="appTransfetApp.transaction.agenceEnvoyeur">Provenance</span></th>
                    <th><span jhiTranslate="appTransfetApp.transaction.agenceReceveur">Destination</span></th>            
                    <th><span jhiTranslate="appTransfetApp.transaction.state">State</span></th>
     
                    <!-- <th><span jhiTranslate="appTransfetApp.transaction.montantEnvoyer">Montant Envoyer</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.montantRemis">Montant Remis</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.tauxFrais">Taux Frais</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.montantFrais">Montant Frais</span></th>
     
     
     
     
     
     
                <th><span jhiTranslate="appTransfetApp.transaction.userCreated">User Created</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.userValidated">User Validated</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.userCanceled">User Canceled</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.userUpdated">User Updated</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.userPaid">User Paid</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.uniteEnvoi">Unite Envoi</span></th>
                <th><span jhiTranslate="appTransfetApp.transaction.uniteRecu">Unite Recu</span></th>
                <th></th> -->
                </tr>
                </thead>
                <tbody>
                <tr *ngFor="let transaction of transactions ;trackBy: trackId">
     
                    <td>{{transaction.dateCreated | date:'shortDate'}}</td>
                    <td>{{transaction.codeEntre}}</td>
                    <td >
                        <div class="text-nowrap" *ngIf="transaction.clientEnvoyeur">
                            {{transaction.clientEnvoyeur?.clientName}} {{transaction.clientEnvoyeur?.clientPhone}}
                        </div>
                    </td>
                    <td >
                        <div class="text-nowrap" *ngIf="transaction.clientReceveur">
                            {{transaction.clientReceveur?.clientName}} {{transaction.clientReceveur?.clientPhone}}
                        </div>
                    </td>
     
                    <td>{{transaction.codeSortie}}</td>
     
                    <td *ngFor="let u of unities ;trackBy: trackI">                    
                        <span *ngIf="transaction.uniteEnvoi?.id == u.id" [class]="'entre_'+transaction.uniteEnvoi.uniteCode">{{transaction.montantEnvoyer}}</span>
                    </td>
     
                    <td *ngFor="let u of unities ;trackBy: trackI">
                        <span *ngIf="transaction.uniteRecu?.id == u.id" [class]="'sortie_'+transaction.uniteEnvoi.uniteCode">{{transaction.montantRemis}}</span>
                    </td>
     
                    <td *ngFor="let u of unities ;trackBy: trackI">
                        <span *ngIf="transaction.uniteEnvoi?.id == u.id" [class]="'frais_'+transaction.uniteEnvoi.uniteCode">{{transaction.montantFrais}}</span>
                    </td>
     
                    <td *ngFor="let u of unities ;trackBy: trackI">
                        <span *ngIf="transaction.uniteEnvoi?.id == u.id" [class]="'pe_'+transaction.uniteEnvoi.uniteCode">
                            {{ transaction.agenceEnvoyeur.tauxEnvoi* transaction.montantFrais/100}}
                        </span>
                    </td>
     
                    <td *ngFor="let u of unities ;trackBy: trackI">
                        <span *ngIf="transaction.uniteEnvoi?.id == u.id" [class]="'ps_'+transaction.uniteEnvoi.uniteCode">
                            {{ transaction.agenceReceveur.tauxReception* transaction.montantFrais/100}}
                        </span>
                    </td>        
     
                    <td *ngFor="let u of unities ;trackBy: trackI" >
                        <span *ngIf="transaction.uniteEnvoi?.id == u.id" [class]="'bureau_'+transaction.uniteEnvoi.uniteCode">
                            {{ transaction.montantFrais - ((transaction.agenceEnvoyeur.tauxEnvoi* transaction.montantFrais/100) + (transaction.agenceReceveur.tauxReception* transaction.montantFrais/100))}}
                        </span>
                    </td>
     
     
                    <td>
                        <div *ngIf="transaction.agenceEnvoyeur">
                            {{transaction.agenceEnvoyeur?.agenceName}}
                        </div>
                    </td>
                    <td>
                        <div *ngIf="transaction.agenceReceveur">
                            {{transaction.agenceReceveur?.agenceName}}
                        </div>
                    </td>
     
                    <td jhiTranslate="{{'appTransfetApp.TransactionState.' + transaction.state}}">{{transaction.state}}</td>
     
                    <!--<td class="text-right">
                        <div class="btn-group flex-btn-group-container">
                            <button type="submit"
                                    [routerLink]="['../transaction', transaction.id ]"
                                    class="btn btn-info btn-sm">
                                <span class="fa fa-eye"></span>
                                <span class="hidden-md-down" jhiTranslate="entity.action.view">View</span>
                            </button>
                            <button type="submit"
                                    [routerLink]="['/', { outlets: { popup: 'transaction/'+ transaction.id + '/edit'} }]"
                                    replaceUrl="true"
                                    class="btn btn-primary btn-sm">
                                <span class="fa fa-pencil"></span>
                                <span class="hidden-md-down" jhiTranslate="entity.action.edit">Edit</span>
                            </button>
                            <button type="submit"
                                    [routerLink]="['/', { outlets: { popup: 'transaction/'+ transaction.id + '/delete'} }]"
                                    replaceUrl="true"
                                    class="btn btn-danger btn-sm">
                                <span class="fa fa-remove"></span>
                                <span class="hidden-md-down" jhiTranslate="entity.action.delete">Delete</span>
                            </button>
                        </div>
                    </td> -->
                </tr>
                <tr>
                        <th colspan="5"><span class="pull-right">TOTAUX</span></th>
     
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
                        <td *ngFor="let u of unities ;trackBy: trackI">
     
                        </td>
     
     
                    </tr>
                </tbody>
            </table>
        </div>
    </div>


    Bien à vous.
    L'effort fait les forts

  2. #2
    Expert éminent sénior
    Avatar de Marco46
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Août 2005
    Messages
    4 413
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Âge : 42
    Localisation : France, Paris (Île de France)

    Informations professionnelles :
    Activité : Développeur informatique

    Informations forums :
    Inscription : Août 2005
    Messages : 4 413
    Points : 19 609
    Points
    19 609
    Par défaut
    La bonne pratique c'est de mettre un minimum de logique dans la partie html du front.

    Et donc de générer un view model approprié à la vue depuis les données remontées par le ou les backend(s) que l'on publie ensuite dans la vue pour avoir un simple ngRepeat à effectuer.

    Il faut donc faire les totaux dans la couche javascript (dans un service) avant de publier le viewmodel dans la vue.
    Un problème avec Git ? Essayez la FAQ, sinon posez votre question sur le forum.



    "Toute personne croyant qu'une croissance exponentielle peut durer indéfiniment dans un monde fini est soit un fou, soit un économiste."
    Kenneth E. Boulding

    "Les richesses naturelles sont inépuisables, car, sans cela, nous ne les obtiendrions pas gratuitement. Ne pouvant être ni multipliées ni épuisées, elles ne sont pas l’objet des sciences économiques."
    Jean-Baptiste Say, Traité d'économie politique, 1803.

    "/home/earth is 102% full ... please delete anyone you can."
    Inconnu

  3. #3
    Membre actif
    Avatar de didate
    Homme Profil pro
    Développeur informatique
    Inscrit en
    Juin 2010
    Messages
    90
    Détails du profil
    Informations personnelles :
    Sexe : Homme
    Localisation : Guinée

    Informations professionnelles :
    Activité : Développeur informatique
    Secteur : High Tech - Éditeur de logiciels

    Informations forums :
    Inscription : Juin 2010
    Messages : 90
    Points : 238
    Points
    238
    Par défaut
    Merci Marco46

    En m'inspirant de ta réponse j'ai fais un truc comme ça (coté front bien-sûr) :

    Code javascript : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
            this.transactionService.query().subscribe((res: ResponseWrapper) => {
                    this.transactions = res.json;
     
                    // calcul des totaux;
     
                    // création des arrays
                    this.totauxEntree = new Array(this.unities.length);
                    this.totauxSortie = new Array(this.unities.length);
                    this.totauxFrais = new Array(this.unities.length);
                    this.totauxPP = new Array(this.unities.length);
                    this.totauxPD = new Array(this.unities.length);
                    this.totauxPB = new Array(this.unities.length);
     
                    // Initialisation à 0 des valeurs des arrays
                    for (var i = 0; i < this.totauxEntree.length; i++) {
                        this.totauxEntree[i] = 0;
                        this.totauxSortie[i] = 0;
                        this.totauxFrais[i] = 0;
                        this.totauxPP[i] = 0;
                        this.totauxPD[i] = 0;
                        this.totauxPB[i] = 0;
                    }
                    this.transactions.forEach((t)=>{
     
                        for (var i = 0; i < this.unities.length; i++) {
                            if(t.uniteEnvoi.id === this.unities[i].id){
                                // 1- Totaux entrée
                                this.totauxEntree[i] += t.montantEnvoyer;
                                // 2- Totaux sortie
                                this.totauxSortie[i] += t.montantRemis;
                                // 3- Totaux Frais
                                this.totauxFrais[i] += t.montantFrais;
                                // 4- Totaux Payement provenance
                                this.totauxPP[i] += t.agenceEnvoyeur.tauxEnvoi* t.montantFrais/100;
                                 // 5- Totaux Payement destinataire
                                this.totauxPD[i] += t.agenceReceveur.tauxReception* t.montantFrais/100;
     
                            }
                        }
                    })
                    // 6- Totaux Part du bureau
                    for (var i = 0; i < this.unities.length; i++) {                    
                        this.totauxPB[i] +=  (this.totauxFrais[i] - (this.totauxPP[i] + this.totauxPD[i]));    
                    } 
                }
    Je ne sais pas vraiment si c'est propre, mais bon je pense que c'est acceptable sachant que le tableau n'est pas trop grand.


    Merci encore une fois.
    L'effort fait les forts

+ Répondre à la discussion
Cette discussion est résolue.

Discussions similaires

  1. [WRS] Calcul de Totaux dans un tableau
    Par fbe95 dans le forum Outils BI
    Réponses: 2
    Dernier message: 13/09/2013, 10h30
  2. [AC-2007] retirer sous totaux d'un tableau dynamique
    Par chris45 dans le forum IHM
    Réponses: 3
    Dernier message: 06/03/2012, 22h22
  3. [DeskI V5-V6] Calculer des sous totaux dans un tableau croisé dynamique
    Par Tancredoc dans le forum Débuter
    Réponses: 17
    Dernier message: 09/06/2010, 14h22
  4. [E-03][Vba]faire une case totaux dans un tableau dynamique
    Par Overcrash dans le forum Macros et VBA Excel
    Réponses: 2
    Dernier message: 18/06/2008, 11h39
  5. Réponses: 1
    Dernier message: 11/07/2007, 16h58

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo