如何在ng-table中绑定title属性值



我使用ng-table在表视图中显示所有值。要显示在备注列(表的最后一列)中的消息非常大。所以,我显示了几个字符。当用户将鼠标悬停在单元格上时,我希望在工具提示中显示整个消息。我试图在标题属性中设置它,但它不起作用。

示例代码: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标题属性显示工具提示。

我认为你可以使用ng-attr-title,所以基本上你的代码保持不变,但在'title='之前你添加'ng-attr-'。最后一行是这样的:<td data-title="'Remarks'" sortable="'remarks'" ng-attr-title="{{doc.remarks}}">

我还没有测试这个表单元格之前,但理论上它应该做的伎俩:-)

查看这个工作柱kr: http://plnkr.co/edit/WHm04jGoiE3oZi244fqj?p=preview

您可以看到,我将ng-table.js设置为本地,然后在index.html中,我将ng- attri -title放在ng-table属性的前面。

方案一:使用插件['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);
};

这里是工作柱塞的链接点击这里

解决方案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"});
            }
        }
    })

此解决方案的Plunker链接

我已经在你的代码中应用了它。请看这个活塞

根据您的要求,使用ng- attri -title找到柱塞的链接

html有一个元素:<abbr>

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

可以在鼠标移动时显示内容的"title"如果你需要,你可以使用它

最新更新