使用clipboard.js复制表格行

3
我有一个bootstrap表格,其中我使用Angular的指令ng-repeat动态显示数据库搜索结果,结果中包括一个电子邮件字段。我想在每个电子邮件单元格右侧显示一个按钮,以将该单元格的电子邮件复制到剪贴板中。
但是这个表格没有唯一的ID字段,我不知道如何为每行的电子邮件字段分配不同的ID,以便clipboard.js的"data-clipboard-target"知道它必须复制同一行的电子邮件。现在,每个按钮都独立地复制第一行的电子邮件,可能是因为它查找了“#emailField”标签的第一次出现。
您有什么想法吗?
以下是我的HTML文件:
<html>
<head>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"></script>
</head>
<body ng-app="myApp">
    <div class="container" ng-controller="AppCtrl">
        <br>
        <input  type="text" class="form-control" ng-model="query" placeholder="Chercher ici">
        <br>
        <br>
        <h4>Results:</h4>
        <table class="table table-striped table-responsive table-condensed table-bordered">
            <thead>
                <tr>
                    <th>Client</th>
                    <th>Contact</th>
                    <th>Email</th>
                    <th>Telephone</th>
                    <th>Mobile</th>
                </tr>
            </thead>
            <tbody>
                <tr ng-repeat="x in queryResult">
                    <td>{{ x.client }}</td>
                    <td>{{ x.contact }}</td>
                    <td>
                        <b id="emailField">{{ x.email }}</b>
                        <button type="button" class="btn btn-default pull-right" data-clipboard-target="#emailField">
                            <span class="glyphicon glyphicon-copy"></span>
                        </button>
                    </td>
                    <td>{{ x.telephone }}</td>
                    <td>{{ x.mobile }}</td>
                </tr>
            </tbody>
        </table>
        <h4>Query status:</h4>
        <pre class="ng-binding" ng-bind="queryStatus"></pre>
    </div>

    <!-- Scripts-->
    <script src="./bower_components/clipboard/dist/clipboard.min.js"></script>
    <script>
        new Clipboard('.btn');
    </script>
    <script src="./bower_components/angular/angular.js"></script>
    <script src="./bower_components/angular-bootstrap/ui-bootstrap-tpls.js"></script>
    <script src="./bower_components/angular-ui-router/release/angular-ui-router.js"></script>
    <script src="./controller.js"></script>
</body>

1个回答

3
尝试更改此标记的部分:
```html

尝试更改此标记的部分:

```
<td>
    <b id="emailField">{{ x.email }}</b>
    <button type="button" class="btn btn-default pull-right" data-clipboard-target="#emailField">
        <span class="glyphicon glyphicon-copy"></span>
    </button>
</td>

转换为:

<td>
    <b id="emailField_{{$index}}">{{ x.email }}</b>
    <button type="button" class="btn btn-default pull-right" data-clipboard-target="#emailField_{{$index}}">
        <span class="glyphicon glyphicon-copy"></span>
    </button>
</td>

您可以在ng-repeat内使用可用的$index来为每个电子邮件元素创建唯一的ID。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接