@@ -179,6 +179,8 @@ function colorTables(){
179179
180180/* Table Generation */
181181
182+ var tableCount = 1 ;
183+
182184/**
183185 * Creates a table holder with the
184186 * given table in it.
@@ -188,16 +190,20 @@ function colorTables(){
188190 * @returns {String|getTableUnit.tableHolder }
189191 */
190192function getTableUnit ( title , tableHtml ) {
191- var tableHolder = "<div class=\"table-holder\"><p class=\"table-title\"><b>{@name}</b></p>{@table}</div>" ;
193+ var id = "table-" + tableCount ;
194+ var downloadButton = "<button title=\"Download the edited table (The name is also editable).\" onClick=\"downloadTable(" + tableCount + ")\" class=\"btn btn-default\"><i class=\"material-icons\" id=\"advanced-controls-display\">save_alt</i></button>" ;
195+ var tableHolder = "<div class=\"table-holder\" id=\"" + id + "\"><p class=\"table-title\"><b contenteditable>{@name}</b> " + downloadButton + " </p>{@table}</div>" ;
192196 tableHolder = tableHolder . replace ( "{@name}" , title ) ;
193197 tableHolder = tableHolder . replace ( "{@table}" , tableHtml ) ;
198+ tableCount ++ ;
194199 return tableHolder ;
195200}
196201
197202/**
198203 * Clears all tables from the page.
199204 */
200205function clearTables ( ) {
206+ tableCount = 1 ;
201207 $ ( "#tables" ) . html ( "" ) ;
202208}
203209
@@ -211,6 +217,90 @@ function addTableUnit(unit){
211217 $ ( "#tables" ) . append ( unit ) ;
212218}
213219
220+ /* Table downloading */
221+
222+ /**
223+ * Download the specified table to
224+ * the users computer.
225+ *
226+ * @param {type } table table number
227+ * @returns {undefined }
228+ */
229+ function downloadTable ( table ) {
230+ var tableId = "table-" + table ;
231+
232+ var myTableArray = [ ] ;
233+
234+ $ ( "#" + tableId + " > table tr" ) . each ( function ( ) {
235+ var arrayOfThisRow = [ ] ;
236+ var tableData = $ ( this ) . find ( 'th' ) ;
237+ if ( tableData . length > 0 ) {
238+ tableData . each ( function ( ) { arrayOfThisRow . push ( $ ( this ) . text ( ) ) ; } ) ;
239+ myTableArray . push ( arrayOfThisRow ) ;
240+ }
241+ } ) ;
242+
243+ $ ( "#" + tableId + " > table tr" ) . each ( function ( ) {
244+ var arrayOfThisRow = [ ] ;
245+ var tableData = $ ( this ) . find ( 'td' ) ;
246+ if ( tableData . length > 0 ) {
247+ tableData . each ( function ( ) { arrayOfThisRow . push ( $ ( this ) . text ( ) ) ; } ) ;
248+ myTableArray . push ( arrayOfThisRow ) ;
249+ }
250+ } ) ;
251+
252+ var csvArray = [ ] ;
253+ var ta = myTableArray ;
254+
255+ var separator = getUserInput ( ) . columnSeparator ;
256+
257+ for ( var i = 0 ; i < ta . length ; i ++ ) {
258+ for ( var j = 0 ; j < ta [ i ] . length ; j ++ ) {
259+ if ( j === ta [ i ] . length - 1 )
260+ csvArray . push ( "\"" + ta [ i ] [ j ] + "\"" ) ;
261+ else
262+ csvArray . push ( "\"" + ta [ i ] [ j ] + "\"" + separator ) ;
263+ }
264+ csvArray . push ( "\n" ) ;
265+ }
266+
267+ var csvString = csvArray . join ( "" ) ;
268+
269+ var fileName = $ ( "#" + tableId + " > p > b" ) . html ( ) ;
270+
271+ if ( fileName === "" || fileName === " " ) {
272+ fileName = "Project CSV Edited File.csv" ;
273+ }
274+
275+ if ( ! fileName . endsWith ( ".csv" ) ) {
276+ fileName = fileName + ".csv" ;
277+ }
278+
279+ //alert(csvString);
280+ download ( fileName , csvString ) ;
281+ }
282+
283+ /**
284+ * Creates and downloads a file to the users computer.
285+ *
286+ *
287+ * @param {type } filename
288+ * @param {type } text
289+ * @returns {undefined }
290+ */
291+ function download ( filename , text ) {
292+ var element = document . createElement ( 'a' ) ;
293+ element . setAttribute ( 'href' , 'data:text/plain;charset=utf-8,' + encodeURIComponent ( text ) ) ;
294+ element . setAttribute ( 'download' , filename ) ;
295+
296+ element . style . display = 'none' ;
297+ document . body . appendChild ( element ) ;
298+
299+ element . click ( ) ;
300+
301+ document . body . removeChild ( element ) ;
302+ }
303+
214304/**
215305 * Gets the users input and creates
216306 * a set of tables from it.
@@ -260,6 +350,8 @@ $("#go").click(function (){
260350/**
261351 * Prevents users submitting the form
262352 * with the enter key.
353+ *
354+ * @param {type } event description
263355 */
264356$ ( document ) . on ( "keypress" , "form" , function ( event ) {
265357 return event . keyCode !== 13 ;
0 commit comments