How to bind title attribute value in ng-table
Asked Answered
M

3

8

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.

Makeshift answered 1/6, 2015 at 7:43 Comment(1)
and what do you use for your tooltips ?Nadabas
D
10

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.

Dorwin answered 1/6, 2015 at 8:44 Comment(11)
@Deepakraj Could you add a fiddle of your code? Because I just tested ng-attr-title in my custom ng-table and it works fine :oDorwin
You do see actual table data in your table right? Since the plunkr gets an error on ng-table..Dorwin
In your plunker only {{item.title}} etc is visible because ng-table isnt injected properly, I assume this works properly in your actual app?Dorwin
no it's not working in my app. In my app it's also shows {{item.desc}} like this.that is the problem.Makeshift
Yeah, as I expected. Your ng-table seems to be injected inproperly. Instead of loading the file from an URL, try to add the file to your project locally. So instead of <script data-require="ng-table@*" data-semver="0.3.0" src="http://bazalt-cms.com/assets/ng-table/0.3.0/ng-table.js"></script> it becomes something like <script src="/Scripts/libs/ng-table.js"></script> (insert your own path to your file ofcourse)Dorwin
Guinn @ I think that is not a problem. the prb is title attr not binding the value. if you hover the description column it's show the value {{item.desc}}Makeshift
it is the problem :) if you hit F12 and go to the control pane, you will see an error in your $injector. If NG-table would be working properly, you would see data instead of {{item.desc}}, but because you use the regular non-angular title attribute, it shows tooltip with what is between quotes.Dorwin
basicly if angular gets an error it 'breaks' the script, hence why you see {{item.~}} instead of actual data.Dorwin
@Deepakraj check my updated answer, I added a plnkr that works. note: on your plnkr above you import ng-table from another URL than in the plnkr in your question. Thisone does indeed work, so you could use that instead of getting ng-table local as in my plnkr.Dorwin
@Deepakraj No offense to Nitin Singh but if you used my solution with the ng-attr-title, why make his answer the accepted answer?Dorwin
Guinn @ Both answer is a right answer that's why I gave one up to each one. Thanks your valuable time. it's not offense.Makeshift
T
1

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.

Twospot answered 1/6, 2015 at 7:58 Comment(2)
Why did you put your entire answer in a codeblock? It isnt very readable like this.Dorwin
simple+handy with the addition of css: width: 69px; text-overflow: ellipsis; overflow: hidden; display: block; white-space: nowrap;Maintenon
R
1

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">
     <link rel="stylesheet" href="style.css">
    <script src="https://code.angularjs.org/1.2.9/angular.min.js"></script>
    <script src="http://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">
 <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
      <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

Rase answered 1/6, 2015 at 8:11 Comment(7)
It's possible to use with out any external plugin ?Makeshift
Hi Deepak, I have updated the solution. Soulution 2 is as per your requirement.Rase
I tried in the solution 2 . but it's not working for ng table.Makeshift
Hi Dipak, I have applied it to in your code and it is working. Please see this plunker plnkr.co/edit/tdRVY7B59WsbNxmiEz7Y?p=previewRase
While this is a pretty neat way to use a directive, isn't it a bit overkill for this example? Why not simply use ng-attr-title?Dorwin
ng-attr-title is also working. PLease check here plnkr.co/edit/tdRVY7B59WsbNxmiEz7Y?p=previewRase
Hi deepak please vote up the answer if it working :)Rase

© 2022 - 2024 — McMap. All rights reserved.