如何获取数据表行值并路由到另一个函数?

时间:2017-06-28 11:57:24

标签: javascript jquery angular typescript datatable

如果我使用箭头函数,我可以路由到另一个组件,但我无法获取数据表值,下面是代码:

$('#list-country tbody').on('click', '.fa-edit', function() {
  var tr = $(this).closest("tr");
  var data = $("#list-country").DataTable().row(tr).data();
  console.log(data);
  this.router.navigate(['/apps/edit-country']);
  this.routePage();
});

如果我使用普通功能,我可以获取值,但我无法路由,下面是代码:

$('#list-country tbody').on('click', '.fa-edit', () => {

  var tr = $(this).closest("tr");
  var data = $("#list-country").DataTable().row(tr).data();
  console.log(data);
  this.routePage();
});

这些功能在ngInit()内。

2 个答案:

答案 0 :(得分:0)

此解决方案可能对您有用:

  1. 首先在全局范围(方法/类)中声明一个变量
  2. 在构造函数中将其初始化为this(如果你有)
  3. 使用self代替this,如下所示

     var self = this;  //declare this in constructor or method
    
     $('#list-country tbody').on( 'click', '.fa-edit', function ()  {
               var tr = $(this).closest("tr");
               var data = $("#list-country").DataTable().row(tr).data();
               console.log(data);
               self.router.navigate(['/apps/edit-country']);
               self.routePage();
           });
    

答案 1 :(得分:0)

$('#list-country tbody').on('click', '.fa-edit', event => {
  var tr = $(event.currentTarget).closest('tr')
  var data = $('#list-country')
    .DataTable()
    .row(tr)
    .data()

  this.router.navigate(['/apps/edit-country']);
  this.routePage()
})