JSFiddle - React, Tailwind, and code Playground
HTML
<html>
<head>
<title>Filtered CSV File</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="static/bootstrap.min.css" rel="stylesheet" media="screen">
<link rel="stylesheet" type="text/css" href="https://cdn.datatables.net/v/bs/dt-1.10.18/datatables.min.css"/>
</head>
<body>
<h1>
Filtered CSV FIle
</h1>
<br/>
<br/>
<div>
<input type="button" value="Import" class="btn btn-info" id="import">
</div>
<br/>
<div class="table-responsive">
<table class="dataframe my_class" id="my_id">
<thead>
<tr style="text-align:right;">
<th> </th>
<th>col1</th>
<th>col2</th>
<th>col3</th>
<th>col4</th>
<th>col5</th>
<th>col6</th>
<th>col7</th>
<th>col8</th>
</tr>
</thead>
<tbody>
<tr>
<th>1</th>
<td>row1</td>
<td>row1</td>
<td>row1</td>
<td></td>
<td>row1</td>
<td><select name="code">
<option value="a">AAA</option>
<option value="b">BBB</option>
</select></td>
<td>row1</td>
<td>row1</td>
</tr>
<tr>
<th>2</th>
<td>row2</td>
<td>row2</td>
<td>row2</td>
<td></td>
<td>row2</td>
<td><select name="code">
<option value="a">AAA</option>
<option value="b">BBB</option>
</select></td>
<td>row2</td>
<td>row2</td>
</tr>
<tr>
<th>3</th>
<td>row3</td>
<td>row3</td>
<td>row3</td>
...
CSS
table {
border-collapse: collapse;
border: 1px black solid;
font: 12px sans-serif;
width: 100%;
table-layout:auto;
}
td {
border: 1px black solid;
text-align: left;
padding: 2px;
}
thead:hover{
text-decoration: none !important;
}
thead tr:first-child{
color:white;
text-align: center;
background-color: #5bc0de;
padding: 10px;
}
tr:nth-child(even){
background-color: #f2f2f2
}
tr:hover{
background-color: #5bc0de;
}
button{
display: inline;
padding: 50px;
}
input button{
display: inline;
}
.dropbtn{
background-color: blue;
}
.table-responsive {
overflow-y: auto;
height: 800px;
}
.table-responsive table {
border-collapse: collapse;
width: 100%;
}
.table-responsive thead th{
position: sticky;
top: 0;
background-color: #5bc0de;
padding: 2px;
}
::-webkit-scrollbar {
width: 12px;
}
::-webkit-scrollbar-thumb {
background-color: darkgrey;
outline: 1px solid slategrey;
}
.myButtons{
display: inline;
padding: 20px;
}
JavaScript
jQuery("#import").on('click', function (event) {
$('#my_id').table2csv({
file_name: 'test.csv',
header_body_space: 0
});
});
// JQuery Plugin
/**
* @description: Plugin to export HTML table to CSV file.
* @author: VenkataRamanaB
* @link: https://github.com/venkataramanab/table2csv
* Feel free to use or modify this plugin as far as my full name is kept
*/
(function ($) {
const _trim_text = (text) => {
return text.trim();
};
const _quote_text = (text) => {
return '"' + text + '"';
};
const _export = (lines, file_name) => {
const uri = 'data:text/csv;charset=utf-8,' + encodeURIComponent(lines.join('\n'));
const el_a = document.createElement('a');
el_a.href = uri;
el_a.download = file_name;
document.body.appendChild(el_a);
el_a.click();
document.body.removeChild(el_a);
};
const init = (tb, options) => {
let lines = [];
$(tb).find('thead>tr').each(function () {
let line = [];
$(this).find('th').each(function () {
line.push(_quote_text(_trim_text($(this).text())));
});
lines.push(line.splice(0).toString());
})
for (let i = 0; i < options.header_body_space; i++) lines.push('\n');
$(tb).find('tbody>tr').each(function () {
let line = [];
$(this).find('td').each(function () {
//This is what I tried to import but it is not working
/*if($(this).find('select').each(function(){
line.push(_quote_text($(this).find("option:selected").text()));
}));*/
line.push(_quote_text(_trim_text($(this).text())));
});
lines.push(line.splice(0).toString());
})
_export(lines, options.file_name)
};
...