0

I am using datatable at my Angular application. I have lots of data and using server-side processing. When I add rows to table, I need to add a button to last row which has click event. Here is my html :

<div class="card m-t p-3">
    <div class="card-body">
        <table id="datatables"  class="table responsive table-bordered table-striped table-hover" datatable [dtOptions]="dtOptions" [dtTrigger]="dtTrigger">
            <thead>
                <tr>
                    <th>Id</th>
                    <th>Quote</th>
                    <th>Author</th>
                    <th>Genre</th>
                    <th>Update</th>
                  </tr>
            </thead>
            
        </table>
    </div>
</div>

Here is my component.ts code (part of it )

 ngOnInit(): void {

this.key = this.keyManager.getKey();

if(this.key == ""){
  alertify.alert("LOGIN REQUIRED","Please go back to the login screen.",()=>{
    this.router.navigateByUrl("/login");
  })
}else{
  this.getAllQuotes();
}

this.dtOptions = {
  dom: 'B <"top"i>rt<"bottom"flp><"clear">',
  paging : true,
  responsive :true,
  processing: true,
  serverSide: true,
  ajax: {
    "url": this.baseUrl+"/api/Quote/paging?apiKey="+this.key,
    "type": "POST",
    "dataType": "json"
  },
  "columnDefs": [{
    "targets": [0],
    "visible": false,
    "searchable" : false
  }],
  "columns": [
    { "data": "id" },
    { "data": "text"},
    { "data": "author"},
    { "data": "genre"},
    {
      "data":null, "render":function(data,row){
        return `<button (click) = updateClicked2() class="btn btn-warning deleteBtn">Update</button>`;
      }
    }
    
  ]
};

And simple updateClicked2() method :

updateClicked2(){
console.log("clicked");

}

Here is the result on chrome: enter image description here

It is not working. It should not be like that because one of my working buttons looks like: enter image description here

So, I have to use my method from component.ts script because that update button will use router and change my app's route to another page. Please help me. Thank you.

2
  • 1
    I imagine you're using a "based JQuery" library. So you're in problems. You has no a function called "updateClicked2" (Angular rename your functions during transpile typeScript to javascript). My suggest it's use simple Angular (and run away all the librarys that use jQuery) Commented Mar 22, 2022 at 7:35
  • Yep, like @Eliseo said, try to use an Angular compatible library for your datatable. You'll find some good stuff at PrimeNg primefaces.org/primeng/#/table Commented Mar 22, 2022 at 9:26

1 Answer 1

1

I solved the problem like this (not adding buttons but make row clickable)

this.dtOptions = {
  rowCallback: (row:Node, data:Quote[] | object, index:number)=>{
  row.childNodes[0].textContent = String(index + 1);      
  const self = this;
  $('td' ,row).off('click');
  $('td',row).on('click',()=>{
    this.updateClicked(data as Quote);
  });
  return row;
  }
}
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.