简体   繁体   English

如何在 php 的数据表中添加导出到 excel pdf 按钮

[英]How to add export to excel pdf buttons in datatable in php

I have fetch some data from database in datatable with edit delete buttons and filtering whole data by custom select option.我已经使用编辑删除按钮从数据表中的数据库中获取了一些数据,并通过自定义 select 选项过滤了整个数据。

Note:- but my requirement is this:-注意:-但我的要求是:-

(1). (1)。 add export to excel pdf buttons in datatable.添加导出到数据表中的 excel pdf 按钮。

(2). (2)。 how to add more columns in single row in datatable.如何在数据表的单行中添加更多列。

HTML Code:- HTML 代码:-

<table id="example1" class="display table">
<thead>
 <tr>
 <th>S.No</th>
<th>Job Title</th>
<th>Experience</th>
 <th>Salary</th>
 <th>Action</th>
 </tr>
 </thead>
</table>

jQuery / Ajax code:- jQuery / Ajax 代码:-

<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.min.js"></script>
    <script type="text/javascript" src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"></script>

    
 
  <script type="text/javascript" rel="stylesheet">
//displaying data on page start here
        $(document).ready(function(){
            loadDatatableAjax();
        });

        function loadDatatableAjax(){
            $('#example1').DataTable({
                "bDestroy" : true,
                "ajax" : "ajaxCompany_search.php",
                "initComplete" : function(){
                    var notApplyFilterOnColumn = [4];
                    var inputFilterOnColumn = [0];
                    var showFilterBox = 'beforeHeading'; //beforeHeading, afterHeading
                    $('.gtp-dt-filter-row').remove();
                    var theadSecondRow = '<tr class="gtp-dt-filter-row">';
                    $(this).find('thead tr th').each(function(index){
                        theadSecondRow += '<td class="gtp-dt-select-filter-' + index + '"></td>';
                    });
                    theadSecondRow += '</tr>';

                    if(showFilterBox === 'beforeHeading'){
                        $(this).find('thead').prepend(theadSecondRow);
                    }else if(showFilterBox === 'afterHeading'){
                        $(theadSecondRow).insertAfter($(this).find('thead tr'));
                    }

                    this.api().columns().every( function (index) {
                        var column = this;

                        if(inputFilterOnColumn.indexOf(index) >= 0 && notApplyFilterOnColumn.indexOf(index) < 0){
                            $('td.gtp-dt-select-filter-' + index).html('<input type="text" class="gtp-dt-input-filter">');
                            $( 'td input.gtp-dt-input-filter').on( 'keyup change clear', function () {
                                if ( column.search() !== this.value ) {
                                    column
                                        .search( this.value )
                                        .draw();
                                }
                            } );
                        }else if(notApplyFilterOnColumn.indexOf(index) < 0){
                            var select = $('<select><option value="">Select</option></select>')
                                .on( 'change', function () {
                                    var val = $.fn.dataTable.util.escapeRegex(
                                        $(this).val()
                                    );
             
                                    column
                                        .search( val ? '^'+val+'$' : '', true, false )
                                        .draw();
                                } );
                            $('td.gtp-dt-select-filter-' + index).html(select);
                            column.data().unique().sort().each( function ( d, j ) {
                                select.append( '<option value="'+d+'">'+d+'</option>' )
                            } );
                        }
                    });
                }
            
            });
        }
 
</script>  

ajaxCompany_search.php ajaxCompany_search.php

<?php
include('../../config.php');

$sql = "SELECT * FROM `jobpost` where `status`=1";
$records = mysqli_query($con,$sql);

$resultList = array();

while ($project =  mysqli_fetch_array($records))
    {
        $resultList[] = $project;
    }
        
        $result = array();
        $button = '';
        $i = 1;
        foreach ($resultList as $key => $value) {
             
                        
            $button = '<a class="btn-sm btn-success text-light" onclick="editFun('.$value['id'].')" href="#"> Edit</a> ';

            $button .= ' <a class="btn-sm btn-danger text-light" onclick="deleteFun('.$value['id'].')" href="#"> Delete</a>';

            $result['data'][] = array(
                $i++,
                $value['jobtitle'],
                $value['experience'],
                $value['salary'],
                $button
            );
        }
        
        
        echo json_encode($result);
?>

Add following code to your datatable function将以下代码添加到您的数据表 function

  dom: 'Bfrtip',
    buttons: [
        'copyHtml5',
        'excelHtml5',
        'csvHtml5',
        'pdfHtml5'
    ]

Example例子

$(document).ready(function() {
    $('#example').DataTable( {
        dom: 'Bfrtip',
        buttons: [
            'copyHtml5',
            'excelHtml5',
            'csvHtml5',
            'pdfHtml5'
        ]
    } );
} );

For details you can check at 有关详细信息,您可以查看

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

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