Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

bootstrap 4 - Include datatable export button using initComplete

I'm trying to follow this example https://datatables.net/extensions/buttons/examples/styling/bootstrap.html to include the export buttons at datatable. Check on the comments tab, it's saying that initComplete need to be used for serverside data.

I'm using PHP and here is my code. You can use https://wtools.io/php-sandbox to test it. Please select PHP V5.6.31.

<!DOCTYPE html>
<html>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.datatables.net/1.10.23/css/dataTables.bootstrap.min.css" rel="stylesheet" type="text/css" />
<link href="https://cdn.datatables.net/buttons/1.6.5/css/buttons.bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<body>

<?php
$users = [
    [
        'id' => 1,
        'name' => 'User 1'
    ],
    [
        'id' => 2,
        'name' => 'User 2'
    ]
];
?>

<table id="users" class="table table-hover table-striped datatable">
    <thead>
        <tr>
            <th width="20">#</th>
            <th width="65">Name</th>
        </tr>
    </thead>
    <tbody>
    <?php if($users != null){ $i=0; foreach($users as $row){ $i++; ?>
    <tr>
        <td class="text-center"><?php echo $i; ?></td>
        <td><?php echo $row['name']; ?></td>
    </tr>
    <?php }}; ?>
    </tbody>
</table>

<script type="text/javascript" src="https://code.jquery.com/jquery-3.5.1.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.23/js/jquery.dataTables.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/1.10.23/js/dataTables.bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/dataTables.buttons.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.bootstrap.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/pdfmake.min.js"></script>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.53/vfs_fonts.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.html5.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.print.min.js"></script>
<script type="text/javascript" src="https://cdn.datatables.net/buttons/1.6.5/js/buttons.colVis.min.js"></script>

<script>
$('.datatable').DataTable({
  lengthChange: false,
  buttons: [ 'copy', 'excel', 'pdf', 'colvis' ],
  initComplete: function(settings, json){
    $('.datatable').wrap('<div class="table-responsive"></div>');
    $('.datatable').buttons().container().appendTo( $('.datatable .col-sm-6:eq(0)'));
  }
});
</script>

</body>
</html>
question from:https://stackoverflow.com/questions/65649537/include-datatable-export-button-using-initcomplete

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...