如何将具有数据范围的jQuery数据表导出为PDF,Excel,CSV和复制

时间:2018-11-19 06:43:26

标签: php jquery xampp

我有一张表格可以进行一些过滤,效果很好。在我的表单中,我只能过滤记录并将过滤后的结果生成到excel(仅过滤后的记录)中。在这里,我必须添加日期范围过滤器,如果用户仅选择from_date和to_date,则需要生成这些之间的所有记录日期。如果他/她想按列或按列生成,那么我也应该能够以这种方式生成自定义报告。我只能过滤符合这些条件的记录,但无法编写逻辑来创建SQL查询以生成报告。谢谢!

连接:

<?php
/* Database connection start */
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db_name";

$conn = mysqli_connect($servername, $username, $password, $dbname) or die("Connection failed: " . mysqli_connect_error());

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

?>

响应代码:

<?php
    //include connection file 
    include_once("connection.php");

    // initilize all variable
    $params = $columns = $totalRecords = $data = array();

    $params = $_REQUEST;

    //define index of column
    $columns = array( 
        0 =>'pid',
        1 =>'product_name', 
        2 => 'product_price',
        3 => 'added_date'
    );

    $where = $sqlTot = $sqlRec = "";

    // getting total number records without any search
    $sql = "SELECT pid,product_name,product_price,added_datek FROM `products` ";
    $sqlTot .= $sql;
    $sqlRec .= $sql;


    $sqlRec .=  " ORDER BY added_date";

    $queryTot = mysqli_query($conn, $sqlTot) or die("database error:". mysqli_error($conn));


    $totalRecords = mysqli_num_rows($queryTot);

    $queryRecords = mysqli_query($conn, $sqlRec) or die("error to fetch employees data");

    //iterate on results row and create new index array of data
    while( $row = mysqli_fetch_row($queryRecords) ) { 
        $data[] = $row;
    }   

    $json_data = array(
            "draw"            => 1,   
            "recordsTotal"    => intval( $totalRecords ),  
            "recordsFiltered" => intval($totalRecords),
            "data"            => $data   // total data array
            );

    echo json_encode($json_data);  // send data as json format
?>

我有一张表格可以进行一些过滤,效果很好。在我的表单中,我只能过滤记录并将过滤后的结果生成到excel(仅过滤后的记录)中。在这里,我必须添加日期范围过滤器,如果用户仅选择from_date和to_date,则需要生成这些之间的所有记录日期。如果他/她想按列或按列生成,那么我也应该能够以这种方式生成自定义报告。我只能过滤符合这些条件的记录,但无法编写逻辑来创建SQL查询以生成报告。谢谢!

HTML内容代码:

    <html>
    <head>

    <link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.css"/>

    <script type="text/javascript" src="https://cdn.datatables.net/r/dt/jq-2.1.4,jszip-2.5.0,pdfmake-0.1.18,dt-1.10.9,af-2.0.0,b-1.0.3,b-colvis-1.0.3,b-html5-1.0.3,b-print-1.0.3,se-1.0.1/datatables.min.js"></script>


        <div class="container" style="padding:20px;20px;">
          <div class="">
            <h1>Data Table with Export features Using PHP server-side</h1>
            <div class="">
            <table id="employee_grid" class="display" width="100%" cellspacing="0">
            <thead>
                <tr>
                    <th>PID</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Purchase Date</th>
                </tr>
            </thead>

            <tfoot>
                <tr>
                   <th>PID</th>
                    <th>Product Name</th>
                    <th>Price</th>
                    <th>Purchase Date</th>

                </tr>
            </tfoot>
        </table>
        </div>
          </div>

        </div>

    <script type="text/javascript">
    $( document ).ready(function() {
    $('#employee_grid').DataTable({
             "processing": true,
             "sAjaxSource":"response.php",
             "dom": 'lBfrtip',
             "buttons": [
                {
                    extend: 'collection',
                    text: 'Export',
                    buttons: [
                        'copy',
                        'excel',
                        'csv',
                        'pdf',
                        'print'
                    ]
                }
            ]
            });
    });
    </script>

0 个答案:

没有答案