2015-12-23 45 views
1

我想我的表與JS tablesort,誰是JSON填充排序,後續的錯誤在我看來與AJAX jQuery的排序表

"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 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

* EDIT 2

this is a example of object in my response

編輯3 - 破解

基本上我用的是的tablesorter出的getData功能,誰提醒我表。

所以,我申請tablesorter後$('#corpotabela').append(data);線,它的作品很棒。

+0

哪行代碼產生錯誤? –

+0

你會得到的原因是,如果你正在訪問0或未定義的變量。意思是,你希望數據在某個地方,但事實並非如此。嘗試一些'console.log'來確定返回的數據 – SoluableNonagon

+0

@CindyMeister jquery.tablesorter.js:600 –

回答

0

我會建議你檢查一下你得到的迴應是不是真實的JSON。 然後,嘗試使用Firebug獲取具體錯誤並將其發佈到此處。

+0

是的,數據來自我製作的web服務 –

+0

數據不一致或語法被破壞仍然存在可能性,甚至可能是頭文件,只要你不解析JSON。看看網絡選項卡或'console.log'迴應。 Firebug錯誤信息真的可以幫助JS。 –

+0

在firebug看起來不錯,但鉻的控制檯仍然顯示錯誤「無法讀取未定義的屬性'0'或'無法讀取屬性'1'未定義的」等每個柱子 –