dongsi7067 2014-09-01 15:31
浏览 44

如何使用jQuery数据表插件将所有数据导出到CSV文件

I want to export the all the result data to be export in CSV file, As of now only current page data is saving in CSV file in jQuery data table

  • 写回答

1条回答 默认 最新

  • doulubashang6936 2014-09-01 16:01
    关注

    I'm not sure how you want to "export" the data, but you can create the csv contents easily with each() in jQuery. Given your question states that you're using a jQuery data table, you'll already have jQuery imported.

    Here is a simple table and the code to parse the contents into csv format. There is a textarea of whose value we set the final "export" data to.

    <table id="tbl">
        <tr>
            <th>Col1</th><th>Col2</th><th>Col3</th>
        </tr>
        <tr>
            <td>eggs</td><td>bacon</td><td>milk</td>
        </tr>
        <tr>
            <td>free</td><td>tasty</td><td>food</td>
        </tr>
    </table>
    
    <textarea id="output"></textarea>
    
    <script>
    
    $("#output").html(getTableData($("#tbl")));
    
    function getTableData(table) {
        var data = [];
        table.find('tr').each(function (rowIndex, r) {
            var cols = [];
            $(this).find('th,td').each(function (colIndex, c) {
                cols.push(c.textContent);
            });
            data.push(cols + "
    ");
        });
        return data;
    }
    </script>
    

    Note: we use .html() on the textarea instead of .val() to avoid extraneous, prepended commas on each line after the first.

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘