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

时间:2015-03-22 16:44:35

标签: php datatables typo3 templavoila

我尝试使用灵活的内容元素和templavoila将https://www.datatables.net/examples/data_sources/server_side.html实现到Typo3(6.2LTS)中。结果是目前正在运行但空(表中没有数据)表。我使用以下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>

我做错了什么或丢失了什么?

1 个答案:

答案 0 :(得分:1)

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

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

然后你还应该检查github上找到的ssp类是否有服务器端处理查询 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 FIle:

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;
}

注意:这是一个工作示例,我使用代码点火器作为基础框架,而MySQL作为数据库,如果要将其转换为PHP,只需用标准的php $ GET方法替换代码igniter函数< / p>

您需要从客户端获取以下内容才能使其正常运行。

$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;