I am trying to display excel button using datatable options. Buttons are showing but when I add dynamic columns to the table, because of some null values in the columns, buttons not working. Actually table column headers are fixed in number but column values differ (they are not fixed in number). Please find the below attached screenshot for reference.
https://i.stack.imgur.com/7gEoV.png
Below is my code when is adding dynamic columns:
<thead>
<tr>
<th>Serial Number</th>
<th>Company Name</th>
<th>Sector</th>
<th>Main Product</th>
<th>City</th>
<th>Province</th>
<th>Owner Name</th>
<th>Artisan 1</th>
<th>Trade</th>
<th>Artisan 2</th>
<th>Trade</th>
</tr>
</thead>
/////////// PHP Code for Retrieving Column Values ///////////
$data .= "<tr>";
$extra = " ";
$sqlArtisan = "select artisan_name,trade from sme_artisans where sme_id = $id order by sme_id limit 2";
$retArtisan = pg_query($db, $sqlArtisan);
$rows = pg_num_rows($retArtisan);
if($rows > 0){
$data .= "<td>".$serialNumber."</td><td>".$companyName."</td><td>".$industryName."</td><td>".$mainProduct."</td>
<td>".$cityName."</td><td>".$province."</td>
<td>".$ownerName."</td>";
while($rowArtisan = pg_fetch_row($retArtisan)){
if($rowArtisan[0] == null){
$data .= "<td>".$extra."</td><td>".$extra."</td>";
}else{
$data .= "<td>".$rowArtisan[0]."</td><td>".$rowArtisan[1]."</td>"; }
}
}
$data .= "</tr>";
///////////// Script for Showing Buttons ////////////
<script>
$(document).ready(function() {
$('#example').DataTable( {
"paging": false,
dom: 'B',
buttons: [
'copy', 'csv', 'excel'
]
} );
} );
</script>
Please take a look at these details and suggest a possible solution. Many Thanks.