使用 FCE 和 Templavoila 的 Typo3 中的数据表(服务器端处理)

Datatables (Server-side processing) in Typo3 with FCE and Templavoila

我正在尝试使用灵活的内容元素和 templavoila 将 https://www.datatables.net/examples/data_sources/server_side.html 实施到 Typo3 (6.2LTS) 中。结果是一个正在运行但为空(table 中没有可用数据)table。我正在使用以下 php 脚本:

<?php

class custom_datatable {  

var $datatable;    // reference to the calling object.  

function custom_table1($columns,$conf)    
{   

    global $TSFE;        
    $TSFE->set_no_cache();

    //do whatever you want here

    //db verbindung
    mysql_connect("my_host", "my_user", "my_password");
    mysql_select_db("my_database");

    /*
     * DataTables example server-side processing script.
     *
     * Please note that this script is intentionally extremely simply to  show how
     * server-side processing can be implemented, and probably shouldn't be used as
     * the basis for a large complex system. It is suitable for simple use cases as
     * for learning.
     *
     * See http://datatables.net/usage/server-side for full details on the server-
     * side processing requirements of DataTables.
     *
     * @license MIT - http://datatables.net/license_mit
     */

    /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
     * Easy set variables
     */

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

    // 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' => 'Field1', 'dt' => 0 ),
        array( 'db' => 'Field2', 'dt' => 1 ),
        array( 'db' => 'Field3', 'dt' => 2 ),
        array( 'db' => 'Field4', 'dt' => 3 ),
        array( 'db' => 'Field5', 'dt' => 4 ),
        array( 'db' => 'Field6', 'dt' => 5 )

    );

    return $columns; 

}      
} 
?>

并在源码中得到如下结果:

 <table id="example" class="display" cellspacing="0" width="100%">
    <thead>
        <tr>
            <th>Field1</th>
            <th>Field2</th>
            <th>Field3</th>
            <th>Field4</th>
            <th>Field5</th>
            <th>Field6</th>
        </tr>
    </thead>

    <tfoot>
        <tr>
            <th>Field1</th>
            <th>Field2</th>
            <th>Field3</th>
            <th>Field4</th>
            <th>Field5</th>
            <th>Field6</th>
        </tr>
    </tfoot>
</table>

<script type="text/javascript">

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

    </script>

我做错了什么或遗漏了什么?

为了让服务器端处理工作,您必须将正确的数据格式传递给它,

{
  "draw": 1,
  "recordsTotal": 57,
  "recordsFiltered": 57,
  "data": [
    [
      "Airi",
      "Satou",
      "Accountant",
      "Tokyo",
      "28th Nov 08",
      "2,700"
    ],
    [
      "Angelica",
      "Ramos",
      "Chief Executive Officer (CEO)",
      "London",
      "9th Oct 09",
      ",200,000"
    ]
  ]
}

那么您还应该检查在 github 上找到的服务器端处理查询的 ssp class https://github.com/DataTables/DataTables/blob/master/examples/server_side/scripts/ssp.class.php

更多信息请访问 http://legacy.datatables.net/usage/server-side

你应该像这样使用它:

在你的数据表初始化中

var your_datatable_variable_here = $('#your_datatable_id').dataTable({
responsive:true,
"bFilter": true,
"oLanguage": {
"sProcessing": "link_to_your_image_processing_gif/img/ajax-loader.gif'>"
},
"autoWidth" : true,
"bProcessing": true,
"bServerSide": true,
"sAjaxSource": "your_php_file_here.php"

})

PHP 文件:

function _dataTableServerSide($iQuery,$aColumns,$dReturnType){
    $iDisplayStart = $this->input->get_post('iDisplayStart', true);
    $iDisplayLength = $this->input->get_post('iDisplayLength', true);
    $iSortCol_0 = $this->input->get_post('iSortCol_0', true);
    $iSortingCols = $this->input->get_post('iSortingCols', true);
    $sSearch = $this->input->get_post('sSearch', true);
    $sEcho = $this->input->get_post('sEcho', true);
    $sLimit = "";

    if(isset($iDisplayStart) && $iDisplayLength != '-1'){
        $sLimit = "LIMIT ".$iDisplayStart.", ".$iDisplayLength; //reverse execution of limit in sql
    }


    if(isset($iSortCol_0)) {
    $sOrder = "ORDER BY  ";
        for($i=0; $i<intval($iSortingCols); $i++) {
        $iSortCol = $this->input->get_post('iSortCol_'.$i, true);
        $bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
        $sSortDir = $this->input->get_post('sSortDir_'.$i, true);

            if($bSortable == "true") {
                $sOrder .= $aColumns[intval($iSortCol)]." ".$sSortDir;
            }
        }
    }

    $sWhere = "";
    if(isset($sSearch) && !empty($sSearch)) {
        $sWhere = "WHERE (";
            for($i=0; $i<count($aColumns); $i++) {
            $bSearchable = $this->input->get_post('bSearchable_'.$i, true);

                if(isset($bSearchable) && $bSearchable == 'true') {
                    $sWhere .= $aColumns[$i]." LIKE '%".$sSearch."%' OR ";
                }
            }
        $sWhere = substr_replace( $sWhere, "", -3 );
        $sWhere .= ")";
    }


    for ( $i=0 ; $i<count($aColumns) ; $i++ ) {
        if ( $this->input->get_post('bSearchable_'.$i, true) == "true" && $this->input->get_post('sSearch_'.$i, true) != '' ) {
            if ( $sWhere == "" ) {
                $sWhere = "WHERE ";
            }
            else {
                $sWhere .= " AND ";
            }
            $sWhere .= $aColumns[$i]." LIKE '%".$this->input->get_post('sSearch_'.$i, true)."%' ";
        }
    }

    switch($dReturnType) {
        case 1: {
            $sQuery = "SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns)).
            " FROM (".$iQuery.") ".$sWhere." ".$sOrder." ".$sLimit;
            $rResult = $this->db->query($sQuery);

            $sQuery = "SELECT FOUND_ROWS() found_rows";
            $iFilteredTotal = $this->db->query($sQuery)->row()->found_rows;

            $sQuery = "SELECT COUNT(*) counter FROM (".$iQuery.") ";
            $iTotal = $this->db->query($sQuery)->row()->counter;
        } break;
        case 2: {
            $sQuery = "SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns)).
            " FROM (".$iQuery.") AA ".$sWhere." "."ORDER BY gl_sub_id ASC LIMIT 1,10";//$sOrder." ".$sLimit;
            $rResult = $this->db->query($sQuery);

            $sQuery = "SELECT FOUND_ROWS() found_rows";
            $iFilteredTotal = $this->db->query($sQuery)->row()->found_rows;

            $sQuery = "SELECT COUNT(*) counter FROM (".$iQuery.") AA";
            $iTotal = $this->db->query($sQuery)->row()->counter;
        }
    }

    $output = array(
        'sEcho' => intval($sEcho),
        'iTotalRecords' => $iTotal,
        'iTotalDisplayRecords' => $iFilteredTotal,
        'aaData' => array()
    );



    foreach($rResult->result_array() as $aRow) {
    $row = array();

        foreach($aColumns as $col) {
            $row[] = $aRow[$col];
        }

    $output['aaData'][] = $row;
    }

    return $output;
}

注意:这是一个工作示例,我使用 code igniter 作为基础框架,MySQL 作为数据库,如果你想将它转换为 PHP,只需替换代码使用标准 php $GET 方法

的点火器功能

您需要从客户端 $GET 以下内容才能使其正常工作。

$iDisplayStart = $this->input->get_post('iDisplayStart', true);
$iDisplayLength = $this->input->get_post('iDisplayLength', true);
$iSortCol_0 = $this->input->get_post('iSortCol_0', true);
$iSortingCols = $this->input->get_post('iSortingCols', true);
$sSearch = $this->input->get_post('sSearch', true);
$sEcho = $this->input->get_post('sEcho', true);
$iSortCol = $this->input->get_post('iSortCol_'.$i, true);
$bSortable = $this->input->get_post('bSortable_'.intval($iSortCol), true);
$sSortDir = $this->input->get_post('sSortDir_'.$i, true);
$bSearchable = $this->input->get_post('bSearchable_'.$i, true);

这是处理数据以传回客户端页面的地方

$output = array(
    'sEcho' => intval($sEcho),
    'iTotalRecords' => $iTotal,
    'iTotalDisplayRecords' => $iFilteredTotal,
    'aaData' => array()
);



foreach($rResult->result_array() as $aRow) {
$row = array();

    foreach($aColumns as $col) {
        $row[] = $aRow[$col];
    }

$output['aaData'][] = $row;
}

return $output;