0

im new to angular and I cant seem to get my datatable working properly. Any help would be greatly appreciated!

This is what i'm currently getting - it just says No data available in table and i cant get the sorting or search to work either This is what i'm currently getting

component.html

<table datatable [dtOptions]="dtOptions" datatable="ng" class="row-border hover table table-striped table-bordered">
        <thead>
          <tr>
            <th>Name</th>
            <th>Company</th>
            <th>Email</th>
            <th>Action</th>
          </tr>
        </thead>
        <tbody>
          <tr *ngFor="let client of clients">
            <td>{{ client?.clientName }}</td>
            <td>{{ client?.clientCompany }}</td>
            <td>{{ client?.clientEmail }}</td>
            <td>
              <button class="solid">Edit</button>
              <button class="danger">Delete</button>
            </td>
          </tr>
        </tbody>
    
      </table>

component.ts

dtOptions: DataTables.Settings = {};
  clients: any = [];
  dtTrigger: Subject<any> = new Subject<any>(); 

  constructor(
    public webService: WebService,
    private renderer: Renderer2,
    private router: Router,
    private httpClient: HttpClient
  ) { }

  ngOnInit(): void {
    this.dtOptions = {
      pagingType: 'full_numbers',
      pageLength: 2
    };
    this.webService.getClients()
      .subscribe(data => {
        this.clients = data;
        console.log(this.clients)
        // Calling the DT trigger to manually render the table
        this.dtTrigger.next;
      });
  }


  ngOnDestroy(): void {
    // Do not forget to unsubscribe the event
    this.dtTrigger.unsubscribe();
  }

example data used example data used

5
  • The example data you are showing contains empty strings on the fields you are showing. Is that the case for all the 'clients'? Commented Apr 11, 2022 at 12:33
  • @Loxx - I'm retrieving 3 fields from the data (clientName, clientCompany & clientEmail). I changed the json data to show empty strings as it contains sensitive data Commented Apr 11, 2022 at 12:58
  • Are u getting errors in the console? Commented Apr 11, 2022 at 13:09
  • @Loxx - no none at all, its pulling the data through okay but when you try to search or sort it just comes up with no data available in table Commented Apr 11, 2022 at 14:02
  • this.dtTrigger.next; - should be called with this.dtTrigger.next() Commented Apr 12, 2022 at 9:25

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.