1

これに関する彼らのドキュメント ( datatables.net ) は、いくつかの巨大な PHP チュートリアルの中に隠れているようです。私が見つけたものはここにあります: http://datatables.net/development/server-side/php_mysql

私が知りたいのは、PHPを使用して配列を作成する方法です.PHPを使用して、後で静的データ(変数なし)でjson_encode()を使用して、いくつかの列タイトルといくつかのサンプル行のデータを含むテーブルを作成します. これの一部は、上記のチュートリアルから取得されました。

<?php
    $output = array(
        "sEcho" => intval($_GET['sEcho']), //no idea what this is
        "iTotalRecords" => $iTotal, //probably total records
        "iTotalDisplayRecords" => $iFilteredTotal, //Not sure, records per page?
        "aaData" => array() //setting the data array for the rows
        );
    $output['aaData'][] = //each row here, unsure of format

    //Where do the column titles go, and the format for those?
?>
<script type="text/javascript">
    $(document).ready(function() {

        var aDataSet = <?php echo json_encode($output); ?>

        $('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
        $('#example').dataTable( {
            "aaData": aDataSet,
            "aoColumns": aoDataSet //This would be a second json_encode PHP array, set to var aoDataSet
        } ); 
    } );
</script>

どんな助けでも大歓迎です。

編集: user2657979 の回答の助けを借りて、私の質問に対する回答が得られました。以下は、PHP 配列から DataTables のタイトルとデータを入力するための答えです。

<?php
$aoColumnDefs = array(
    array(
        'sTitle'   => 'Column 1',
        'aTargets' => array(0) 
    ),
    array(
        'sTitle'   => 'Column 2',
        'aTargets' => array(1) 
    ),
    array(
        'sTitle'   => 'Column 3',
        'aTargets' => array(2) 
    ),
    array(
        'sTitle'   => 'Column 4',
        'aTargets' => array(3) 
    ),
    array(
        'sTitle'   => 'Column 5',
        'aTargets' => array(4) 
    )
);

$aoRowDefs = array(
    0   =>  array(
        0   =>  "Row 1 data Column 1", 
        1   =>  "Row 1 data Column 2",
        2   =>  "Row 1 data Column 3",
        3   =>  "Row 1 data Column 4",
        4   =>  "Row 1 data Column 5"
        ),
    1   =>  array(
        0   =>  "Row 2 data Column 1",
        1   =>  "Row 2 data Column 2",
        2   =>  "Row 2 data Column 3",
        3   =>  "Row 2 data Column 4",
        4   =>  "Row 2 data Column 5"
        ),
    2   =>  array(
        0   =>  "Row 3 data Column 1",
        1   =>  "Row 3 data Column 2",
        2   =>  "Row 3 data Column 3",
        3   =>  "Row 3 data Column 4",
        4   =>  "Row 3 data Column 5"
        )
    );
?>

<script type="text/javascript">

    var aoRowDefs = <?php echo json_encode($aoRowDefs); ?>

    var aoColumnDefs = <?php echo json_encode($aoColumnDefs); ?>

    $(document).ready(function() {
        $('#demo').html( '<table cellpadding="0" cellspacing="0" border="0" class="display" id="example"></table>' );
        $('#example').dataTable( {
        "aaData": aoRowDefs,
        "aoColumns": aoColumnDefs
        } ); 
    } );
</script>

<div id="demo">
    <div id="example">
    </div>
</div>
4

1 に答える 1