【发布时间】:2019-09-12 14:06:57
【问题描述】:
我已经定义了一个类如下:
(function() {
'use strict';
function UsersHelper(){
this.init();
this.usersTable = null;
}
UsersHelper.prototype.init = function(){
$('#userType').on('change', function(){
let newData = [{ 'name': 'foo' }];
this.usersTable.row.add(newData);
}.bind(this));
this.initUserTable();
};
UsersHelper.prototype.initUserTable = function(){
this.usersTable = $('#my-dataTable').DataTable({
columns: [{
data: 'name'
}],
destroy: true,
});
$('#userType').trigger('change');
};
});
基本上我已经在类的构造函数中声明了字段usersTable,它实际上包含DataTable 实例,然后init 方法创建DataTable 并保存实例。
初始化完成后,我触发userType 上的更改事件,该事件应将newData 行添加到存储在类中的usersTable。
问题是没有添加数据。我看到如果我在UsersHelper 之外声明变量usersTable,那么不使用this.usersTable 一切正常。
我做错了什么?
【问题讨论】:
标签: javascript jquery datatables