0

我有以下设计

function crudDataTable()
{
    this.addEditFormContainerSelector = ''; 
    this.paginationType = "full_numbers";  // Provide Pagination Type
    this.processing = true; 
    this.serverSide = true;  //I want this this to be accessible in fnRowCallback below:

    this.create = function(){

        this.tableInstance = $(this.tableLocationSelector).DataTable({
            "sPaginationType": this.paginationType
            fnRowCallback:function(nRow, aData) {
                // "this" from crudDataTable should be accessible here
            }  
        })
    }
}

我希望thisfromcrudDataTable可以在fnRowCallback.

我怎样才能做到这一点?,也是fnRowCallbackdatatable组件触发的,所以这不在我的控制范围内。如何thisfnRowCallback.

或者,如果这是不可能的,那么实现它的其他方法是什么?

我正在尝试编写一个组件,这些组件的用户可以简单地设置其变量并调用 create 函数。我正面临着thisfnRowCallback函数中访问的挑战。

谢谢。

4

1 回答 1

2

您可以存储this对回调外部的引用:

var table = this;
this.tableInstance = $(this.tableLocationSelector).DataTable({
     "sPaginationType": this.paginationType,
     fnRowCallback: function(nRow, aData) {
         //Use `table` to refer to the instance of `crudDataTable`
     }  
});

或者,您可以使用 ES5bind方法(但请注意,旧浏览器不支持它):

this.tableInstance = $(this.tableLocationSelector).DataTable({
     "sPaginationType": this.paginationType,
     fnRowCallback: function(nRow, aData) {
         //Use `this` to refer to the instance of `crudDataTable`
     }.bind(this);
});
于 2012-06-14T14:59:43.297 回答