Bootstrap Table表格分页的使用及分页数据(Excel)导出

简介: Bootstrap Table表格分页的使用及分页数据(Excel)导出
步骤:

1:引入Bootstrap Table表格插件相关链接:这里直接拿来用就可以了,如果要下载到本地,可以自行去官网下载。

    <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入bootstrap-table样式 -->
    <link href="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.css" rel="stylesheet">
    <!-- jquery及bootstrapjs -->
    <script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <!-- bootstrap-table.min.js -->
    <script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
    <!-- 引入中文语言包 -->
    <script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>

2:实现一个简单的表格和分页

自己写了一个json格式的数据,进行模拟

3:前端js实现打印(导出)excel表格

4:可复制黏贴的代码:

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <link href="https://cdn.bootcss.com/bootstrap/3.3.6/css/bootstrap.min.css" rel="stylesheet">
    <!-- 引入bootstrap-table样式 -->
    <link href="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.css" rel="stylesheet">
    <!-- jquery及bootstrapjs -->
    <script src="https://cdn.bootcss.com/jquery/2.2.3/jquery.min.js"></script>
    <script src="https://cdn.bootcss.com/bootstrap/3.3.6/js/bootstrap.min.js"></script>
    <!-- bootstrap-table.min.js -->
    <script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/bootstrap-table.min.js"></script>
    <!-- 引入中文语言包 -->
    <script src="https://cdn.bootcss.com/bootstrap-table/1.11.1/locale/bootstrap-table-zh-CN.min.js"></script>
    <body>
        <div class="tools">
            <button type="button" class="btn green" id="excell" onclick="method5('mytab')">导出表格</button>
        </div>
        <table id="mytab" class="table table-hover"></table>
        <script>
            $('#mytab').bootstrapTable({
                url: 'data1.json',
                queryParams: "queryParams",
                toolbar: "#toolbar",
                sidePagination: "true",
                striped: true, // 是否显示行间隔色
                //search : "true",
                uniqueId: "ID",
                pageSize: "5",
                pagination: true, // 是否分页
                sortable: true, // 是否启用排序
                columns: [{
                        field: 'id',
                        title: '登录名'
                    },
                    {
                        field: 'name',
                        title: '昵称'
                    },
                    {
                        field: 'price',
                        title: '角色'
                    },
                    {
                        field: 'price',
                        title: '操作',
                        width: 120,
                        align: 'center',
                        valign: 'middle',
                        formatter: actionFormatter,
                    },
                ]
            });
            //操作栏的格式化
            function actionFormatter(value, row, index) {
                var id = value;
                var result = "";
                result += "<a href='javascript:;' class='btn btn-xs green' onclick=\"EditViewById('" + id + "', view='view')\" title='查看'><span class='glyphicon glyphicon-search'></span></a>";
                result += "<a href='javascript:;' class='btn btn-xs blue' onclick=\"EditViewById('" + id + "')\" title='编辑'><span class='glyphicon glyphicon-pencil'></span></a>";
                result += "<a href='javascript:;' class='btn btn-xs red' onclick=\"DeleteByIds('" + id + "')\" title='删除'><span class='glyphicon glyphicon-remove'></span></a>";
                return result;
            }
            //打印表格
            var idTmr;
            function getExplorer() {
                var explorer = window.navigator.userAgent;
                //ie  
                if(explorer.indexOf("MSIE") >= 0) {
                    return 'ie';
                }
                //firefox  
                else if(explorer.indexOf("Firefox") >= 0) {
                    return 'Firefox';
                }
                //Chrome  
                else if(explorer.indexOf("Chrome") >= 0) {
                    return 'Chrome';
                }
                //Opera  
                else if(explorer.indexOf("Opera") >= 0) {
                    return 'Opera';
                }
                //Safari  
                else if(explorer.indexOf("Safari") >= 0) {
                    return 'Safari';
                }
            }
            function method5(tableid) {
                if(getExplorer() == 'ie') {
                    var curTbl = document.getElementById(tableid);
                    var oXL = new ActiveXObject("Excel.Application");
                    var oWB = oXL.Workbooks.Add();
                    var xlsheet = oWB.Worksheets(1);
                    var sel = document.body.createTextRange();
                    sel.moveToElementText(curTbl);
                    sel.select();
                    sel.execCommand("Copy");
                    xlsheet.Paste();
                    oXL.Visible = true;
                    try {
                        var fname = oXL.Application.GetSaveAsFilename("Excel.xls",
                            "Excel Spreadsheets (*.xls), *.xls");
                    } catch(e) {
                        print("Nested catch caught " + e);
                    } finally {
                        oWB.SaveAs(fname);
                        oWB.Close(savechanges = false);
                        oXL.Quit();
                        oXL = null;
                        idTmr = window.setInterval("Cleanup();", 1);
                    }
                } else {
                    tableToExcel(tableid)
                }
            }
            function Cleanup() {
                window.clearInterval(idTmr);
                CollectGarbage();
            }
            var tableToExcel = (function() {
                var uri = 'data:application/vnd.ms-excel;base64,',
                    template = '<html><head><meta charset="UTF-8"></head><body><table  border="1">{table}</table></body></html>',
                    base64 = function(
                        s) {
                        return window.btoa(unescape(encodeURIComponent(s)))
                    },
                    format = function(s, c) {
                        return s.replace(/{(\w+)}/g, function(m, p) {
                            return c[p];
                        })
                    }
                return function(table, name) {
                    if(!table.nodeType)
                        table = document.getElementById(table)
                    var ctx = {
                        worksheet: name || 'Worksheet',
                        table: table.innerHTML
                    }
                    window.location.href = uri + base64(format(template, ctx))
                }
            })()
        </script>
    </body>
</html>

data1.json

 [  
        {  
            "id": 0,  
            "name": "高渐离",  
            "price": "$0"  
        },  
        {  
            "id": 1,  
            "name": "王昭君",  
            "price": "$1"  
        },  
        {  
            "id": 2,  
            "name": "安琪拉",  
            "price": "$2"  
        },  
        {  
            "id": 3,  
            "name": "蔡文姬",  
            "price": "$3"  
        },  
        {  
            "id": 4,  
            "name": "孙尚香",  
            "price": "$4"  
        },  
        {  
            "id": 5,  
            "name": "甄姬",  
            "price": "$5"  
        },  
        {  
            "id": 6,  
            "name": "妲己",  
            "price": "$6"  
        },  
        {  
            "id": 7,  
            "name": "米莱地",  
            "price": "$7"  
        },  
        {  
            "id": 8,  
            "name": "大乔",  
            "price": "$8"  
        },  
        {  
            "id": 9,  
            "name": "小巧",  
            "price": "$9"  
        },  
        {  
            "id": 10,  
            "name": "张亮",  
            "price": "$10"  
        },  
        {  
            "id": 11,  
            "name": "诸葛亮",  
            "price": "$11"  
        },  
        {  
            "id": 12,  
            "name": "韩信",  
            "price": "$12"  
        },  
        {  
            "id": 13,  
            "name": "兰陵王",  
            "price": "$13"  
        },  
        {  
            "id": 14,  
            "name": "貂蝉",  
            "price": "$14"  
        },  
        {  
            "id": 15,  
            "name": "后裔",  
            "price": "$15"  
        },  
        {  
            "id": 16,  
            "name": "黄忠",  
            "price": "$16"  
        },  
        {  
            "id": 17,  
            "name": "虞姬",  
            "price": "$17"  
        },  
        {  
            "id": 18,  
            "name": "程咬金",  
            "price": "$18"  
        },  
        {  
            "id": 19,  
            "name": "庄周",  
            "price": "$19"  
        },  
        {  
            "id": 20,  
            "name": "项羽",  
            "price": "$20"  
        }  
    ]  

导出当前选中页码的表格(这是第一页的数据)

相关文章
20.6K star!Excel级交互体验!这款开源Web表格神器绝了!
Handsontable 是一款功能强大的 JavaScript 数据表格组件,提供类 Excel 的交互体验。支持实时协作、数据绑定、公式计算等企业级功能,可轻松集成到 React/Vue/Angular 等主流框架。
116 11
|
5月前
|
Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
【10月更文挑战第29天】Java编程如何读取Word文档里的Excel表格,并在保存文本内容时保留表格的样式?
366 5
Probly:开源 AI Excel表格工具,交互式生成数据分析结果与可视化图表
Probly 是一款结合电子表格功能与 Python 数据分析能力的 AI 工具,支持在浏览器中运行 Python 代码,提供交互式电子表格、数据可视化和智能分析建议,适合需要强大数据分析功能又希望操作简便的用户。
329 2
【图片型PDF】批量识别扫描件PDF指定区域局部位置内容,将识别内容导出Excel表格或批量改名文件,基于阿里云OCR对图片型PDF识别改名案例实现
在医疗和政务等领域,图片型PDF文件(如病历、报告、公文扫描件)的处理需求广泛。通过OCR技术识别这些文件中的文字信息,提取关键内容并保存为表格,极大提高了信息管理和利用效率。本文介绍一款工具——咕嘎批量OCR系统,帮助用户快速处理图片型PDF文件,支持区域识别、内容提取、导出表格及批量改名等功能。下载工具后,按步骤选择处理模式、进行区域采样、批量处理文件,几分钟内即可高效完成数百个文件的处理。
255 8
想让Excel表格设计更美观?试试这几款好用工具!
Excel表格设计在项目管理和数据分析中至关重要。本文推荐四款辅助工具:板栗看板、Excel自动图表助手、Think-Cell Chart 和 Power BI,分别在任务管理、图表生成、数据可视化等方面表现突出,帮助你设计出更专业、美观的表格。
249 2
Bootstrap5 分页3
分页的对齐方式可以通过添加不同的类来实现,默认为左对齐,居中对齐需添加 `justify-content-center` 类,右对齐则添加 `justify-content-end` 类。面包屑导航通过 `.breadcrumb` 和 `.breadcrumb-item` 类进行设置,用于显示当前页面在网站中的位置。
Bootstrap5 分页1
Bootstrap 5 提供了简单易用的分页组件。通过在 `&lt;ul&gt;` 元素上添加 `.pagination` 类,并在 `&lt;li&gt;` 元素上添加 `.page-item` 类,以及在 `&lt;a&gt;` 标签上添加 `.page-link` 类,即可实现基本的分页效果。使用 `.active` 类可以高亮显示当前页。
10 PHP结合bootstrap完成分页
路老师分享了如何使用PHP实现CSDN问答列表的分页功能。首先,通过编写 `index.php` 文件,定义数据和分页逻辑。接着,安装 Composer 并配置 PHP 包管理工具,以引入 `jasongrimes/paginator` 分页插件。最后,展示分页效果并确保代码路径正确。下篇将介绍 PHP 和 Web 页面的交互。
48 0
Bootstrap5 分页2
使用 `.disabled` 类可使分页链接不可点击。通过 `.pagination-lg` 和 `.pagination-sm` 类可调整分页条目的大小,分别设置为大字体和小字体。示例代码展示了如何实现这些效果。
Bootstrap5 表格12
使用 .table-responsive 类可以创建响应式表格。当屏幕宽度小于 992px 时,表格会出现水平滚动条;当屏幕宽度大于 992px 时,表格正常显示,没有滚动条。示例代码展示了如何实现这一效果。
AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等