How to bind title attribute value in ng-table

All we need is an easy explanation of the problem, so here it is.

I am using ng-table to display all values in a table view. Message to be displayed in remarks column (last column of the table) is huge. So, I am displaying few character. When user hovers over the cells I want to show the entire message in a tool-tip. I tried to set it in title attribute, but it’s not working.

Sample code : http://plnkr.co/edit/I4nCTNhMfxgbBX7Zjxs9?p=preview

<table ng-table="tableParams" class="table">
        <tr ng-repeat="doc in $data">
        <td data-title="'#'" sortable="doc_name">{{$index + 1 }}</td>
        <td data-title="'Visa Type'" sortable="'type'">{{doc.type}}</td>
        <td data-title="'Country'" sortable="'country'">{{doc.country}}</td>
        <td data-title="'Starting Date'" sortable="'start_date'">{{doc.start_date}}</td>
        <td data-title="'Expired Date'" sortable="'end_date'">{{doc.end_date}}</td>
        <td data-title="'Remarks'" sortable="'remarks'" title="{{doc.remarks}}">
        {{doc.remarks | limitTo: 15 }} {{doc.remarks.length > 15 ? '...' : ''}}
        </td>
        </tr>
</table>

Please suggest me how to show tool-tip using HTML title attribute.

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

I think you can use the ng-attr-title for that, so basicly your code remains the same, but just before ‘title=’ you add ‘ng-attr-‘. So your last line would like like: <td data-title="'Remarks'" sortable="'remarks'" ng-attr-title="{{doc.remarks}}">

I haven’t tested this on table cells before, but theoretically it should do the trick 🙂

UPDATE

See this working plunkr: http://plnkr.co/edit/WHm04jGoiE3oZi244fqj?p=preview

As you can see I made ng-table.js local, and then in index.html I put the ng-attr-title in front of the ng-table attributes.

Method 2

Solution 1:
Using plugin [‘ui-bootstrap’].

You can use below code:

In HTML:

<head>
    <link rel="stylesheet" href="http://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="nofollow noreferrer noopener">
     <link rel="stylesheet" href="style.css" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">
    <script src="https://code.angularjs.org/1.2.9/angular.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/angular-ui-bootstrap/0.10.0/ui-bootstrap-tpls.min.js"></script>

    <script src="script.js"></script>
  </head>

<body ng-app="myApp">
    <table class="table" ng-controller="ctrl">
        <thead>
            <tr><th>column</th></tr>
        </thead>
        <tbody>
            <tr>
                <td>
                    <span tooltip="that">this</span>
                </td>
            </tr>
            <tr ng-repeat="foo in bar">
                <td><span tooltip="{{foo.tooltip}}">{{foo.content}}</span></td>
            </tr>
        </tbody>
    </table>
</body>

And in script file:

// Code goes here

var app = angular.module('myApp', ['ui.bootstrap']);

app.controller('ctrl', ['$scope', function ($scope) {
    $scope.bar = [];
    // doing something async (exec time simulated by setTimeout)
    myAsyncFunc(function (bar) {

        $scope.$apply(function() {
             $scope.bar = bar;
        });
    });

}]);

var myAsyncFunc = function (done) {
    // simulate some kind of timeout due to processing of the function
    setTimeout(function () {
        return done([{tooltip: 'this is the tooltip', content: 'this is the content'}]);
    }, 500);
};

here is the link for working plunker Click Here

Solution 2: (Without dependency injection)

Using Directive:

HTML PART:

 <link rel="stylesheet" href="style.css" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css" rel="nofollow noreferrer noopener">
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://code.angularjs.org/1.2.9/angular.min.js"></script>
<!-- Latest compiled and minified JavaScript -->
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
<!-- Latest compiled and minified CSS -->
<script src="script.js"></script>

 <div ng-app="myApp">
    <div ng-controller="MyCtrl">
        <li ng-repeat="item in items" >
            <a rel="tooltip" tooltip="item.tooltip">{{item.name}}</a>
        </li>
    </div>
</div>

Script part:

var myApp = angular.module("myApp", []);

function MyCtrl($scope) {
    $scope.items = [{ name: "item 01", tooltip: "This is item 01 tooltip!"},
                    { name: "item 02", tooltip: "This is item 02 tooltip!"},
                    { name: "item 03", tooltip: "This is item 03 tooltip!"},
                    { name: "item 04", tooltip: "This is item 04 tooltip!"},
                    { name: "item 05", tooltip: "This is item 05 tooltip!"} ];
    console.log("MyCtrl");
}

    myApp.directive('tooltip', function () {
        return {
            restrict:'A',
            link: function(scope, element, attrs)
            {
                $(element)
                    .attr('title',scope.$eval(attrs.tooltip))
                    .tooltip({placement: "right"});
            }
        }
    })

Link for Plunker for this solution Plunker

I have applied it on your code. Please see this plunker

As per your requirement, using ng-attr-title find the link of plunker

Method 3

html has a element: <abbr>

<abbr title="{{doc.remarks}}">
{{doc.remarks | limitTo: 15 }} {{doc.remarks.length > 15 ? '...' : ''}}
</abbr>

It can display the ‘title’ of content when the mouse is moved on ”
if you need it,you can use it.

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply