0

I was implementing row-click-event into my datatables. Using tutorial and example from (http://l-lin.github.io/angular-datatables/#/advanced/row-click-event)

I made a mock table by hard coding a table into HTML to run the example. The problem is that the rowCallback function doesn't seem to work at all when I load into the webpage. From what I can tell the basic datatable functionality works, sorting, filtering searching etc. But I cannot get the click even to get bind to each of the records. I have been trying to figure this problem out for couple of days. Can anyone tell what might be wrong with my code?

<div class="col-md-12">
    <p class="homeText">{{homeText}}</p>
</div>
<blockquote>Please click on a row</blockquote>
<p class="text-danger">You clicked on: <strong>{{ message }}</strong></p>
<br />
<table datatable class="row-border hover">
    <thead>
      <tr>
        <th>ID</th>
        <th>First name</th>
        <th>Last name</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td>1</td>
        <td>Foo</td>
        <td>Bar</td>
      </tr>
      <tr>
        <td>2</td>
        <td>Someone</td>
        <td>Youknow</td>
      </tr>
    </tbody>
  </table>
import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'app-home',
  templateUrl: './home.component.html',
  styleUrls: ['./home.component.css']
})
export class HomeComponent implements OnInit {
  message = 'placeholder';
  public homeText: string;
  dtOptions: DataTables.Settings = {};

  constructor() { }

  someClickHandler(info: any): void {
    this.message = info.id + ' - ' + info.firstName;
  }

  ngOnInit() {
    this.homeText = "Welcome home";
    this.dtOptions = {
      ajax: 'data/data.json',
      columns: [{
        title: 'ID',
        data: 'id'
      }, {
        title: 'First name',
        data: 'firstName'
      }, {
        title: 'Last name',
        data: 'lastName'
      }],
      rowCallback: (row: Node, data: any[] | Object, index: number) => {
        const self = this;
        // Unbind first in order to avoid any duplicate handler
        // (see https://github.com/l-lin/angular-datatables/issues/87)
        $('td', row).unbind('click');
        $('td', row).bind('click', () => {
          self.someClickHandler(data);
        });
        return row;
      }
    };
  }
}
3
  • The documentation says that you should add [dtOptions]="dtOptions" to the table. Commented Jun 27, 2019 at 20:00
  • You are right. I am seeing if that solves the problem but now I am running into another error. "DataTables warning: table id=DataTables_Table_0 - Ajax error. For more information about this error, please see datatables.net/tn/7" Commented Jun 27, 2019 at 20:08
  • 1
    Thank you sabithpocker. That did fix the issue. Thank you for you help. Now I need to figure out why this does not work with my Ajax calls. Commented Jun 27, 2019 at 20:14

1 Answer 1

0

Thanks to what sabithpocker pointed out. I was missing js [dtOptions]="dtOptions" and also I included js ajax: 'data/data.json', which was causing this error "DataTables warning: table id={id} - Ajax error" https://datatables.net/manual/tech-notes/7

Sign up to request clarification or add additional context in comments.

Comments

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.