繁体   English   中英

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

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

我正在尝试将https://www.datatables.net/examples/data_sources/server_side.html实现为具有灵活的内容元素和templavoila的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>

我在做什么错或丢失了?

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

{
  "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文件:

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方法

您将需要从客户端获取$ 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;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM