带有 Angular 数据表的 Jquery onclick 事件

标签 jquery angularjs datatables

每当单击数据表的列时,我都必须打开一个新链接。这是我的数据表代码,它通过 ajax 调用获取数据-

<table datatable="" dt-options="dtOptions" dt-columns="dtColumns" class="featuretable row-border compact hover" >
   <thead >
      <!--change style of column with css-->
      <tr>
         <th class="Header">Feature</th>
         <th class="Header">ID</th>
         <th class="Header">Log</th>
         <th class="Header">Location</th>
      </tr>
   </thead>
</table>

这是我正在尝试的 Jquery 代码 -

$('.featuretable tbody').on('click', 'tr', function () {
    console.log(table.row(this).data());
    var data = table.row(this).data();
    alert('You clicked on ' + data[1] + '\'s row');
});

CSS 文件读取 -

.Header {
    border-bottom: 2px solid #6f7277;
    padding: 3px 15px;
    text-align: left;
    color: #4b4a4a;
    overflow: hidden;
}

table.featuretable {
    table-layout: fixed;
    width: 100%;
    font-family: Helvetica, Arial, sans-serif;
    margin-top: 20px;
    margin-bottom: 20px;
    border-collapse: collapse;
    border-spacing: 0;
}

table.featuretable td,
th {
    border: 1px solid transparent;
    height: 30px;
    transition: all 0.3s;
    overflow: hidden;
}

table.featuretable th {
    font-weight: bold;
    text-align: center;
    vertical-align: :middle;
}

table.featuretable td {
    text-align: center;
    vertical-align: :middle;
}

table.featuretable tr:nth-child(even) td {
    background: #f3f7fb;
}

table.featuretable tr:nth-child(odd) td {
    background: #ffffff;
}

点击时,chrome 给出错误“ReferenceError:表未定义”。有人知道怎么做吗 ?

最佳答案

由于您使用的是 Angular dataTables 指令,因此您应该定义一个 dtInstance 并对其进行处理:

$scope.dtInstance = {}
<table datatable="" dt-instance="dtInstance" dt-options="dtOptions" dt-columns="dtColumns" class="featuretable row-border compact hover" >

然后在 jQuery 事件处理程序中,使用 dtInstance 作为表引用

$('.featuretable tbody').on('click', 'tr', function () {
   var data = $scope.dtInstance.DataTable.row( this ).data();
   alert( 'You clicked on '+data[1]+'\'s row' );
} );

关于带有 Angular 数据表的 Jquery onclick 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39077102/

相关文章:

php - 在 DataTables 服务器端处理脚本中运行 MySQL 查询

javascript - 旋转时触发不透明度,jquery

angularjs - 更新范围时,NG-Repeat 不会干净地更新

javascript - 是否可以在标准 JavaScript 提示/确认弹出窗口中删除键盘访问?

javascript - 具有多个 Controller 的 Angular 共享模型

javascript - 工厂函数的依赖注入(inject)(AngularJS)

spring - 如何更改 datatable.net 中的参数名称,例如 : "draw", "recordsTotal"、 "recordsFiltered"

php - DataTable 中的响应表

javascript - jQuery 改变 html 文本

javascript - 使用数据属性过滤选择框(使用第三方JS过滤代码)