2017-07-10 20 views
0

私は、このマニュアルに従って、サーバー側の処理のDataTableを作成しようとしました:サーバーサイドの処理データテーブルはどのように作成できますか?

https://datatables.net/examples/server_side/simple.html

しかし、私は私のデータベースから任意の結果を得ることはありません。

のindex.php:

<link rel="stylesheet" href="https://cdn.datatables.net/1.10.15/css/jquery.dataTables.min.css"> 

<table id="example" class="display" width="100%" cellspacing="0"> 
    <thead> 
     <tr> 
      <th>id</th> 
     </tr> 
    </thead> 
    <tfoot> 
     <tr> 
      <th>id</th> 
     </tr> 
    </tfoot> 
</table> 
    <script> 

$(document).ready(function() { 
    $('#example').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": "server.php" 
    }); 
}); 

</script> 

<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script> 

server.phpという:

<?php 


// DB table to use 
$table = 'elephants'; 

// Table's primary key 
$primaryKey = 'id'; 

// Array of database columns which should be read and sent back to DataTables. 
// The `db` parameter represents the column name in the database, while the `dt` 
// parameter represents the DataTables column identifier. In this case simple 
// indexes 
$columns = array(
    array('db' => 'id', 'dt' => 0), 
); 

// SQL server connection information 
$sql_details = array(
    'user' => 'root', 
    'pass' => 'root', 
    'db' => 'animals', 
    'host' => 'localhost' 
); 


/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * 
* If you just want to use the basic configuration for DataTables with PHP 
* server-side, there is no need to edit below this line. 
*/ 

require('ssp.class.php'); 

echo json_encode(
    SSP::simple($_GET, $sql_details, $table, $primaryKey, $columns) 
); 

とssp.class.php:私は知りません

https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php

、どのようなI間違っていましたが、結果は

ですあなたが依存JSライブラリを追加する前に、あなたのコードを初期化している

ID

ID

答えて

1

<script src="https://code.jquery.com/jquery-1.12.4.js"></script> 
<script src="https://cdn.datatables.net/1.10.15/js/jquery.dataTables.min.js"></script> 
<script> 
// Initialise after adding Jquery and Datatable plugin 
$(document).ready(function() { 
    $('#example').DataTable({ 
     "processing": true, 
     "serverSide": true, 
     "ajax": "server.php" 
    }); 
}); 
</script> 

のような$columns配列にfieldパラメータを追加してみてください、のような、それを試してみてください

$columns = array(
    array('db' => 'id', 'dt' => 0, 'id') 
); 

また、Datatableには2つのカラムがありますが、サーバ側からは1つだけ返します。したがって、HTMLテーブルから2番目の列を削除するか、$columns配列に1列追加してください。

読むよりおよそSSP

+0

私はあなたが提案し、まさにテストが、残念ながら、私は私の更新asnwerを試してみてください@Jarlaどんな結果 – Jarla

+1

を得ることはありません、あなたはjQueryとDataTableのファイルを追加する前にJSを初期化しています。 –

+0

ああ!今度はデータテーブルがロードされます!しかし、私はまだ 'DataTables警告:table id = example - Ajaxエラーです。このエラーの詳細については、http:// datatables.net/tn/7'を参照してください。 – Jarla

関連する問題