2

I have 100 tables on this page and each has a checkbox. My goal is to download the contents of each table that has the corresponding checkbox checked to one cvs file. Here is the code for the table.

for(int i = 0; i <100; i++){%>
<table style="text-align: left;" id="Table<%= i %>" cellpadding="5" cellspacing="2" frame="box">
  <input type="checkbox" name='checkbox<%= i %>' checked> Include <br>
  <%String[][] p = players1.remove(0);
  for(int j=0;j<9;j++){ %>
    <tr>
    <%for(int k=0;k<10;k++){%>
    <td style="vertical-align: top"><%= p[j][k]%><br></td>
    <%}%>
    </tr>
  <%} %>

Here is a picture of the tables on my page to help visualize.

SatyaTNV
  • 4,137
  • 3
  • 15
  • 31
Dan S.
  • 167
  • 1
  • 3
  • 15

2 Answers2

0

Though the exact language was not mentioned, a possible option would be to check for tables where the box is selected first and if the box is selected.

print each table out with comma separated values.

By doing so you will be able to c/p all of the data into a file and have it ready to be used as such.

In other languages such as php, you can explicitly change the header, to force the site to download as a specific file to your machine rather then open up a bare web page.

WeeniehuahuaXD
  • 852
  • 3
  • 10
  • 30
  • 1
    This is on jsp. Your first suggestion is definitely not what I'm looking for. And your second suggestion wouldn't work because I don't want this info downloaded every time. I want to be able to view it first and select the ones that I want to be included on the csv. Also there needs to be info on the page that won't be included on the csv. – Dan S. Sep 06 '15 at 04:22
0

I'm not that much familiar with Jquery. This is one solution I found. Put your tables in one div with some id (myDiv). But this is not recommended to put write java code in scriplet tag so avoid it.

JSP code:

<input type="button" value="Download" id="btn"/>
<%
for(int i = 0; i <10; i++){
%>
<div id="myDiv">
<table id="table<%=i%>">
  <input type="checkbox" value='<%=i%>' class="use-class"> Include <br>
  <%
  for(int j=0;j<9;j++){ %>
    <tr>
    <%for(int k=0;k<10;k++){%>
    <td><%="abc"+i+j+k%></td>
    <%}%>
    </tr>
  <%} 
  }%>
  </table>
  </div>

Script code in JSP:

<script>
$(document).ready(function() {
$("#btn").click(function(){//Called when you clicks on Download button
var data = Array();
var result="";
$('input:checkbox.use-class').each(function () {
//get all checked checkboxes
if(this.checked)
{  
var va="table"+$(this).val();//table id to get data

$('#'+va+' tr').each(function(i, v){//get content of all rows of above table id 
    data[i] = Array();
    $(this).children('td').each(function(ii, vv){
        data[i][ii] = $(this).text();
    }); 
});
result+=data.join(",")+"##";//appending the single table result 
}
});
alert(result);
//Now pass result that contains all content of all tables to JSP with Ajax call to download
}); 
});
</script>
Community
  • 1
  • 1
SatyaTNV
  • 4,137
  • 3
  • 15
  • 31