weixin_33738555 2020-01-22 16:25 采纳率: 0%
浏览 86

Ajax数据表排序

Is it possible to sorting based on data inside while loop? without using the sql order by. But also after i select which column, it will sort desc or asc based on data inside not sort by database column and limit it only 10 every page

lookup.php

    $draw            = $_POST['draw'];
$row             = $_POST['start'];
$rowperpage      = $_POST['length']; // Rows display per page
$columnIndex     = $_POST['order'][0]['column']; // Column index
$columnName      = $_POST['columns'][$columnIndex]['data']; // Column name
$columnSortOrder = $_POST['order'][0]['dir']; // asc or desc
$searchValue     = $_POST['search']['value']; // Search value

## Custom Field value
$searchByName    = $_POST['searchByName'];
$searchByGender  = $_POST['searchByGender'];
$sel = $con->query("SELECT * FROM a, b WHERE a.nrek=b.nrek GROUP BY a.name");
$records = $con->num_rows();
$totalRecords = $records;

## Total number of records with filtering
$sel = $con-query("SELECT * FROM a, b WHERE a.nrek=b.nrek AND 1 ".$searchQuery."GROUP BY a.name");
$records = $con->num_rwos();
$totalRecordwithFilter = $records;


               $empQuery = $con->query("SELECT * FROM a, b, c WHERE a.nrek=b.nrek AND a.kode=c.kode AND a.kode='3' GROUP BY a.nrek ORDER BY ".$columnName." ".$columnSortOrder." limit ".$row.",".$rowperpage");

               while ($row1 = $empquery->fetch_array()) 
               {            
                   $querybalance2       = $con->query("SELECT SUM(b.balance) as balance FROM a JOIN a ON a.nrek= b.nrek WHERE a.kode= '$row1[kode]' AND b.period='$_SESSION[lastperiod]'");

                   $querybalance2       = $con->query("SELECT SUM(b.balance) as balance FROM a JOIN a ON a.nrek= b.nrek WHERE a.kode= '$row1[kode]' AND b.period='$_SESSION[newperiod]'");

                   $balance2 = $querybalance2->fetch_array();
                   $balance1 = $querybalance1->fetch_array();

                  $data[] = array(
                    "arek"=>$row1[nrek],
                    "balance1"=>$balance1,
                    "balance2"=>$balance2
                   );
               }

    $response = array(
    "draw" => intval($draw),
    "iTotalRecords" => $totalRecords,
    "iTotalDisplayRecords" => $totalRecordwithFilter,
    "aaData" => $data
);

echo json_encode($response);

Ajax.js

var dataTable = $('#empTable').DataTable
({
    'processing': true,
    'serverSide': true,
    'serverMethod': 'post',
    //'searching': false, // Remove default Search Control
    'ajax': 
    {
        'url':'lookup.php',
        'data': function(data)
        {

        }
    },
    'columns': 
    [
        { data: 'arek' },
        { data: 'balance1', className: 'text-center' },
        { data: 'balance2', className: 'text-center' },
    ]
});

html.php

            <table id="empTable" class="display dataTable">
                    <thead>
                        <tr>
                            <th>Nrek</th>
                            <th>Balance1</th>
                            <th>Balance2</th>
                        </tr>
                    </thead>
                </table>

I want to limit it so the data will load and display faster. If user click on the column, datatable will sort it based on data not column. I tried but "invalid json response", because there isn't "balance2" column in database.

Need help and thank you

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
    • ¥15 python爬取bilibili校园招聘网站
    • ¥30 求解达问题(有红包)
    • ¥15 请解包一个pak文件
    • ¥15 不同系统编译兼容问题
    • ¥100 三相直流充电模块对数字电源芯片在物理上它必须具备哪些功能和性能?
    • ¥30 数字电源对DSP芯片的具体要求
    • ¥20 antv g6 折线边如何变为钝角
    • ¥30 如何在Matlab或Python中 设置饼图的高度
    • ¥15 nginx中的CORS策略应该如何配置