angular-datatables icon indicating copy to clipboard operation
angular-datatables copied to clipboard

Server Side rerendering issues resolved :)

Open awais2080 opened this issue 7 years ago • 0 comments

I'm submitting a...

hello all, i am writing a complete guide that how can you solve the serverside rerendring issues even having all export buttons and even search which i found angular 7 doc does not have that properly.


[a bug is fixed related to angular ] Bug report  
[angular 7 datattable ] Feature request
[ ] Documentation issue or request
[this is tested with latest angular 7 datatable for any serverside rerendering issue you can solve it here. ] Question

Current behavior

Expected behavior

Minimal reproduction of the problem with instructions

Environment


- node version:
- angular version:
- angular-cli version:
- jquery version:
- datatables version:
- angular-datatables version:

Your html should have code like this here you will find #dataTable which is basically working as id like jquery datatable but with the angular base

now in your ts file you need to declare #dataTable // before constructor starts @ViewChild('dataTable') table; // declare this before your constructor api_check: any; // this will be used to unsubscribe previous call when we are searching it will be useful there // because api response take time so api call with aw will have more records and will take time // where as awais will have less time so it will overcome this.

@ViewChild(DataTableDirective) dtElement: DataTableDirective; dtOptions: DataTables.Settings = {};

// before constructor ends

now this will be your ngOnInit

ngOnInit(): void { const header = new HttpHeaders().set('Authorization', 'Bearer ' + this.auth.getToken()); const that = this; this.dtOptions = { dom: 'lBfrtip', pagingType: 'full_numbers', pageLength: 10, processing: true, serverSide: true, ajax: (dataTablesParameters: any, callback) => { that.api_check ? that.api_check.unsubscribe() : that.api_check = null; that.api_check = that.http .post( 'your url ', dataTablesParameters, { headers: header } ).subscribe(resp => { callback({ recordsTotal: resp['data']['original'].recordsTotal, recordsFiltered: resp['data']['original'].recordsFiltered, // helps you in pagination data: resp['data']['original']['data'] }); }); }, rowCallback: (row: Node, data: any[] | Object, index: number) => { const self = this;

      // custom column of row click event start
        // Unbind first in order to avoid any duplicate handler
        // (see https://github.com/l-lin/angular-datatables/issues/87)
        // $('td:last-child', row).unbind('click');
        // $('td:last-child', row).bind('click', () => {
        //     self.nextButtonClickEvent(data);
        // });
        
        // $('td:first-child', row).unbind('click');
        // $('td:first-child', row).bind('click', () => {
        //     self.nextButtonClickEvent(data);
        // });
        // custom column of row click event ends

       // below code will give you row click event data
        $('tr', row).unbind('click');
        $('td', row).bind('click', () => {
            console.log(data);
        });
        return row;
    },
     columns: [
        {
           title: 'Member#',
           data: 'member_id',
           defaultContent: '',
           orderable: true,
           searchable: true
        },
        {
           title: 'Name',
           data: 'first_name',
           render: function (data, type, row) {
           return row.first_name + ' ' + row.last_name; },
           //data: 'first_name',
           defaultContent: '',
           orderable: true,
           searchable: true
        },
        {
           title: 'Contact#',
           data: 'contact_no',
           defaultContent: '',
           orderable: true,
           searchable: true
        },
        {
           title: 'Birth Date',
           data: 'date_of_birth',
           defaultContent: '',
           orderable: true,
           searchable: true
        },
        {
           title: 'Age',
           data: 'age',
           defaultContent: '',
           orderable: true,
           searchable: false
        },
     ],
     language: {
       'processing':
         ' <divclass="my-preloader preloader my-modal"><svg class="circular" viewBox="25 25 50 50"><circle class="path" cx="50" cy="50" r="20" fill="none" stroke-width="2" stroke-miterlimit="10" /></svg></div>',
     }
  };

// this is the main part that will help you in rerendering :)

  that.dataTable = $(that.table.nativeElement);
  that.dataTable.DataTable(that.dtOptions);

}

thats it enjoy the coding! one note rest datatable integration is like @l-lin documentation this will help you solve serverside rerendering

awais2080 avatar May 19 '19 23:05 awais2080