javascript - 使用 AJAX jquery 对表进行排序

标签 javascript php jquery ajax twitter-bootstrap

我正在尝试使用 JS tablesort 对我的表进行排序,该表由 JSON 填充,并且出现以下错误

"Cannot read property '0' of undefined"

看起来 tablesort 无法识别 ajax 数据。

这是我的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);

这是我的 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>

有什么建议吗?

*编辑 This is my table right now

*编辑2

this is a example of object in my response

编辑 3 - 解决方案

基本上,我使用了 getData 函数中的表排序器,它填充了我的表。

因此,我在 $('#corpotabela').append(data); 行之后应用了 tablesorter,效果很好。

最佳答案

我建议您检查您收到的响应是否实际上是 JSON。 然后,尝试使用 Firebug 获取具体错误并将其发布到此处。

关于javascript - 使用 AJAX jquery 对表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34440305/

相关文章:

javascript - 推送到 Firebase 数组时写入预先确定的节点 ID

php - 单击时如何突出显示导航中的选定链接

jquery - 如何在jquery中使用具有span标签的append或wrappinner div

javascript - 如何使用 C# 从可编辑的 div 内容中获取文本

javascript - 我怎样才能更新日期?它卡在我运行应用程序的日期

javascript - 使用 Javascript 和 css 的菜单栏

javascript - jQuery 添加/删除动态滚动不起作用

php - 将 Excel 日期序列转换回可读的日期格式

java - 使用 Joda Time 模拟 PHP 日期计算

jquery - 获取选中的行id