如何在 ng-table 中绑定 title 属性值
How to bind title attribute value in ng-table
我正在使用 ng-table 在 table 视图中显示所有值。要在备注栏(table 的最后一栏)中显示的消息很大。所以,我表现得很少。当用户将鼠标悬停在单元格上时,我想在 tool-tip 中显示整个消息。我尝试在标题属性中设置它,但它不起作用。
示例代码: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>
请建议我如何使用 HTML title 属性显示 tool-tip。
html 有一个元素:<abbr>
<abbr title="{{doc.remarks}}">
{{doc.remarks | limitTo: 15 }} {{doc.remarks.length > 15 ? '...' : ''}}
</abbr>
当鼠标移到''上时可以显示'title'的内容
如果你需要它,你可以使用它。
解决方案一:
使用插件 ['ui-bootstrap'].
您可以使用以下代码:
在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>
并且在脚本文件中:
// 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);
};
这里是工作 plunker linkClick Here
解决方案 2:(无依赖注入)
使用指令:
HTML 部分:
<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>
脚本部分:
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 用于此解决方案的 Plunker Plunker
我已经在你的代码中应用了它。请看这个plunker
根据您的要求,使用 ng-attr-title 找到 plunker
的 link
我认为您可以为此使用 ng-attr-title,所以基本上您的代码保持不变,但就在 'title=' 之前添加 'ng-attr-'。所以你的最后一行喜欢:<td data-title="'Remarks'" sortable="'remarks'" ng-attr-title="{{doc.remarks}}">
我之前没有在 table 单元格上测试过,但理论上它应该可以解决问题:-)
更新
查看此工作插件:http://plnkr.co/edit/WHm04jGoiE3oZi244fqj?p=preview
如您所见,我将 ng-table.js 设为本地,然后在 index.html 中,我将 ng-attr-title 放在 ng-table 属性的前面。
我正在使用 ng-table 在 table 视图中显示所有值。要在备注栏(table 的最后一栏)中显示的消息很大。所以,我表现得很少。当用户将鼠标悬停在单元格上时,我想在 tool-tip 中显示整个消息。我尝试在标题属性中设置它,但它不起作用。
示例代码: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>
请建议我如何使用 HTML title 属性显示 tool-tip。
html 有一个元素:<abbr>
<abbr title="{{doc.remarks}}">
{{doc.remarks | limitTo: 15 }} {{doc.remarks.length > 15 ? '...' : ''}}
</abbr>
当鼠标移到''上时可以显示'title'的内容 如果你需要它,你可以使用它。
解决方案一: 使用插件 ['ui-bootstrap'].
您可以使用以下代码:
在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>
并且在脚本文件中:
// 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);
};
这里是工作 plunker linkClick Here
解决方案 2:(无依赖注入)
使用指令:
HTML 部分:
<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>
脚本部分:
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 用于此解决方案的 Plunker Plunker
我已经在你的代码中应用了它。请看这个plunker
根据您的要求,使用 ng-attr-title 找到 plunker
的 link我认为您可以为此使用 ng-attr-title,所以基本上您的代码保持不变,但就在 'title=' 之前添加 'ng-attr-'。所以你的最后一行喜欢:<td data-title="'Remarks'" sortable="'remarks'" ng-attr-title="{{doc.remarks}}">
我之前没有在 table 单元格上测试过,但理论上它应该可以解决问题:-)
更新
查看此工作插件:http://plnkr.co/edit/WHm04jGoiE3oZi244fqj?p=preview
如您所见,我将 ng-table.js 设为本地,然后在 index.html 中,我将 ng-attr-title 放在 ng-table 属性的前面。