30 Posts
claudioalfonso
1 year ago
3
Topic

Hello

I'm not posting a doubt, but a solution: exporting search data to csv. 1. Create custom html module 2. Insert in highlighted code 3. Publish the module on the search page in a position at the end of the search

----- code

<script>

function exportTableToCSV(filename) {

var csv = [];

var rows = document.querySelectorAll("table tr");

for (var i = 0; i < rows.length; i++) {

var row = [], cols = rows[i].querySelectorAll("td, th");

for (var j = 0; j < cols.length; j++)

row.push(cols[j].innerText);

csv.push(row.join(","));

}

// Download CSV file

downloadCSV(csv.join("\n"), filename);

}

function downloadCSV(csv, filename) {

var csvFile;

var downloadLink;

// CSV file

csvFile = new Blob([csv], {type: "text/csv"});

// Download link

downloadLink = document.createElement("a");

// File name

downloadLink.download = filename;

// Create a link to the file

downloadLink.href = window.URL.createObjectURL(csvFile);

// Hide download link

downloadLink.style.display = "none";

// Add the link to DOM

document.body.appendChild(downloadLink);

// Click download link

downloadLink.click();

}

</script>

<button onclick="exportTableToCSV('SinDab_query.csv')">Baixar planilha de resultados</button>

Get a VIP membership
151 Posts
jimenaes
1 year ago
2
Level 1

Hi, claudioalfonso, thanks for sharing this. The custom html module is a normal  joomla module, right? The code you wrote goes in that html module, right?

Could you please explain what you mean by "2. Insert in highlighted code 3"?

Thanks again

30 Posts
claudioalfonso
14 days ago
1
Level 2

new solution

<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Exportar para XLSX</title> <script src="https://cdn.jsdelivr.net/npm/xlsx/dist/xlsx.full.min.js"></script> <style> /* Estilizando o botão para evitar conflito com o template */ .exportar-btn { background-color: #007bff; color: white; border: none; padding: 10px 20px; font-size: 14px; cursor: pointer; border-radius: 5px; margin-top: 10px; } .exportar-btn:hover { background-color: #0056b3; } </style> </head> <body> <table id="cck1r"> </table> <button onclick="exportarTabelaParaXlsx()">Exportar para XLSX</button> <script> (function () { function exportarTabelaParaXlsx() { // Seleciona a tabela const tabela = document.getElementById('cck1r'); // Extrai os dados relevantes (ignorando links) const dados = []; const linhas = tabela.querySelectorAll('tr'); linhas.forEach(linha => { const colunas = linha.querySelectorAll('th, td'); const linhaDados = []; colunas.forEach(celula => { const texto = celula.textContent.trim(); // Captura somente o texto linhaDados.push(texto); }); if (linhaDados.length > 0) { dados.push(linhaDados); } }); // Cria uma planilha com os dados const ws = XLSX.utils.aoa_to_sheet(dados); const wb = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(wb, ws, 'Dados'); // Exporta o arquivo XLSX XLSX.writeFile(wb, 'Resultados_Consulta.xlsx'); } // Exponha a função no escopo global window.exportarTabelaParaXlsx = exportarTabelaParaXlsx; })(); </script> </body> </html>

30 Posts
claudioalfonso
14 days ago
0
Level 3

Html module

Get a Book for SEBLOD