web/js/export.js
/**
 * Exports a list of tables into CSV
 * @param {list} table - List of tables to be exported
 * @returns {string} The list of tables converted into CSV
 */
Exports a list of tables into CSV
@param {list} table - List of tables to be exported
@returns {string} The list of tables converted into CSV
Exports a list of tables into CSV @param { list } table - List of tables to be exported @returns { string } The list of tables converted into CSV
function exportTableToCSV($table) {
    switch ($table.length) {
        case 0:
            return '';
            break;
        case 1:
            var $rows = $table.find('tr:has(th,td)'),
                tmpColDelim = String.fromCharCode(11), 
                tmpRowDelim = String.fromCharCode(0), 
                colDelim = '","',
                rowDelim = '"\r\n"',
                csv = '"' + $rows.map(function (i, row) {
                    var $row = $(row),
                        $cols = $row.find('th,td');
                    return $cols.map(function (j, col) {
                        var $col = $(col),
                            text = $col.text();
                        return text.replace('"', '""'); 
                    }).get().join(tmpColDelim);
                }).get().join(tmpRowDelim)
                    .split(tmpRowDelim).join(rowDelim)
                    .split(tmpColDelim).join(colDelim) + '"';
            return csv;
            break;
        default:
            return $.map($table, function(v) {
                    return exportTableToCSV($(v));
                }).join('\n\n');
    }
}
function exportTableToCSV ( $table ) { switch ( $table . length ) { case 0 : return '' ; break ; case 1 : var $rows = $table . find ( 'tr:has(th,td)' ) , tmpColDelim = String . fromCharCode ( 11 ) , tmpRowDelim = String . fromCharCode ( 0 ) , colDelim = '","' , rowDelim = '"\r\n"' , csv = '"' + $rows . map ( function ( i , row ) { var $row = $ ( row ) , $cols = $row . find ( 'th,td' ) ; return $cols . map ( function ( j , col ) { var $col = $ ( col ) , text = $col . text ( ) ; return text . replace ( '"' , '""' ) ; } ) . get ( ) . join ( tmpColDelim ) ; } ) . get ( ) . join ( tmpRowDelim ) . split ( tmpRowDelim ) . join ( rowDelim ) . split ( tmpColDelim ) . join ( colDelim ) + '"' ; return csv ; break ; default : return $ . map ( $table , function ( v ) { return exportTableToCSV ( $ ( v ) ) ; } ) . join ( '\n\n' ) ; } }
Exports a list of tables into CSV
 @param {list} table - List of tables to be exported
 @returns {string} The list of tables converted into CSV
Exports a list of tables into CSV @param { list } table - List of tables to be exported @returns { string } The list of tables converted into CSV
// Temporary delimiter characters unlikely to be typed by keyboard // This is to avoid accidentally splitting the actual contents // vertical tab character // null character // actual delimiter characters for CSV format // Grab text from table into CSV formatted string // escape double quotes