数据分页

时间:2016-12-27 07:21:33

标签: jquery pagination datatables

我正在使用数据服务器端处理来管理大型数据库。除了分页之外,一切都很完美。它完美地生成了分页按钮,并在我改变页面长度后调用ajax。但它显示的是同一页面中的所有数据。像ajax返回47行,它创建5页,每页包含10行。但它在同一页面中显示47行,并且5页都有47行。这是我的代码

function reportManagement(){
    if (my_details[0].role_id != 1) {
        my_details = JSON.parse(checkSesssion('is_auth'));
        var createdby = my_details[0].id;
    }else{
        var createdby = 1;
    }

    var generateTable = '<div class="card">'+
        '<div class="card-header" data-background-color="purple"><h4 class="title">List of Campaign</h4></div>'+
        '<div class="card-content"></div>'+
        '<div class="row"><div class="col-sm-10 col-sm-offset-1 col-md-10 col-md-offset-1"><div class="card-content table-responsive table-full-width">'+
        '<table id="dataTables_show_list" class="table table-striped table-bordered" cellspacing="0" width="100%"></table></div</div></div></div>';

    $("#editAllAccountTable").empty().html(generateTable);


    var dataInfo = {};
    dataInfo['account_id'] = createdby;
    var table =  $('#dataTables_show_list').DataTable({
        "serverSide": true,
        "processing": true,

        "ajax": {
            url: service_url['get_campaign_details'],
            type: 'POST',
            data: {'info': dataInfo}
        },

        "columns": [
            {"title": "Campaign ID", "class": "center"},
            {"title": "Campaign Type","class": "center"},
            {"title": "Number Of Services", "class": "center"},
            {"title": "Total Spent", "class": "center"},
            {"title": "Status", "class": "center"},
            {"title": "Creation Time", "class": "center"},
            {"title": "Details", "class": "center"},
            {"title": "Approved by", "class": "center"}
        ],
        "paging": true,
        "searching": { "regex": true },
        "lengthMenu": [ [10, 25, 50, 100, -1], [10, 25, 50, 100, "All"] ],
        "pageLength": 10,
        dom: 'Bfrtip',
        buttons: [
            'copy', 'csv', 'excel', 'pdf'
        ],



    });


}



<?php
/**
 * Created by PhpStorm.
 * User: Anik-pc
 * Date: 12/22/2016
 * Time: 4:52 PM
 */

include_once "../CMSWebService/config.php";
$isError = 0;

$aColumns = array( 'jobId', 'jobAction', 'description','createdTime','approvedBy');
$sIndexColumn = "jobId";
$sTable = "job";


/*
 * Paging
 */
$sLimit = "";
if ( isset( $_GET['iDisplayStart'] ) && $_GET['iDisplayLength'] != '-1' )
{
    $sLimit = "LIMIT ".intval( $_GET['iDisplayStart'] ).", ".
        intval( $_GET['iDisplayLength'] );
}


/*
 * Ordering
 */
$sOrder = "";
if ( isset( $_GET['iSortCol_0'] ) )
{
    $sOrder = "ORDER BY  ";
    for ( $i=0 ; $i<intval( $_GET['iSortingCols'] ) ; $i++ )
    {
        if ( $_GET[ 'bSortable_'.intval($_GET['iSortCol_'.$i]) ] == "true" )
        {
            $sOrder .= $aColumns[ intval( $_GET['iSortCol_'.$i] ) ]."
                    ".($_GET['sSortDir_'.$i]==='asc' ? 'asc' : 'desc') .", ";
        }
    }

    $sOrder = substr_replace( $sOrder, "", -2 );
    if ( $sOrder == "ORDER BY" )
    {
        $sOrder = "";
    }
}


/*
 * Filtering
 * NOTE this does not match the built-in DataTables filtering which does it
 * word by word on any field. It's possible to do here, but concerned about efficiency
 * on very large tables, and MySQL's regex functionality is very limited
 */
$sWhere = "";
if ( isset($_GET['sSearch']) && $_GET['sSearch'] != "" )
{
    $sWhere = "WHERE (";
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" )
        {
            $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string( $_GET['sSearch'] )."%' OR ";
        }
    }
    $sWhere = substr_replace( $sWhere, "", -3 );
    $sWhere .= ')';
}

/* Individual column filtering */
for ( $i=0 ; $i<count($aColumns) ; $i++ )
{
    if ( isset($_GET['bSearchable_'.$i]) && $_GET['bSearchable_'.$i] == "true" && $_GET['sSearch_'.$i] != '' )
    {
        if ( $sWhere == "" )
        {
            $sWhere = "WHERE ";
        }
        else
        {
            $sWhere .= " AND ";
        }
        $sWhere .= $aColumns[$i]." LIKE '%".mysql_real_escape_string($_GET['sSearch_'.$i])."%' ";
    }
}


/*
 * SQL queries
 * Get data to display
 */


    $sQuery = "SELECT jobId,jobAction,description,approvedBy FROM job $sOrder $sLimit";



$rResult = Sql_exec( $con,$sQuery ) or fatal_error( 'MySQL Error: ' . mysql_errno() );

/* Data set length after filtering */
$sQuery = "
        SELECT FOUND_ROWS()
    ";
$rResultFilterTotal = Sql_exec($con, $sQuery ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultFilterTotal = Sql_fetch_array($rResultFilterTotal);
$iFilteredTotal = $aResultFilterTotal[0];

/* Total data set length */
$sQuery = "
        SELECT COUNT(".$sIndexColumn.")
        FROM   $sTable
    ";
$rResultTotal = Sql_exec( $con,$sQuery ) or fatal_error( 'MySQL Error: ' . mysql_errno() );
$aResultTotal = Sql_fetch_array($rResultTotal);
$iTotal = $aResultTotal[0];


/*
 * Output
 */
$output = array(
    "sEcho" => intval($_GET['sEcho']),
    "iTotalRecords" => $iTotal,
    "iTotalDisplayRecords" => $iFilteredTotal,
    "aaData" => array()
);

while ( $aRow = Sql_fetch_array( $rResult ) )
{
    $row = array();
    for ( $i=0 ; $i<count($aColumns) ; $i++ )
    {
        if ( $aColumns[$i] == "jobAction" )
        {
            $row[] = ($aRow['serviceNo'] * $aRow['spent']);
            /* Special output formatting for 'version' column */

            if ( $aRow[$aColumns[$i]]== 'S') {

                $qryJobDetails = "SELECT COUNT(jobId) AS serviceNo,noofmessage*(0.1071)*(.51) AS spent,msgStatus FROM smsoutbox_temp WHERE jobId = '$id'";

                try {
                    $resJobDetails = Sql_exec($con, $qryJobDetails);
                } catch (Exception $e) {
                    $isError = 1;
                }

                while ($dtDetails = Sql_fetch_assoc($resJobDetails)) {

                    $row[] = $dtDetails['serviceNo'];
                    $row[] = ($dtDetails['serviceNo'] * $dtDetails['spent']);
                    $row[] = $dtDetails['msgStatus'];
                }

            } elseif ($aRow[$aColumns[$i]] == 'O') {

                $qryJobDetails = "SELECT COUNT(OutDialId) AS serviceNo,2*(0.1071)*(.71) AS spent,Remarks FROM outdialque WHERE TransactionId = '$id'";
                try {
                    $resJobDetails = Sql_exec($con, $qryJobDetails);
                } catch (Exception $e) {
                    $isError = 1;
                }

                while ($dtDetails = Sql_fetch_assoc($resJobDetails)) {

                    $row[] = $dtDetails['serviceNo'];
                    $row[] = ($dtDetails['serviceNo'] * $dtDetails['spent']);
                    $row[] = $dtDetails['Remarks'];
                }

            }

        }
        else if ( $aColumns[$i] != ' ' )
        {
            if($aRow[ $aColumns[$i]] == null){
                $row[] = '--';
            }else{
                $row[] = $aRow[ $aColumns[$i] ];
            }
            /* General output */

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

echo json_encode( $output );

data returns from server side processin

dataTable showing all row in a same page

0 个答案:

没有答案