<!DOCTYPE html>
<html lang="en">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<title>Export HTML to Excel</title>
		<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
		<script src="//cdn.rawgit.com/rainabba/jquery-table2excel/1.1.0/dist/jquery.table2excel.min.js"></script>
	</head>
	<body>
		<h2>Export Table to Excel</h2>
		<table id="dataTable">
			<thead>
				<tr>
					<th>Name</th>
					<th>Age</th>
					<th>City</th>
				</tr>
			</thead>
			<tbody>
				<tr>
					<td>John Doe</td>
					<td>30</td>
					<td>New York</td>
				</tr>
				<tr>
					<td>Jane Smith</td>
					<td>25</td>
					<td>London</td>
				</tr>
				<tr>
					<td>Bob Johnson</td>
					<td>35</td>
					<td>Paris</td>
				</tr>
			</tbody>
		</table>
		
		<button id="exportBtn">Export to Excel</button>
		 
		<script type="text/javascript">
			// Fungsi untuk mengekstrak data dari tabel
			function getTableData() {
				var data = [];
				$('#dataTable tbody tr').each(function() {
					var rowData = [];
					$(this).find('td').each(function() {
						rowData.push($(this).text());
					});
					data.push(rowData);
				});
				return data;
			}
			 
			// Ketika tombol export diklik
			$('#exportBtn').click(function() {
				var data = getTableData();
				
				// Kirim data ke controller dengan AJAX
				$.ajax({
					url: '<?= base_url("export/export_to_excel") ?>',
					type: 'POST',
					data: {
						data: data
					},
					dataType:"json",
					success: function(response) {
						if(response.url) {
							 
							window.open(response.url,"_blank");
						}
					},
					error: function(xhr, status, error) {
						console.log('Error:', error);
					}
				});
			});
		</script>
	</body>
</html>
