Angular datatables not refreshing the new data correctly

I am working with Angular datatables in my app and I am showing my list of data I am getting from the service, and I have 3 actions buttons in the UI (showAll – filter check in – filter check out)

at the first I am pushing the data I am getting to the main array and also to the filteredData array and on every filter action I am filtering the main array in change the filteredData array with the filtered data, and always showing the filteredData array in the datatable

The filter check in button filters the data of the list and just take the data which have the check_in_date same as today date and the filter check out data is taking the data with the check_out_date same as today and updates the filteredData array with the new data, and every function calls reinitializeTable() that refreshes the table

but the filteration only works at the first time, for example if I filtered the check ins it works fine but if I again tried to filter the check outs or decided to show all the data it just emptying the datatable

this is the ts code:

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>dtOptions: Config = {};
dtTrigger: Subject<any> = new Subject<any>();
reservations: Reservation[] = [];
filteredReservations: Reservation[] = [];
constructor(private reservationsService: ReservationsService) { }
ngOnInit(): void {
this.dtOptions = {
scrollX: true,
scrollCollapse: true,
pageLength: 25,
processing: true,
stateSave: true,
pagingType: 'full_numbers',
retrieve: true
};
this.getReservations();
}
getReservations(): void {
this.reservationsService.getReservations().subscribe(data => {
for (let reservation of data.data) {
let finalReservation: Reservation = new Reservation(
reservation.id, reservation.name, reservation.national_id, reservation.phone,
reservation.unit, reservation.platform, reservation.guest, reservation.reference_id,
reservation.day_amount, reservation.total_amount, reservation.paid_amount,
reservation.remaining_amount, reservation.notes, reservation.employee,
reservation.check_in_date, reservation.check_in_time, reservation.check_out_date,
reservation.check_out_time, reservation.duration, reservation.status
);
this.reservations.push(finalReservation);
}
this.filteredReservations = [...this.reservations];
this.dtTrigger.next(null);
console.log("All reservations: ", this.reservations);
}, error => {
Swal.fire({
icon: 'error',
title: 'Error!',
text: error.error.message
});
});
}
showAllReservations(): void {
this.filteredReservations = [...this.reservations];
this.reinitializeTable();
console.log("Show all reservations: ", this.filteredReservations);
}
filterCheckIns(): void {
const today = this.formatDate(new Date());
console.log("Today's date: ", today);
this.filteredReservations = this.reservations.filter(reservation => {
const checkInDate = this.formatDate(new Date(reservation.check_in_date));
console.log("Checking in date: ", checkInDate);
return checkInDate === today;
});
this.reinitializeTable();
console.log("Filtered check-ins: ", this.filteredReservations);
console.log("Main Reservations: ", this.reservations);
}
filterCheckOuts(): void {
const today = this.formatDate(new Date());
console.log("Today's date: ", today);
this.filteredReservations = this.reservations.filter(reservation => {
const checkOutDate = this.formatDate(new Date(reservation.check_out_date));
console.log("Checking out date: ", checkOutDate);
return checkOutDate === today;
});
this.reinitializeTable();
console.log("Filtered check-outs: ", this.filteredReservations);
console.log("Main Reservations: ", this.reservations);
}
formatDate(date: Date): string {
const day = ('0' + date.getDate()).slice(-2);
const month = ('0' + (date.getMonth() + 1)).slice(-2);
const year = date.getFullYear();
return `${day}/${month}/${year}`;
}
reinitializeTable(): void {
if ($.fn.dataTable.isDataTable('#reservationsTable')) {
$('#reservationsTable').DataTable().destroy();
}
this.dtTrigger.next(null);
}
ngOnDestroy(): void {
this.dtTrigger.unsubscribe();
}
</code>
<code>dtOptions: Config = {}; dtTrigger: Subject<any> = new Subject<any>(); reservations: Reservation[] = []; filteredReservations: Reservation[] = []; constructor(private reservationsService: ReservationsService) { } ngOnInit(): void { this.dtOptions = { scrollX: true, scrollCollapse: true, pageLength: 25, processing: true, stateSave: true, pagingType: 'full_numbers', retrieve: true }; this.getReservations(); } getReservations(): void { this.reservationsService.getReservations().subscribe(data => { for (let reservation of data.data) { let finalReservation: Reservation = new Reservation( reservation.id, reservation.name, reservation.national_id, reservation.phone, reservation.unit, reservation.platform, reservation.guest, reservation.reference_id, reservation.day_amount, reservation.total_amount, reservation.paid_amount, reservation.remaining_amount, reservation.notes, reservation.employee, reservation.check_in_date, reservation.check_in_time, reservation.check_out_date, reservation.check_out_time, reservation.duration, reservation.status ); this.reservations.push(finalReservation); } this.filteredReservations = [...this.reservations]; this.dtTrigger.next(null); console.log("All reservations: ", this.reservations); }, error => { Swal.fire({ icon: 'error', title: 'Error!', text: error.error.message }); }); } showAllReservations(): void { this.filteredReservations = [...this.reservations]; this.reinitializeTable(); console.log("Show all reservations: ", this.filteredReservations); } filterCheckIns(): void { const today = this.formatDate(new Date()); console.log("Today's date: ", today); this.filteredReservations = this.reservations.filter(reservation => { const checkInDate = this.formatDate(new Date(reservation.check_in_date)); console.log("Checking in date: ", checkInDate); return checkInDate === today; }); this.reinitializeTable(); console.log("Filtered check-ins: ", this.filteredReservations); console.log("Main Reservations: ", this.reservations); } filterCheckOuts(): void { const today = this.formatDate(new Date()); console.log("Today's date: ", today); this.filteredReservations = this.reservations.filter(reservation => { const checkOutDate = this.formatDate(new Date(reservation.check_out_date)); console.log("Checking out date: ", checkOutDate); return checkOutDate === today; }); this.reinitializeTable(); console.log("Filtered check-outs: ", this.filteredReservations); console.log("Main Reservations: ", this.reservations); } formatDate(date: Date): string { const day = ('0' + date.getDate()).slice(-2); const month = ('0' + (date.getMonth() + 1)).slice(-2); const year = date.getFullYear(); return `${day}/${month}/${year}`; } reinitializeTable(): void { if ($.fn.dataTable.isDataTable('#reservationsTable')) { $('#reservationsTable').DataTable().destroy(); } this.dtTrigger.next(null); } ngOnDestroy(): void { this.dtTrigger.unsubscribe(); } </code>
dtOptions: Config = {};
dtTrigger: Subject<any> = new Subject<any>();
reservations: Reservation[] = [];
filteredReservations: Reservation[] = [];

constructor(private reservationsService: ReservationsService) { }

ngOnInit(): void {
  this.dtOptions = {
    scrollX: true,
    scrollCollapse: true,
    pageLength: 25,
    processing: true,
    stateSave: true,
    pagingType: 'full_numbers',
    retrieve: true
  };

  this.getReservations();
}

getReservations(): void {
  this.reservationsService.getReservations().subscribe(data => {
    for (let reservation of data.data) {
      let finalReservation: Reservation = new Reservation(
        reservation.id, reservation.name, reservation.national_id, reservation.phone,
      reservation.unit, reservation.platform, reservation.guest, reservation.reference_id,
      reservation.day_amount, reservation.total_amount, reservation.paid_amount,
      reservation.remaining_amount, reservation.notes, reservation.employee,
      reservation.check_in_date, reservation.check_in_time, reservation.check_out_date,
      reservation.check_out_time, reservation.duration, reservation.status
      );
      this.reservations.push(finalReservation);
    }

    this.filteredReservations = [...this.reservations];
    this.dtTrigger.next(null);
    console.log("All reservations: ", this.reservations);
  }, error => {
    Swal.fire({
      icon: 'error',
      title: 'Error!',
      text: error.error.message
    });
  });
}

showAllReservations(): void {
  this.filteredReservations = [...this.reservations];
  this.reinitializeTable();
  console.log("Show all reservations: ", this.filteredReservations);
}

filterCheckIns(): void {
  const today = this.formatDate(new Date());
  console.log("Today's date: ", today);
  this.filteredReservations = this.reservations.filter(reservation => {
    const checkInDate = this.formatDate(new Date(reservation.check_in_date));
    console.log("Checking in date: ", checkInDate);
    return checkInDate === today;
  });
  this.reinitializeTable();
  console.log("Filtered check-ins: ", this.filteredReservations);
  console.log("Main Reservations: ", this.reservations);
}

filterCheckOuts(): void {
  const today = this.formatDate(new Date());
  console.log("Today's date: ", today);
  this.filteredReservations = this.reservations.filter(reservation => {
    const checkOutDate = this.formatDate(new Date(reservation.check_out_date));
    console.log("Checking out date: ", checkOutDate);
    return checkOutDate === today;
  });
  this.reinitializeTable();
  console.log("Filtered check-outs: ", this.filteredReservations);
  console.log("Main Reservations: ", this.reservations);
}

formatDate(date: Date): string {
  const day = ('0' + date.getDate()).slice(-2);
  const month = ('0' + (date.getMonth() + 1)).slice(-2);
  const year = date.getFullYear();
  return `${day}/${month}/${year}`;
}

reinitializeTable(): void {
  if ($.fn.dataTable.isDataTable('#reservationsTable')) {
    $('#reservationsTable').DataTable().destroy();
  }
  this.dtTrigger.next(null);
}

ngOnDestroy(): void {
  this.dtTrigger.unsubscribe();
}

And this the html code

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code><table id="reservationsTable" class="table table-hover row-border hover nowrap" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger">
<thead class="bg-danger">
<tr>
<th class="p-3 text-center">ID</th>
<th class="p-3 text-center">Name</th>
<th class="p-3 text-center">Unit</th>
<th class="p-3 text-center">Platform</th>
<th class="p-3 text-center">Check In Date</th>
<th class="p-3 text-center">Duration</th>
<th class="p-3 text-center">Total Amount</th>
<th class="p-3 text-center">Remaining Amount</th>
<th class="p-3 text-center">Status</th>
<th class="p-3 text-center">View</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let reservation of filteredReservations">
<td class="p-3 text-center">{{ reservation.id }}</td>
<td class="p-3 text-center">{{ reservation.name }}</td>
<td class="p-3 text-center">{{ reservation.unit.name }}</td>
<td class="p-3 text-center">
<img [src]="'https://back.sky2030central.com' + reservation.platform.logo"
[alt]="reservation.platform.name + 'Logo'" class="platform-logo">
</td>
<td class="p-3 text-center">{{ reservation.check_in_date | date: 'dd/MM/yyyy' }}</td>
<td class="p-3 text-center">{{ reservation.duration }}</td>
<td class="p-3 text-center">{{ reservation.total_amount }} SAR</td>
<td class="p-3 text-center">
<span class="p-2 rounded" [class.remaining-amount-positive]="+reservation.remaining_amount > 0"
[class.remaining-amount-negative]="+reservation.remaining_amount <= 0">{{ reservation.remaining_amount }}
SAR</span>
</td>
<td class="p-3 text-center">
<span class="p-2 rounded" [class.status-box-unconfirmed]="reservation.status === 'Unconfirmed'"
[class.status-box-confirmed]="reservation.status === 'Confirmed'"
[class.status-box-staying]="reservation.status === 'Staying'"
[class.status-box-suspended]="reservation.status === 'Suspended'"
[class.status-box-closed]="reservation.status === 'Closed'">{{ reservation.status }}</span>
</td>
<td class="p-3 text-center">
<button type="button" class="btn btn-success text-center" [routerLink]="'../view/' + reservation.id">
<i class="bi bi-eye"></i>
</button>
</td>
</tr>
</tbody>
</table>
</code>
<code><table id="reservationsTable" class="table table-hover row-border hover nowrap" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger"> <thead class="bg-danger"> <tr> <th class="p-3 text-center">ID</th> <th class="p-3 text-center">Name</th> <th class="p-3 text-center">Unit</th> <th class="p-3 text-center">Platform</th> <th class="p-3 text-center">Check In Date</th> <th class="p-3 text-center">Duration</th> <th class="p-3 text-center">Total Amount</th> <th class="p-3 text-center">Remaining Amount</th> <th class="p-3 text-center">Status</th> <th class="p-3 text-center">View</th> </tr> </thead> <tbody> <tr *ngFor="let reservation of filteredReservations"> <td class="p-3 text-center">{{ reservation.id }}</td> <td class="p-3 text-center">{{ reservation.name }}</td> <td class="p-3 text-center">{{ reservation.unit.name }}</td> <td class="p-3 text-center"> <img [src]="'https://back.sky2030central.com' + reservation.platform.logo" [alt]="reservation.platform.name + 'Logo'" class="platform-logo"> </td> <td class="p-3 text-center">{{ reservation.check_in_date | date: 'dd/MM/yyyy' }}</td> <td class="p-3 text-center">{{ reservation.duration }}</td> <td class="p-3 text-center">{{ reservation.total_amount }} SAR</td> <td class="p-3 text-center"> <span class="p-2 rounded" [class.remaining-amount-positive]="+reservation.remaining_amount > 0" [class.remaining-amount-negative]="+reservation.remaining_amount <= 0">{{ reservation.remaining_amount }} SAR</span> </td> <td class="p-3 text-center"> <span class="p-2 rounded" [class.status-box-unconfirmed]="reservation.status === 'Unconfirmed'" [class.status-box-confirmed]="reservation.status === 'Confirmed'" [class.status-box-staying]="reservation.status === 'Staying'" [class.status-box-suspended]="reservation.status === 'Suspended'" [class.status-box-closed]="reservation.status === 'Closed'">{{ reservation.status }}</span> </td> <td class="p-3 text-center"> <button type="button" class="btn btn-success text-center" [routerLink]="'../view/' + reservation.id"> <i class="bi bi-eye"></i> </button> </td> </tr> </tbody> </table> </code>
<table id="reservationsTable" class="table table-hover row-border hover nowrap" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger">
  <thead class="bg-danger">
    <tr>
      <th class="p-3 text-center">ID</th>
      <th class="p-3 text-center">Name</th>
      <th class="p-3 text-center">Unit</th>
      <th class="p-3 text-center">Platform</th>
      <th class="p-3 text-center">Check In Date</th>
      <th class="p-3 text-center">Duration</th>
      <th class="p-3 text-center">Total Amount</th>
      <th class="p-3 text-center">Remaining Amount</th>
      <th class="p-3 text-center">Status</th>
      <th class="p-3 text-center">View</th>
    </tr>
  </thead>
  <tbody>
    <tr *ngFor="let reservation of filteredReservations">
      <td class="p-3 text-center">{{ reservation.id }}</td>
      <td class="p-3 text-center">{{ reservation.name }}</td>
      <td class="p-3 text-center">{{ reservation.unit.name }}</td>
      <td class="p-3 text-center">
        <img [src]="'https://back.sky2030central.com' + reservation.platform.logo"
          [alt]="reservation.platform.name + 'Logo'" class="platform-logo">
      </td>
      <td class="p-3 text-center">{{ reservation.check_in_date | date: 'dd/MM/yyyy' }}</td>
      <td class="p-3 text-center">{{ reservation.duration }}</td>
      <td class="p-3 text-center">{{ reservation.total_amount }} SAR</td>
      <td class="p-3 text-center">
        <span class="p-2 rounded" [class.remaining-amount-positive]="+reservation.remaining_amount > 0"
          [class.remaining-amount-negative]="+reservation.remaining_amount <= 0">{{ reservation.remaining_amount }}
          SAR</span>
      </td>
      <td class="p-3 text-center">
        <span class="p-2 rounded" [class.status-box-unconfirmed]="reservation.status === 'Unconfirmed'"
          [class.status-box-confirmed]="reservation.status === 'Confirmed'"
          [class.status-box-staying]="reservation.status === 'Staying'"
          [class.status-box-suspended]="reservation.status === 'Suspended'"
          [class.status-box-closed]="reservation.status === 'Closed'">{{ reservation.status }}</span>
      </td>
      <td class="p-3 text-center">
        <button type="button" class="btn btn-success text-center" [routerLink]="'../view/' + reservation.id">
          <i class="bi bi-eye"></i>
        </button>
      </td>
    </tr>
  </tbody>
</table>

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật