2017-06-14 9 views
0

DataTableでHTMLに関する最新の問題を解決するには、いくつかの助けが必要です。DataTableの問題点

私は2つの列(名前とローカル)を持つテーブルを表示しています。

私は "必要な" ファイル(CSSやJS)をインポート:

<div class="tabela"> 
     <table id="myTable" class="table table-striped"> 
     <thead> 
      <tr> 
      <th class="col-lg-1" style="background:#1a4669; color:white; font-size: 16px; text-shadow: none;"> Name </th> 
      <th class="col-lg-1" style="background:#1a4669; color:white; font-size: 16px; text-shadow: none;"> Local </th> 
      </tr> 
     </thead> 
     <tbody id="listview"> 
     </tbody> 
     </table> 
    </div> 

データ:

<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.css"/> 

    <script type="text/javascript" src="https://cdn.datatables.net/v/dt/dt-1.10.15/datatables.min.js"></script> 

そしてここでは、テーブルを "描く" というのが私のHTMLコードの一部ですテーブルの私は<tbody>でテーブルを埋める<body>の最後にインポートされます:

<script src="./js/clients.js" charset="utf-8"></script> 

問題は検出されたデータがないことですが、そこにあります!私はスクリーンショット去る

:[![表示] [1]]

を、問題が何[1]

は誰もが知っていますか?

おかげ

マイclients.jsコード:

$(document).ready(function(){ 
     var url2="http://localhost:8080/CS-Management/php/clients.php"; 
     $.getJSON(url2, function(result){ 
     console.log(result); 
     $.each(result, function(i,field){ 
      var idclient = field.idclient; 
      var code = field.code; 
      var name=field.name; 
      var local=field.local; 

     if ((i % 2) == 0){ 
      $("#listview").append("<tr style='background:#FFFFFF'><td><a style='color:inherit; font-size: 14px; font-weight: bold; color:#3357C3; a:hover {background-color: yellow;}' class='item' href='myclient.html?idclient=" 
      + idclient + "'><div style='height:100%;width:100%'>" + nome 

      +"</div></a></td><td><a style='color:inherit;font-weight: bold; font-size: 14px; color:#3357C3' class='item' href='client.html?idclient=" 
      + idclient + "'><div style='height:100%;width:100%'>" + localidade 

      +"</div></a></td></tr>"); 
      } 

      else if ((i % 2) != 0) { 
      $("#listview").append("<tr style='background:#D9E8F5'><td><a style='color:inherit; font-size: 14px; font-weight: bold; color:#3357C3' class='item' href='myclient.html?idclient=" 
      + idclient + "'><div style='height:100%;width:100%'>" + nome 

      +"</div></a></td><td><a style='color:inherit;font-weight: bold; font-size: 14px; color:#3357C3' class='item' href='myclient.html?idclient=" 
      + idclient + "'><div style='height:100%;width:100%'>" + local 

      +"</div></a></td></tr>"); 
      } 

     }); 
     }); 
}); 
+1

データテーブル用のJavaScriptコードですか? –

+0

あなたのclient.jsの内容を追加してから、私はあなたを助けることができます –

+0

非同期要求が応答を返す前に、あなたのDataTableをインスタンス化している可能性があります。 XHRの 'onload'イベントハンドラコールバックでDataTableインスタンスを開始することを確認してください。これは野生の推測であり、[最小限の完全で検証可能な例](https:// stackoverflow。com/help/mcve)を使用してください。 – Ricky

答えて

0

コメントで述べたように:

あなたが応答を返す 非同期要求する前に、あなたのDataTableをインスタンス化しています。 XHRの 'onload'イベントハンドラコールバックでDataTableインスタンスを開始することを確認してください。あなたのclients.jsで


非同期要求がそうのように、成功したら、DataTableのプラグインを開始する必要があります。

$(document).ready(function() { 
    var url2 = "http://localhost:8080/CS-Management/php/clients.php"; 
    $.getJSON(url2, function(result) { 
    console.log(result); 
    $.each(result, function(i, field) { 
     var idclient = field.idclient; 
     var code = field.code; 
     var name = field.name; 
     var local = field.local; 

     if ((i % 2) == 0) { 
     $("#listview").append("<tr style='background:#FFFFFF'><td><a style='color:inherit; font-size: 14px; font-weight: bold; color:#3357C3; a:hover {background-color: yellow;}' class='item' href='myclient.html?idclient=" + idclient + "'><div style='height:100%;width:100%'>" + name 

      + "</div></a></td><td><a style='color:inherit;font-weight: bold; font-size: 14px; color:#3357C3' class='item' href='client.html?idclient=" + idclient + "'><div style='height:100%;width:100%'>" + localidade 

      + "</div></a></td></tr>"); 
     } else if ((i % 2) != 0) { 
     $("#listview").append("<tr style='background:#D9E8F5'><td><a style='color:inherit; font-size: 14px; font-weight: bold; color:#3357C3' class='item' href='myclient.html?idclient=" + idclient + "'><div style='height:100%;width:100%'>" + name 

      + "</div></a></td><td><a style='color:inherit;font-weight: bold; font-size: 14px; color:#3357C3' class='item' href='myclient.html?idclient=" + idclient + "'><div style='height:100%;width:100%'>" + local 

      + "</div></a></td></tr>"); 
     } 

    }); 
    //Initialize the DataTable plugin here. 
    $("#myTable").DataTable(); 
    }); 
}); 

注:

  • 変数nomeは存在していれば、間違っています。nameを使用してください。
  • 変数localidadeもスペルミス(local)の可能性があります。
  • ifステートメントでコードの繰り返しを避け、スタイル の代わりにクラスを使用できます。