How to display data array without duplication in angular 9?

135 views Asked by At

Html file:

   // First Table has array data --> name,netCash,phonePay2,card,cellPay2,total2 
   <table class="table table-hover table-bordered table-striped table-sm">
    <thead class="table-primary">
        <tr>
            <th class="th-sm">Collection</th>
            <th class="th-sm" *ngFor="let data of recObj">{{data?.name }}</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th class="th-sm">Cash</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.netCash | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Fone Pay</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.phonePay2 | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Card</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.card | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Cell Pay</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.cellPay2 | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Total</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.total2 | number : '.2-2'}}</td>
        </tr>
    </tbody>
</table>

// Second Table has array data --> trnUser,cashSales,fonePay,cardSales,cellPay,netSales 
<table class="table table-hover table-bordered table-striped table-sm">
    <thead class="table-primary">
        <tr>
            <th class="th-sm">Collection</th>
            <th class="th-sm" *ngFor="let data of recObj">{{data?.trnUser }}</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <th class="th-sm">Cash</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.cashSales | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Fone Pay</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.fonePay | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Card</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.cardSales | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Cell Pay</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.cellPay | number : '.2-2'}}</td>
        </tr>
        <tr>
            <th class="th-sm">Total</th>
            <td class="th-sm" *ngFor="let data of recObj">
                {{data?.netSales | number : '.2-2'}}</td>
        </tr>
    </tbody>
</table>

Ts File:

getSalesData() {
console.log(this.cnvForm.value);
this.salesService.getSalesCollectionVariance(this.cnvForm.value).subscribe(data => {
  this.recObj = data;   // array data is in recObj ie. Users, TrnMode, Net amount
  console.log(this.recObj);
  this.users =this.recObj.map(function(item)  // array of Users
  {
    console.log(this.users);
    return item.user
  });
  let trnmodes =this.recObj.map(function(item)   // array of TrnMode
  {
    console.log(trnmodes);
    return item.trnmode
  });

  let netamounts =this.recObj.map(function(item)  // array of Net amount
  {
    console.log(netamounts);
    return item.netamnt
  });
});    

}

Array data:

(2) [{…}, {…}]
  0:
    card: "1631.00"
    cardSales: "1631.00"
    cashSales: "41097.00"
    cellPay: ""
    cellPay2: ""
    div: "BAN"
    division: "BAN"
    fonePay: "0.00"
    name: "prabina"
    netCash: "41097.00"
    netSales: "42728.000000000000"
    phonePay2: "0.00"
    postDateTime: "03/12/20 12:00:00 AM"
    total2: "42728.000000000000"
    trnDate: "03/12/20 12:00:00 AM"
    trnUser: "prabina"
  1: {trnDate: "03/12/20 12:00:00 AM", division: "BAN", trnUser: "prakash", netSales: "80439.200000000000", cashSales: "65376.01", …}

Array Data: [![Console data I am getting this data in console in angular. It is same data as Array data in above --> Sql server data][1]][1] I am getting this data in console --> Sql server data

This is the array data that i want to display unique name with their respective Cash, CreditCard, QRCode in Html page

Html view [Screenshoot] [![enter image description here][3]][3]

Just want to display data in its position as shown in Html view [Screenshoot]. I tried so hard to resolve this problem but could not.. Help me to solve this problem.. Thank you For helping hand...

1

There are 1 answers

7
StefanK On

You can use the NgFor directive to loop over your data array (https://angular.io/api/common/NgForOf).

A possible approach could look like this example here: https://stackblitz.com/edit/angular-nhtmsz?file=src%2Fapp%2Fapp.component.html

EDIT

I revised the Stackblitz to include a possible approach for the data accumulation.