简体   繁体   English

使用AJAX jQuery排序表

[英]Sort table with AJAX jquery

I'm trying to sort my table with the JS tablesort, who is populate with JSON, and the follow error appear to me 我正在尝试使用JSON填充的JS tablesort对表进行排序,并且出现错误提示

"Cannot read property '0' of undefined" “无法读取未定义的属性'0'”

Looks like the tablesort dont recognize the ajax data. 看起来tablesort无法识别ajax数据。

This is my jquery 这是我的jQuery

 $.get(url, function(response){

    serverResponse = response;

    for(i in response.content){

        totalclientes++;

        var status = response.content[i].LojaStatus;
            if(status == 0){
                LojaStatus = "Ativo";
                botao = '\ ' +
                    '<div class = "btn-group"> <button type = "button" class = "btn btn-primary" onclick="redirect(' + response.content[i].LojaId + ')" data-toggle="modal" data-target="#myModal">Editar</button>       <button type = "button" class = "btn btn-primary dropdown-toggle" data-toggle = "dropdown"> <span class = "caret"></span> <span class = "sr-only">Toggle Dropdown</span> </button> <ul class = "dropdown-menu" role = "menu"> <li><a href = "javascript:deletar(' + response.content[i].LojaId + ')">Desativar</a></li> <li><a href = "http://amovitrine.devmaker.com.br/relatorioloja.php?LojaId='+ response.content[i].LojaId +'">Relatorio</a></li> </ul> </div>';
            }else{
                LojaStatus = "Inativo";
                botao = '\ ' +
                    '<div class = "btn-group"> <button type = "button" class = "btn btn-primary" onclick="redirect('+response.content[i].LojaId+')" data-toggle="modal" data-target="#myModal">Editar</button>       <button type = "button" class = "btn btn-primary dropdown-toggle" data-toggle = "dropdown"> <span class = "caret"></span> <span class = "sr-only">Toggle Dropdown</span> </button> <ul class = "dropdown-menu" role = "menu"> <li><a href = "javascript:ativar('+response.content[i].LojaId+')">Ativar</a></li><li><a href = "http://amovitrine.devmaker.com.br/relatorioloja.php?LojaId='+ response.content[i].LojaId +'">Relatorio</a></li> </ul> </div>';
            }

        var botao =


            data +='\
        <tr>\
            <td>'+response.content[i].LojaNome+'</td>\
            <td>'+response.content[i].LojaBairro+'</td>\
            <td>'+response.content[i].LojaTelefone1+'</td>\
            <td>'+LojaStatus+'</td>\
            <td>'+response.content[i].PlanoNome+'</td>\
            <td>'+response.content[i].LojaInicioPlano+'</td>\
            <td>'+response.content[i].LojaFimPlano+'</td>\
            <td>'+botao+'</td>\
        </tr>';
    }

    $('#corpotabela').empty();
    $('#corpotabela').append(data);

This is my table 这是我的桌子

 <table class="table table-bordered table-hover" id="table">
            <thead style="border: 1px solid #ddd;" >
            <tr style="cursor: pointer;">
                <th>Nome</th>
                <th>Bairro</th>
                <th>Telefone</th>
                <th>Status</th>
                <th>Plano</th>
                <th>Data Inicio</th>
                <th>Data Fim</th>
                <th>Ações</th>
            </tr>
            </thead>
            <tbody id="corpotabela">

            </tbody>
        </table>

Any suggestions? 有什么建议么?

*EDIT This is my table right now *编辑这是我的桌子

*EDIT 2 *编辑2

this is a example of object in my response 这是我回应中对象的一个​​例子

EDIT 3 - THE SOLUTION 编辑3-解决方案

Basically I was using the tablesorter out of the getData function, who populate me table. 基本上,我是在getData函数中使用tablesorter的,后者将表格填充到了我。

So, i apply the tablesorter after $('#corpotabela').append(data); 因此,我在$('#corpotabela').append(data);之后应用表$('#corpotabela').append(data); line and it works great. 线,效果很好。

I would recommend you to check if the response you're getting is actualy JSON. 我建议您检查收到的响应是否实际上是JSON。 Then, try using Firebug to get concrete error and post it here. 然后,尝试使用Firebug获取具体错误,然后将其发布在此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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