Perform a reload to a datatables

0

Good afternoon, I need to know how I can apply a reload to a datatable in typescript with Angular 2,

options = {
  dom: "Bfrtip",
  ajax: (data, callback, settings) => {
    this.servicioUsuarios.getUsuarios().subscribe(data => {
      this.usuarioArray = data
      console.log(this.usuarioArray);
      callback({
        aaData: data
      })
    }, e => {
      this.notificaciones.notificacionError(e);
    });
  },
  columns: [
    { data: "id" },
    { data: "login" },
    {
      "render":
      function (data, type, row) {
        return (row.nombre + ' ' + row.primerApellido + ' ' + row.segundoApellido);
      }
    },
    { data: "correo" },
    { data: "fechaAlta" },
  ],
  select: true,
  rowCallback: (row: Node, data: any[] | Object, index: number) => {
    $('td', row).unbind('click');
    $('td', row).bind('click', () => {
      this.usuarioArray = data;
      // $('td', row).addClass('alert alert-success');
    });
    return row;
  }
};
    
asked by Gonzalo Alberto 20.06.2017 в 01:31
source

1 answer

1

Here is an example of how I did it.

You should use the ajax option in this way:

ajax  : {
    url : 'api/endpoint/data', // URL de tu endpoint
    dataSrc : ''
},

You must obtain a reference to the table in this way:

@ViewChild(DataTableDirective) private dtElement;

Or if you have several tables:

@ViewChildren(DataTableDirective) private dtElements;

Once having the reference simply use the ajax.reload() method:

private async reloadTable(){
    let dtIntance = await this.dtElement.dtInstance;
    dtInstance.ajax.reload()
}
    
answered by 20.06.2017 в 16:34