2016-05-02 5 views
2

私はPHPスクリプトからこのJSONを受けAJAX呼び出しを使用して、DataTableのjQueryプラグインを使用してこのHTMLテーブルを移入しようとしている、からのDataTable(jQueryプラグイン)AJAXを使用してテーブルを移入ない、有効なJSONは

レスポンスを返されますサーバー:

{ 
    "newsletters": [{ 
     "anio": "2016", 
     "mes": "1", 
     "quincena": "1" 
    }, { 
     "anio": "2016", 
     "mes": "1", 
     "quincena": "2" 
    }] 
} 

HTMLファイル:

<div id="tabla_newsletters" > 
    <table id="newsletter_datatable"> 
     <thead> 
      <tr> 
       <th>anio</th> 
       <th>mes</th> 
       <th>quincena</th> 
      </tr> 
     </thead> 
     <tbody> 

     </tbody> 

    </table> 
</div> 


<script> 
$(document).ready(function(){ 


      var table = $('#newsletter_datatable').DataTable({ 
       ajax: { 
        url: '/newsletter/getNewsletters', 
        dataSrc: 'newsletters' 
       }, 
       columns:[ 
          { 'newsletters': 'anio' }, 
          { 'newsletters': 'mes' }, 
          { 'newsletters': 'quincena' } 
        ],  
      }); 

    }); 
</script> 

、その後、私のPHPサービス(symfony 1.4で作られた)、私はこの前に述べたようにに従って正しいJSONを返しますJSONオンラインバリ:

public function executeGetNewsletters(sfWebRequest $request){ 

    $conn = Doctrine_Manager::getInstance()->getCurrentConnection(); 

    $qry=$conn->execute("Select anio,mes,quincena from newsletters"); 

      $newsletters = $qry; 

      $dataNews = array(); 
      $i=0; 
       foreach ($newsletters as $news) 
        { 

          $dataNews[$i] = array(
           "anio" => $news['anio'], 
           "mes" => $news['mes'], 
           "quincena" => $news['quincena'], 
          ); 
         ++$i; 
        } 

      $output = array(
       "newsletters" => $dataNews, 
      ); 

      $json=$this->renderText(json_encode($output)); 
      return $json; 




    } 

のDataTableには、次のエラーがスローされます。

「のDataTable警告:テーブルID = newsletter_datatable - このエラーの詳細については、行0の要求された未知パラメータ 『0』、参照してください。 http://datatables.net/tn/4

私は他の例を見てみたが、彼らは一見異なるケースだった.....

編集:私はすでにそれを解決し....問題は、サーバー上のスクリプトにありました、インデックスは数字である必要がありましたサーバーは今から

-response(どうやらプラグインは、私は他の人のバグを見てきたものからのみ、このフォーマットを受け付けます):

....今では、テーブルを正しく

最終的なコードに取り込むています

{"newsletters":[["2016","1","1"],["2016","1","2"]]} 

-htmlファイルのコードは次のとおりです。

<div id="tabla_newsletters" > 
    <table id="newsletter_datatable"> 
     <thead> 
      <tr> 
       <th>anio</th> 
       <th>mes</th> 
       <th>quincena</th> 
      </tr> 
     </thead> 
     <tbody> 

     </tbody> 

    </table> 
</div> 


<script> 
$(document).ready(function(){ 


      var table = $('#newsletter_datatable').DataTable({ 
       ajax: { 
        url: '/newsletter/getNewsletters', 
        dataSrc: 'newsletters' 
       } 
      }); 

    }); 
</script> 

とPHPのサービスコードされた(私は数字だけに、フィールド名から連想配列のインデックスを変更した):

public function executeGetNewsletters(sfWebRequest $request){ 

    $conn = Doctrine_Manager::getInstance()->getCurrentConnection(); 

    $qry=$conn->execute("Select anio,mes,quincena from newsletters"); 

      $newsletters = $qry; 

      $dataNews = array(); 
      $i=0; 
       foreach ($newsletters as $news) 
        { 

          $dataNews[$i] = array(
           "0" => $news['anio'], 
           "1" => $news['mes'], 
           "2" => $news['quincena'], 
          ); 
         ++$i; 
        } 

      $output = array(
       "newsletters" => $dataNews, 
      ); 

      $json=$this->renderText(json_encode($output)); 
      return $json; 




    } 

答えて

1

はFYI:あなたは、単に右のそれをすることによって、あなたのオリジナルの問題を解決している可能性:

columns:[ 
    { data: 'anio' }, //NOT { 'newsletters': 'anio' } 
    { data: 'mes' }, 
    { data: 'quincena' } 
], 

使用dataをカラムに行くべき各ニュースレターの項目からどの属性を定義します。

+1

ありがとうございます!私は今コードを変更しますxD – Juan

関連する問題