2017-03-03 3 views
0

테이블을 채우고 CSV로 내보내려고합니다. 다음은 내 코드입니다테이블을 CSV로 내보낼 수 없습니다

<html> 
<head> 
    <title></title> 
</head> 
<body> 


<div class="container"> 
    <h1>Build Table</h1> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.1/jquery.min.js"></script> 

    <div> 
     <table id="blacklistgrid"> 
      <tr id="Row1"> 
       <td>Week Number</td> 
       <td>Oranges Sold</td> 
       <td>Apples Sold</td> 
      </tr> 
      <tr class="Row2"> 
       <td> 
        <input type="text"/> 
       </td> 
       <td> 
        <input type="text"/> 
       </td> 
       <td> 
        <input type="text"/> 
       </td> 
      </tr> 
     </table> 
     <button type="button" id="btnAdd">Add Row!</button> 
     <button><a href="#" class="export">Export Table data into Excel</a></button> 
    </div> 

    <script> 
    jQuery(document).ready(function() { 
    jQuery('#btnAdd').click(function() { 
      jQuery(".Row2").clone().appendTo("#blacklistgrid"); 

     }); 
    }); 


$(document).ready(function() { 

    function exportTableToCSV($table, filename) { 

    var $rows = $table.find('tr:has(td)'), 

     // Temporary delimiter characters unlikely to be typed by keyboard 
     // This is to avoid accidentally splitting the actual contents 
     tmpColDelim = String.fromCharCode(11), // vertical tab character 
     tmpRowDelim = String.fromCharCode(0), // null character 

     // actual delimiter characters for CSV format 
     colDelim = '","', 
     rowDelim = '"\r\n"', 

     // Grab text from table into CSV formatted string 
     csv = '"' + $rows.map(function(i, row) { 
     var $row = $(row), 
      $cols = $row.find('td'); 

     return $cols.map(function(j, col) { 
      var $col = $(col), 
      text = $col.text(); 

      return text.replace(/"/g, '""'); // escape double quotes 

     }).get().join(tmpColDelim); 

     }).get().join(tmpRowDelim) 
     .split(tmpRowDelim).join(rowDelim) 
     .split(tmpColDelim).join(colDelim) + '"'; 

    // Deliberate 'false', see comment below 
    if (false && window.navigator.msSaveBlob) { 

     var blob = new Blob([decodeURIComponent(csv)], { 
     type: 'text/csv;charset=utf8' 
     }); 

     // Crashes in IE 10, IE 11 and Microsoft Edge 
     // See MS Edge Issue #10396033 
     // Hence, the deliberate 'false' 
     // This is here just for completeness 
     // Remove the 'false' at your own risk 
     window.navigator.msSaveBlob(blob, filename); 

    } else if (window.Blob && window.URL) { 
     // HTML5 Blob 
     var blob = new Blob([csv], { 
     type: 'text/csv;charset=utf-8' 
     }); 
     var csvUrl = URL.createObjectURL(blob); 

     $(this) 
     .attr({ 
      'download': filename, 
      'href': csvUrl 
     }); 
    } else { 
     // Data URI 
     var csvData = 'data:application/csv;charset=utf-8,' + encodeURIComponent(csv); 

     $(this) 
     .attr({ 
      'download': filename, 
      'href': csvData, 
      'target': '_blank' 
     }); 
    } 
    } 

    // This must be a hyperlink 
    $(".export").on('click', function(event) { 
    // CSV 
    var args = [$('#blacklistgrid'), 'export.csv']; 

    exportTableToCSV.apply(this, args); 

    // If CSV, don't do event.preventDefault() or return false 
    // We actually need this to be a typical hyperlink 
    }); 
}); 


    </script> 

</body> 
</html> 

테이블을 채우고 동적으로 행을 추가 할 수 있습니다. 그러나 채워진 데이터를 CSV 파일로 내보낼 수 없습니다. 내가 도대체 ​​뭘 잘못하고있는 겁니까?

답변

1

입력 요소를 사용하고 있으므로 text() 메서드는 작동하지 않습니다. 기본적으로 요소 사이에는 아무 것도 없습니다. 대신, val() 메서드를 사용하여 해당 입력 요소에서 텍스트를 가져 오려고합니다. 여기에 빠르고 예가 있습니다 : https://jsfiddle.net/dzy5ktv6/

td 대신 input 요소를 선택하도록 선택기를 변경했습니다.

관련 문제