256 Kilobytes

Answers in Client-Side | By August R. Garcia

Published

261 views, 0 RAMs, and 0 comments

Tags: JavaScript, Tables, TSV

Profile Photo - August R. GarciaAugust R. GarciaLARPing as a Sysadmi...Portland, ORSite Owner

Title.

<!DOCTYPE html>
<html>
<head>
<title>Test Table Download</title>
</head>
<body>

<h1>This is a Heading</h1>
<h2>HTML Table</h2>

<table id="some_data">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
    <th>Header 3</th>
  </tr>
  <tr>
    <td>Row 1, Data 1</td>
    <td>Row 1, Data 2</td>
    <td>Row 1, Data 3</td>
  </tr>
  <tr>
    <td>Row 2, Data 1</td>
    <td>Row 2, Data 2</td>
    <td>Row 2, Data 3</td>
  </tr>
  <tr>
    <td>Row 3, Data 1</td>
    <td>Row 3, Data 2</td>
    <td>Row 3, Data 3</td>
  </tr>
</table>
<a id="download_btn">Download This Query</a>  

<!-- Download Files -->
<script>
function download_table_as_tsv(table_id, link_id) {  
        var arr = [];

        rows = document.querySelectorAll("#" + table_id + " tr");
        console.log(rows.length); 
        for (iii = 0; iii < rows.length; ++iii) {  
                arr.push( [] ); 

                cells = rows[iii].querySelectorAll("tr > *");
                for (jjj = 0; jjj < cells.length; ++jjj) {  
                        console.log(cells[jjj].innerHTML); 
                        arr[iii].push(  cells[jjj].innerText.trim()  ); 
                }
        } 
        
        let tsvContent = "";
        arr.forEach(function(rowArray) {
            let row     = rowArray.join("\t");  
            tsvContent += row + "\r\n";
        });

        // Can alternatively use the first line here to base64 encode 
        //var encodedUri = "data:text/tsv;base64," + btoa(tsvContent);  
        var encodedUri = "data:text/tsv;charset=utf-8," + tsvContent;  

        document.getElementById(link_id).href     = encodedUri;

        // The filename that will be used for downloads   
        timestamp = new Date().toISOString(); 
        document.getElementById(link_id).download = "your-site-name--"+link_id+ "--" + timestamp + ".tsv"; 
}
download_table_as_tsv("some_data", "download_btn");
</script>
</body>
</html>

See Also:

Download more RAM. 🐏 ⨉ 0Posted by August R. Garcia 1 year ago 🕓 Posted at 04 August, 2019 05:42 AM PDT

Sir, I can do you a nice SEO.

Post a New Comment

Do you like having a good time?

Register an Account

You can also login to an existing account or reset your password. All use of this site is subject to the terms of service and privacy policy.

Read Quality Articles

Read some quality articles. If you can manage to not get banned for like five minutes, you can even post your own articles.

View Articles →

Argue with People on the Internet

Use your account to explain why people are wrong on the Internet forum.

View Forum →

Vandalize the Wiki

Or don't. I'm not your dad.

View Wiki →

Ask and/or Answer Questions

If someone asks a terrible question, post a LMGTFY link.

View Answers →

Make Some Money

Hire freelancers and/or advertise your goods and/or services. Hire people directly. We're not a middleman or your dad. Manage your own business transactions.

Register an Account

Answers— Read More

Find more related content below!