1

他们关于此的文档(datatables.net)似乎包含在一些巨大的 PHP 教程中。我找到的是这里:http ://datatables.net/development/server-side/php_mysql

我只想知道,如何使用 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

0

我的数据表没有动态标题,所以我在 javascript 的配置中定义它们。

"aoColumns": [{
        "sTitle": "Column 1"
    }, {
        "sTitle": "Column 2"
    }, {
        "sTitle": "Column 3"
    }, {
        "sTitle": "Column 4"
    }, {
        "sTitle": "Column 5"
    }]

插件的创建者现在建议使用 aoColumnDefs,然后指定列的目标。

"aoColumnDefs": [{
        "sTitle": "Column 1",
        "aTargets": [0]
    }, {
        "sTitle": "Column 2",
        "aTargets": [1]
    }, {
        "sTitle": "Column 3",
        "aTargets": [2]
    }, {
        "sTitle": "Column 4",
        "aTargets": [3]
    }, {
        "sTitle": "Column 5",
        "aTargets": [4]
    }]

如果某些操作会影响多于一列,aoColumnDefs 允许更大的灵活性,允许您指定多于一列。

但是,如果您的列标题是动态的,您可以将一个 json 编码的数组传递给 javascript 中的配置。

$aoColumnDefs = array(
    array(
        'sTitle'   => 'Column 1',
        'aTargets' => array(0) 
    )
);
$aoColumnDefsEndcoded = json_encode($aoColumnDefs);
于 2013-09-12T19:12:49.023 回答