add download csv resumo operacao table functionality
This commit is contained in:
parent
8eacf7b3e6
commit
9a7398ff15
@ -36,6 +36,39 @@ export default function ResumoOperacao({tableData, clientsData, userName, client
|
||||
setUnidade(event.target.value);
|
||||
};
|
||||
|
||||
function downloadCSVFile(csv, filename) {
|
||||
const csv_file = new Blob([csv], {type: "text/csv"});
|
||||
|
||||
const download_link = document.createElement("a");
|
||||
|
||||
download_link.download = filename;
|
||||
|
||||
download_link.href = window.URL.createObjectURL(csv_file);
|
||||
|
||||
download_link.style.display = "none";
|
||||
|
||||
document.body.appendChild(download_link);
|
||||
|
||||
download_link.click();
|
||||
}
|
||||
|
||||
function htmlToCSV(html, filename) {
|
||||
const data = [];
|
||||
const rows = document.querySelectorAll("table tr");
|
||||
|
||||
for (let i = 0; i < rows.length; i++) {
|
||||
const row = [], cols: any = rows[i].querySelectorAll("td, th");
|
||||
|
||||
for (let j = 0; j < cols.length; j++) {
|
||||
row.push(cols[j].innerText);
|
||||
}
|
||||
|
||||
data.push(row.join(","));
|
||||
}
|
||||
|
||||
downloadCSVFile(data.join("\n"), filename);
|
||||
}
|
||||
|
||||
useEffect(() => {
|
||||
if (unidade!=='' || month!==''){
|
||||
api.post('/operation/summary', {
|
||||
@ -128,10 +161,10 @@ export default function ResumoOperacao({tableData, clientsData, userName, client
|
||||
</tbody>
|
||||
</table>
|
||||
<div className='btn'>
|
||||
<CSVLink data={csvData} filename="Arquivo_Teste_Smart_Energia">
|
||||
|
||||
<BasicButton title='Baixar CSV' onClick={() => console.log()}/>
|
||||
</CSVLink>
|
||||
<BasicButton title='Baixar CSV' onClick={() => {
|
||||
const html = document.querySelector("table").outerHTML;
|
||||
htmlToCSV(html, "resumo_operacao.csv");
|
||||
}}/>
|
||||
</div>
|
||||
</TableView>
|
||||
)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user