简体   繁体   中英

Error when call a method in another method inside a Object Javascript

i have a little problem with my code, i have a object and i have a error Paginator.validate is not a function , i call the method validate() from another method in the same scope of the Object, the code below, i really appreciate your help :D, cheers!

function Paginator(res){
    this.total = res['data']['TotalRegistros'];
    this.resPorPagina = res['data']['RegistrosPorPagina'];
    this.pagActual = res['data']['PaginaActual'];
    this.registros = res['data']['Registros'];

    this.paginas = function(){
        return Math.round(this.total/this.resPorPagina);
    }

    this.render = function() {
        var table;
        $.each(this.registros,function(index,element){
            table += '<tr>';
            $.each(element,function(i,e){
                table += '<td>' + Paginator.validate(e) + '</td>';
            })
            table += '</tr>';
        });
        return table;
    }

    this.validate = function(e){
        if(e == null || e == false){e = 'Sin Info';}
        return e;
    }

    this.paginator = function(){
        var b = '';
        for (var i=1;i<=Paginator.paginas();i++) {
            active =  (this.pagActual === i)?'active':'';
            b += '<li class="'+active+'"><a class="btn_paginate" href="#" data-page="'+i+'">'+i+'</a></li>';
        }
        return b;
    }
}
var res = //i get the data from a ajax call in PHP
// object Paginate
var Paginate = new Paginator(res);
// object Paginate
$('#reportsTable tbody').append(Paginate.render());
$('.paginate').find('ul').html(Paginate.paginator());

The problem is that the context of this within the jquery function will not be the Paginator . You'll need to create a reference to this somewhere. Try:

this.render = function() {
    var table;
    var self = this;
    $.each(this.registros,function(index,element){
        table += '<tr>';
        $.each(element,function(i,e){
            table += '<td>' + self.validate(e) + '</td>';
        })
        table += '</tr>';
    });
    return table;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM