4

I have an angular application where I have a Datatable. In the datatable, I change cell content to contain html content.

In the code below, I change the cell content of the 5th column to have links. I wish to have a tooltip that informs the user about the link. I am using UI Bootstrap.

var app = angular.module('smsmanagement', ['ngRoute', 'ui.bootstrap']);
app.controller('RecipientsController', function ($scope, $http, $routeParams, $timeout, SweetAlert) {

var groupID = $routeParams.param;

    $('#table-recipients-view').DataTable({
        sDom: "<'row'<'col-sm-6'l><'col-sm-6'f>r>t<'row'<'col-sm-6'i><'col-sm-6'p>>",
        "processing": true,
        "serverSide": true,
        "ajax": {
            "url": "recipients/dt",
            "data": function (d) {
                d.groupID = groupID; 
            }
         },
        "fnRowCallback": function (nRow, aData, iDisplayIndex, iDisplayIndexFull) {
              var groupLink = '';
              // The number of columns to display in the datatable
              var cols = 6;

              var rowElementID = aData[(cols - 1)];

              groupLink = '<a href="#smsgroups/' + rowElementID + '" uib-tooltip="View group(s)">' + noOfGroups + '</a>';

            // Create a link in no of groups column
              $(nRow).children('td:eq(' + (cols - 2) + ')').html(groupLink);

          }
      });
  });

I have added the uib-tooltip directive in groupLink html content.

The problem is the tooltip does not appear. I've heard of using $compile but I am not quite sure on how to use it.

1 Answer 1

4

You are right about using compile.

Try changing the following line:

$(nRow).children('td:eq(' + (cols - 2) + ')').html(groupLink);

To this:

$(nRow).children('td:eq(' + (cols - 2) + ')').html($compile(groupLink)($scope));

Also do not forget to inject $compile service to your controller.

Edit

This is definitely not the cleanest (or most AngularJS) way to solve the problem, but probably the easiest given your current setup. The best approach would probably be to create a new directive to deal with your data tables, but that is out of the scope of this question.

Edit 2

The general idea is to create directive to decouple DOM manipulation from your Controllers/Services.

Have a look at the official AngularJS documentation (https://docs.angularjs.org/guide/directive) to see how to create a directive. Your aim is probably something which would get the data in the controller via a service and pass that into your directive to deal with the DOM manipulation. For example like this:

<my-data-table data="dataVariableInScope"></my-data-table>
Sign up to request clarification or add additional context in comments.

3 Comments

No problem, glad I could help!
About your side note, maybe you can point me in the right direction on the cleaner (or more AngularJS) way of working with datatables.
I updated the answer with a general gist of what I meant by "cleaner" way.

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.