dongyi4170 2015-12-23 16:51
浏览 161

使用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

"Cannot read property '0' of undefined"

Looks like the tablesort dont recognize the ajax data.

This is my 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

this is a example of object in my response

EDIT 3 - THE SOLUTION

Basically I was using the tablesorter out of the getData function, who populate me table.

So, i apply the tablesorter after $('#corpotabela').append(data); line and it works great.

  • 写回答

1条回答 默认 最新

  • douye7033 2015-12-23 17:00
    关注

    I would recommend you to check if the response you're getting is actualy JSON. Then, try using Firebug to get concrete error and post it here.

    评论

报告相同问题?

悬赏问题

  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站
  • ¥50 成都蓉城足球俱乐部小程序抢票
  • ¥15 yolov7训练自己的数据集
  • ¥15 esp8266与51单片机连接问题(标签-单片机|关键词-串口)(相关搜索:51单片机|单片机|测试代码)
  • ¥15 电力市场出清matlab yalmip kkt 双层优化问题
  • ¥30 ros小车路径规划实现不了,如何解决?(操作系统-ubuntu)