3

So, I cannot seem to find an answer anywhere about this.

I was trying to use this the "Server way" which works great, until you find out the limitations. Like the "select" plugin does not work, or you cant use "expanding" tables

So, I need to go back to using Datatable natively.

Let's say I define my table like this

this.dtOptions = {
  ajax: (dataTablesParameters: any, callback: any) => {
    this.myService.myEndPoint(dataTablesParameters).
    subscribe((resp: any) => {
      this.attributes = resp.aaData;

      callback({
        recordsTotal: resp.iTotalRecords,
        recordsFiltered: resp.iTotalDisplayRecords,
        data: resp.aaData
      });
    });
  },
  select: true,
  order: [2, "asc"],
  columns: [{
      data: null,
      defaultContent: 'details',
      orderable: false,
      class: 'details-control'
    },
    {
      data: null,
      orderable: false,
    },
    {
      data: "skuPartNumber",
      orderable: true,
    },
    {
      data: "activeUnits",
      orderable: true
    },
    //{ data: "consumedUnits", orderable: true }
    {
      title: "Display Name",
      data: null,
      orderable: true,
      render: (data, type, full) => `<button (click)="testClick(data.id)">sdfsdf</button>`
    }
  ]
};

How can I listen for the button event? My assumption is the page is rendered (compiled) before the AJAX and data table completes?

I was able to do this in knockoutJS and durandal, but cannot figure this out in Angular

3
  • Could you elaborate more ? Commented Oct 13, 2018 at 22:32
  • I am adding the HTML(with a button and a click event) AFTER Angular has processes the page. How do I get the event to fire? Must be a way, I just dont know how to do it Commented Oct 14, 2018 at 13:33
  • 1
    I should have been more clear.. I need to re-bind (not sure the terminology) Angular. I need to be inside the “Angular” context, so I can pass data into the function from the row.. Like an id, or something else. Commented Oct 14, 2018 at 23:56

2 Answers 2

1

If you are using server-side rendering you can define your click events on drawCallback (docs)

this.dtOptions = {
    ...
    "drawCallback": ()=>{
        //define your listeners here
    }
    ...
Sign up to request clarification or add additional context in comments.

2 Comments

What exactly does "define your listeners here" look like? Thats what I dont know.
define event listeners for your button. something like document.getElementById('idOfButton').addEventListener('click',testClick(5))
1

You can capture event in drawCallback.

let table = $('#example').DataTable({
      drawCallback: () => {
        $('.buttonClass').on('click', () => {  //click event on button with class `buttonClass`
            this.nextButtonClickEvent();
          });
      }
    });

Refer this demo which has table pagination https://stackblitz.com/edit/angular-datatables-pagination

1 Comment

I should have been more clear.. I need to re-bind (not sure the terminology) Angular. I need to be inside the “Angular” context, so I can pass data into the function from the row.. Like an id, or something else.

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.