广告
返回顶部
首页 > 资讯 > 前端开发 > JavaScript >关于BootstrapTable导出数据的问题最终解决方案
  • 386
分享到

关于BootstrapTable导出数据的问题最终解决方案

2024-04-02 19:04:59 386人浏览 泡泡鱼
摘要

要导出的数据:https://examples.bootstrap-table.com/JSON/data1.json?order=asc 使用的插件(注意插件版本依赖):tabl

要导出的数据:https://examples.bootstrap-table.com/JSON/data1.json?order=asc

使用的插件(注意插件版本依赖):tableExport.Jquery.plugin

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>TableExport</title>
    <!--jquery-->
    <script src="Https://cdn.bootCSS.com/jquery/3.4.0/jquery.min.js"></script>
    <!--bootstrap-->
    <link href="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/css/bootstrap.min.css" rel="external nofollow"  rel="stylesheet">
    <script src="https://cdn.bootcss.com/popper.js/1.14.7/umd/popper.min.js"></script>
    <script src="https://cdn.bootcss.com/twitter-bootstrap/4.3.1/js/bootstrap.min.js"></script>
    <!--fontawesome-->
    <script src="https://cdn.bootcss.com/font-awesome/5.8.1/js/all.min.js"></script>
    <!--bootstrap-table-->
    <link href="https://cdn.bootcss.com/bootstrap-table/1.14.2/bootstrap-table.min.css" rel="external nofollow"  rel="stylesheet">
    <script src="https://cdn.bootcss.com/bootstrap-table/1.14.2/bootstrap-table.min.js"></script>
    <!--bootstrap-table-lanuage-->
    <script src="https://cdn.bootcss.com/bootstrap-table/1.14.2/bootstrap-table-locale-all.min.js"></script>
    <!--bootstrap-table-export-->
    <script src="https://cdn.bootcss.com/bootstrap-table/1.14.2/extensions/export/bootstrap-table-export.min.js"></script>
    <!--在客户端保存生成的导出文件-->
    <script src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
    <!--以XLSX(excel 2007+ XML格式)格式导出表(SheetJS)-->
    <script src="https://cdn.bootcss.com/xlsx/0.14.2/xlsx.core.min.js"></script>
    <!--以PNG格式导出表格-->
    <!--对于IE支持包括 html2canvas 之前的 es6-promise-->
    <script src="https://cdn.bootcss.com/es6-promise/4.1.1/es6-promise.auto.min.js"></script>
    <script src="https://cdn.bootcss.com/html2canvas/0.5.0-beta4/html2canvas.min.js"></script>
    <!--将表导出为pdf文件-->
    <script src="https://unpkg.com/tableexport.jquery.plugin/libs/jsPDF/jspdf.min.js"></script>
    <script src="https://unpkg.com/tableexport.jquery.plugin/libs/jsPDF-AutoTable/jspdf.plugin.autotable.js"></script>
    <!--无论期望的格式如何,最后都包含 tableexport.jquery.plugin(不是tableexport)-->
    <script src="https://unpkg.com/tableexport.jquery.plugin/tableExport.min.js"></script>
</head>
<body>
<div class="container">
    <div id="toolbar">
        <select class="fORM-control">
            <option value="">Export Basic</option>
            <option value="all">Export All</option>
            <option value="selected">Export Selected</option>
        </select>
    </div>
    <button type="button" onclick="exportData()" class='btn btn-mini btn-info'>导出</button>
    <table id="table" data-locale="zh-CN"></table>
</div>
<script>
    $(function () {
        $.ajax({
            url: "https://examples.bootstrap-table.com/json/data1.json?order=asc",
            success: function (result) {
                // 初始化表格
                $('#toolbar').find('select').change(function () {
                    $('#table').bootstrapTable('destroy').bootstrapTable({
                        data: result,
                        pagination: true,//显示分页
                        clickToSelect: true,//单击列表选中
                        toolbar: "#toolbar",//显示工具栏
                        showToggle: true,//工具栏上显示列表模式切换
                        showExport: true,//工具栏上显示导出按钮
                        exportDataType: $(this).val(),//显示导出范围
                        exportTypes: ['json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'],//导出格式
                        exportOptions: {//导出设置
                            fileName: 'Tablexxx',//下载文件名称
                        },
                        columns: [
                            {
                                field: 'state',
                                checkbox: true,
                                visible: $(this).val() === 'selected'
                            },
                            {
                                field: 'id',
                                title: 'ID'
                            }, {
                                field: 'name',
                                title: 'Item Name'
                            }, {
                                field: 'price',
                                title: 'Item Price'
                            }
                        ]
                    })
                }).trigger('change');
            }
        });
    })
    // 自定义按钮导出数据
    function exportData(){
        $('#table').tableExport({
            type: 'excel',
            exportDataType: "all",
            ignoreColumn: [0],//忽略某一列的索引
            fileName: 'Tablexxx',//下载文件名称
            onCellHtmlData: function (cell, row, col, data){//处理导出内容,自定义某一行、某一列、某个单元格的内容
                console.info(data);
                return data;
            },
        });
    }
</script>
</body>
</html>

结果

bootstrap-table-export:https://bootstrap-table.com/docs/extensions/export/

tableexport.jquery.plugin CDN:https://unpkg.com/tableexport.jquery.plugin/

到此这篇关于BootstrapTable 导出数据的文章就介绍到这了,更多相关BootstrapTable 导出数据内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!

--结束END--

本文标题: 关于BootstrapTable导出数据的问题最终解决方案

本文链接: https://www.lsjlt.com/news/144344.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作