1

I need to have a button at the end of each row in my datatable and get the row values when I click on it. Please see my code below.

In my .ts

ngOnInit() {
    this.getUsers();
  }

  getUsers() {
    this.us.getUsers()
      .subscribe((data: any) => {
      this.users = data;

      var table = $('#datatables').DataTable({
        "pagingType": "full_numbers",
        "lengthMenu": [[10, 25, 50, -1], [10, 25, 50, "All"]],
        responsive: true,
        data:this.users,
        columns:[
          {
            data:"Id"
          },
          {
            data:"FirstName"
          },
          {
            data:"MiddleName"
          },
          {
            data:"LastName"
          },
          {
            data:"UserName"
          },
          {
            data:"Email"
          },
          {
            data:"DateAdded"
          },
          {
            data:"IsActivated"
          }
        ],
        language: {
          search: "_INPUT_",
          searchPlaceholder: "Search records",
        }
      });

    })
  }

In my ,html

<table id="datatables" class="table table-striped table-bordered table-hover" cellspacing="0"
    width="100%" style="width:100%">
    <thead>
        <tr>
            <th>Id</th>
            <th>FirstName</th>
            <th>MiddleName</th>
            <th>LastName</th>
            <th>UserName</th>
            <th>Email </th>
            <th>DateAdded</th>
            <th>Activated</th>

        </tr>
    </thead>

</table>

Can you please help me how to this right. I can't use the *ngFor loop because it causes issues int the datatables. Thank you.

1
  • Thanks to Nabil Shahid for the cool solution. If anyone has the problem that the buttons do not work on the second page or when the table is searched then you have to add a listener on the table draw event. See here https://stackblitz.com/edit/angular-hxdbgi-kw44dp Commented Sep 21, 2019 at 12:54

1 Answer 1

4

You can do it using defaultContent key in columns object of datatable. First you need to create a <th> for showing the button column. I have created a stackblitz for achieving this. Please read the comments i have added in that stackblitz carefully before implementing it in your logic. I have added three buttons in it which show the data of current row. See: Buttons In Datatable

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

3 Comments

Thank you for this WONDERFUL SOLUTION. I just have one last question though, if you will indulge me, instead of just the Id, I need to get all the data in a row in a click of one button. Thank you SO MUCH.
All data in a row is returned by row.data() function. Instead of passing row.data().FirstName in a function, you can pass it directly to access whole object. See this stackblitz.com/edit/…. It logs all data of a row on console in showValue function on button click.
No problem. Happy to help

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.