bootstrap-table的基本使用


一、基本使用步骤

1.引入相关资源文件

页面如下:
在这里插入图片描述

代码如下(示例):

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <title>bootstrap-table</title>
    <!--css样式-->
    <link rel="stylesheet" href="css/bootstrap.css">
    <link rel="stylesheet" href="css/bootstrap-table.min.css">
    <!--js样式-->
     <script src="js/jquery-3.5.1.min.js"></script>
    <script src="js/bootstrap.min.js"></script>
    <script src="js/bootstrap-table.min.js"></script>
    <script src="js/bootstrap-table-zh-CN.min.js"></script>
    <%--表格导出脚本引入--%>
    <script src="js/bootstrap-table-export.min.js"></script>
    <script src="js/tableExport.min.js"></script>
    <script src="js/jspdf.min.js"></script>
    <script src="js/jspdf.plugin.autotable.js"></script>
    <%--表格打印脚本--%>
    <script src="js/bootstrap-table-print.min.js"></script>
</head>
<body>
<div id="toolbar">
    <div class="form-inline" role="form">
        <div class="form-group">
            <span>偏移量: </span>
            <input name="offset" class="form-control w70" type="number" value="0">
        </div>
        <div class="form-group">
            <span>限制: </span>
            <input name="limit" class="form-control w70" type="number" value="5">
        </div>
        <div class="form-group">
            <input name="search" class="form-control" type="text" placeholder="搜索">
        </div>
        <div class="form-group">
            <button id="print" class="btn btn-info btn-undefined" type="button" name="print" aria-label="Print" title="Print"><i class="glyphicon glyphicon-print icon-share"></i> 打印</button>
        </div>
        <button id="ok" type="submit" class="btn btn-primary">查询</button>
    </div>
</div>



<div class="container" style="margin-top: 150px">
    <table id="table"></table>
</div>

<script>
    //打印事件
    $("#print").click(function (){
        var printData = $('#table').parent().html();
        window.document.body.innerHTML = printData;
        // 开始打印
        window.print();
        window.location.reload(true);
    })
</script>
<script>
    $("#table").bootstrapTable({
        url: 'user/findAllUser',    //请求路径
        method: 'get', //请求方式
        dataType:"json",    //表格数据类型
        cache:false,    //不缓存
        striped:true,   //是否紧凑
        pagination:true,    //启用分页
        sidePagination: "client", //客户端分页
        sortable:true,  //客户端排序一般排序方式排序字段同用
        sortOrder:"asc",    //排序方式 desc 降序 asc 升序
        // silentSort:true,    //静态排序
        toolbar:"#toolbar", //自定义工具栏
        detailView:true,  //显示详细视图
        detailFormatter:function (index,row){
            var html = []
            $.each(row, function (key, value) {
                html.push('<p><b>' + key + ':</b> ' + value + '</p>')
            })
            return html.join('')
        },  //显示详细视图的数据转换函数
        rowStyle:function (row,index){
            var classes = [
                'info',
                'active',
                'success',
                'danger',
                'warning'
            ]

            if (index % 2 === 0 && index / 2 < classes.length) {
                return {
                    classes: classes[index / 2]
                }
            }
            return {
                css: {
                    color: 'blue'
                }
            }
        },  //定义行样式类型
        iconSize: "undefined",  //定义图标大小可选值 undefined | ls | sm
        showColumnsSearch: true,    //显示列搜索框
        showColumns: true,  //显示选择列
        showColumnsToggleAll: true, //显示列切换全部
        pageList: "[10, 25, 50, 100, 200, All]",    //定义可选的行数
        // pageNumber:"1", //定义在第几页
        // pageSize:"2",   //定义初始化页面大小
        paginationPreText: "上一页",   //上一页文字
        paginationNextText: "下一页",  //下一页文字
        showButtonText: true,   //显示按钮文字
        paginationHAlign: "left",   //页数选择栏在左边
        paginationDetailHAlign: "right",    //页面大小选择在右边
        search:true,    //显示搜索框
        showExport: true,   //显示导出按钮
        exportDataType: "basic",    //默认basic:只导出当前页的表格数据;all:导出所有数据;selected:导出选中的数据
        // exportButton:$("#id"),  //绑定自定义按钮样式和图标
        exportOptions: {
            fileName:"用户数据",    //文件名
            worksheetName:"sheet1", //表格工作区名
            tableName: "文档数据表格",    //表格名
        },  //导出参数设置
        showSearchButton: true, //显示搜索按钮
        showSearchClearButton: true,    //显示搜索清除按钮
        showRefresh: true,  //显示刷新按钮
        showFullscreen: true,   //是否全屏显示
        buttonsClass:"info",    //按钮的类选择器名
        showPaginationSwitch: true, //显示或隐藏分页信息
        clickToSelect:true, //点击行进行选中checkbox
        // buttonsPrefix:"btn btn-sm", //按钮前缀的定义
        // cardView:true,  //表格卡视图
        showToggle: true,   //显示详细信息转换按钮
        columns: [{checkbox: true,},
            {
                title: "序号",
                formatter:function (value,row,index){
                    return index+1;
                }
            },
            {field: 'id', title: 'id', visible: false,switchable:false /*决定是否列切换与否*/},
            {field: 'name',title: '姓名'},
            {field: 'gender',title: '性别'},
            {field: 'age', title: '年龄'},
            {field: 'address', title: '籍贯'},
            {field: 'qq', title: 'QQ'},
            {field: 'email', title: 'email'},
            { field: 'operate',
                title: '操作',
                align: 'center',
                valign: 'middle',
                width: 200,
                events: {
                    'click #edit': function (e, value, row, index) {
                       alert(row.id)
                    },
                    'click #delete': function (e, value, row, index) {
                        deleteInfo(row.id);
                    },
                    'click #add': function (e, value, row, index) {
                    }
                },
                formatter: function (value, row, index) {
                    var result = "";
                    result += '<button id="edit" class="btn btn-info" data-toggle="modal" data-target="#updateBookInfo">编辑</button>';
                    result += '<button id="add" class="btn btn-success" data-toggle="modal" data-target="#addBookInfo" style="margin-left:10px;">添加</button>';
                    result += '<button id="delete" class="btn btn-danger" style="margin-left:10px;">删除</button>';
                    return result;
                }
            }
        ],
    });
</script>
</body>
</html>


详细文档请参考官网:
bootstrap-table官网

JS组件Bootstrap Table使用方法详解 转载 2016年03月21日 15:06:09 标签: Bootstrap Table 最近客户提出需求,想将原有的管理系统,做下优化,通过手机也能很好展现,想到2个方案: a方案:保留原有的页面,新设计一套适合手机的页面,当手机访问时,进入m.zhy.com(手机页面),pc设备访问时,进入www.zhy.com(pc页面) b方案:采用bootstrap框架,替换原有页面,自动适应手机、平板、PC 设备 采用a方案,需要设计一套界面,并且要得重新写适合页面的接口,考虑到时间及成本问题,故项目采用了b方案 一、效果展示 二、BootStrap table简单介绍 bootStrap table 是一个轻量级的table插件,使用AJAX获取JSON格式的数据,其分页和数据填充很方便,支持国际化 三、使用方法 1、引入js、css [js] view plain copy <!--css样式--> <link href="css/bootstrap/bootstrap.min.css" rel="stylesheet"> <link href="css/bootstrap/bootstrap-table.css" rel="stylesheet"> <!--js--> [removed][removed] [removed][removed] [removed][removed] [removed][removed] 2、table数据填充 bootStrap table获取数据有两种方式,一是通过table 的data-url属性指定数据源,二是通过JavaScript初始化表格时指定url来获取数据 [xhtml] view plain copy ... ... [xhtml] view plain copy $('#table').bootstrapTable({ url: 'data.json' }); 第二种方式交第一种而言在处理复杂数据时更为灵活,一般使用第二种方式来进行table数据填充。 [js] view plain copy $(function () { //1.初始化Table var oTable = new TableInit(); oTable.Init(); //2.初始化Button的点击事件 /* var oButtonInit = new ButtonInit(); oButtonInit.Init(); */ }); var TableInit = function () { var oTableInit = new Object(); //初始化Table oTableInit.Init = function () { $('#tradeList').bootstrapTable({ url: '/VenderManager/TradeList', //请求后台的URL(*) method: 'post', //请求方式(*) toolbar: '#toolbar', //工具按钮用哪个容器 striped: true, //是否显示行间隔色 cache: false, //是否使用缓存,默认为true,所以一般情况下需要设置一下这个属性(*) pagination: true, //是否显示分页(*) sortable: false, //是否启用排序 sortOrder: "asc", //排序方式 queryParams: oTableInit.queryParams,//传递参数(*) sidePagination: "server", //分页方式:client客户端分页,server服务端分页(*) pageNumber:1, //初始化加载第一页,默认第一页 pageSize: 50, //每页的记录行数(*) pageList: [10, 25, 50, 100], //可供选择的每页的行数(*) strictSearch: true, clickToSelect: true, //是否启用点击选中行 height: 460, //行高,如果没有设置height属性,表格自动根据记录条数觉得表格高度 uniqueId: "id", //每一行的唯一标识,一般为主键列 cardView: false, //是否显示详细视图 detailView: false, //是否显示父子表 columns: [{ field: 'id', title: '序号' }, { field: 'liushuiid', title: '交易编号' }, { field: 'orderid', title: '订单号' }, { field: 'receivetime', title: '交易时间' }, { field: 'price', title: '金额' }, { field: 'coin_credit', title: '投入硬币' }, { field: 'bill_credit', title: '投入纸币' }, { field: 'changes', title: '找零' }, { field: 'tradetype', title: '交易类型' },{ field: 'goodmachineid', title: '货机号' },{ field: 'inneridname', title: '货道号' },{ field: 'goodsName', title: '商品名称' }, { field: 'changestatus', title: '支付' },{ field: 'sendstatus', title: '出货' },] }); }; //得到查询的参数 oTableInit.queryParams = function (params) { var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的 limit: params.limit, //页面大小 offset: params.offset, //页码 sdate: $("#stratTime").val(), edate: $("#endTime").val(), sellerid: $("#sellerid").val(), orderid: $("#orderid").val(), CardNumber: $("#CardNumber").val(), maxrows: params.limit, pageindex:params.pageNumber, portid: $("#portid").val(), CardNumber: $("#CardNumber").val(), tradetype:$('input:radio[name="tradetype"]:checked').val(), success:$('input:radio[name="success"]:checked').val(), }; return temp; }; return oTableInit; }; field字段必须与服务器端返回的字段对应才会显示出数据。 3、后台获取数据 a、servlet获取数据 [js] view plain copy BufferedReader bufr = new BufferedReader( new InputStreamReader(request.getInputStream(),"UTF-8")); StringBuilder sBuilder = new StringBuilder(""); String temp = ""; while((temp = bufr.readLine()) != null){ sBuilder.append(temp); } bufr.close(); String json = sBuilder.toString(); JSONObject json1 = JSONObject.fromObject(json); String sdate= json1.getString("sdate");//通过此方法获取前端数据 ... b、springMvc Controller里面对应的方法获取数据 [js] view plain copy public JsonResult GetDepartment(int limit, int offset, string orderId, string SellerId,PortId,CardNumber,Success,maxrows,tradetype) { ... } 4、分页(遇到问题最多的) 使用分页,server端返回的数据必须包括rows和total,代码如下: [js] view plain copy ...gblst = SqlADO.getTradeList(sql,pageindex,maxrows); JSONArray jsonData=new JSONArray(); JSONObject jo=null; for (int i=0,len=gblst.size();i<len;i++) { TradeBean tb = gblst.get(i); if(tb==null) { continue; } jo=new JSONObject(); jo.put("id", i+1); jo.put("liushuiid", tb.getLiushuiid()); jo.put("price", String.format("%1.2f",tb.getPrice()/100.0)); jo.put("mobilephone", tb.getMobilephone()); jo.put("receivetime", ToolBox.getYMDHMS(tb.getReceivetime())); jo.put("tradetype", clsConst.TRADE_TYPE_DES[tb.getTradetype()]); jo.put("changestatus", (tb.getChangestatus()!=0)?"成功":"失败"); jo.put("sendstatus", (tb.getSendstatus()!=0)?"成功":"失败"); jo.put("bill_credit", String.format("%1.2f",tb.getBill_credit()/100.0)); jo.put("changes",String.format("%1.2f",tb.getChanges()/100.0)); jo.put("goodroadid", tb.getGoodroadid()); jo.put("SmsContent", tb.getSmsContent()); jo.put("orderid", tb.getOrderid()); jo.put("goodsName", tb.getGoodsName()); jo.put("inneridname", tb.getInneridname()); jo.put("xmlstr", tb.getXmlstr()); jsonData.add(jo); } int TotalCount=SqlADO.getTradeRowsCount(sql); JSONObject jsonObject=new JSONObject(); jsonObject.put("rows", jsonData);//JSONArray jsonObject.put("total",TotalCount);//总记录数 out.print(jsonObject.toString()); ... 5、分页界面内容介绍 前端获取分页数据,代码如下: [js] view plain copy ...oTableInit.queryParams = function (params) { var temp = { //这里的键的名字和控制器的变量名必须一直,这边改动,控制器也需要改成一样的 limit: params.limit, //第几条记录 offset: params.offset, //显示一页多少记录 sdate: $("#stratTime").val(), }; return temp; };... 后端获取分页数据,代码如下: [js] view plain copy ...int pageindex=0; int offset = ToolBox.filterInt(json1.getString("offset")); int limit = ToolBox.filterInt(json1.getString("limit")); if(offset !=0){ pageindex = offset/limit; } pageindex+= 1;//第几页... 以上就是为大家分享的Bootstrap Table使用方法,希望对大家熟练掌握Bootstrap Table使用方法有所帮助。 转自:http://www.jb51.net/article/79033.htm
Bootstrap Table Treegrid 是基于 Bootstrap Table 的扩展插件,用于在表格中显示树形结构数据。下面是使用 Bootstrap Table Treegrid 的步骤: 1. 引入必要的文件 ``` <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap/3.3.7/css/bootstrap.min.css"> <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap-table/1.15.4/bootstrap-table.min.css"> <link rel="stylesheet" href="https://cdn.staticfile.org/bootstrap-table-treegrid/1.11.0/bootstrap-table-treegrid.min.css"> <script src="https://cdn.staticfile.org/jquery/3.5.1/jquery.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap-table/1.15.4/bootstrap-table.min.js"></script> <script src="https://cdn.staticfile.org/bootstrap-table-treegrid/1.11.0/bootstrap-table-treegrid.min.js"></script> ``` 2. 在 HTML 页面中添加表格元素,并设置 data-url 属性为数据源的 URL。例如: ``` <table id="treegrid" data-url="data.json"></table> ``` 3. 在 JavaScript 中初始化表格,启用 Treegrid 插件。例如: ``` $('#treegrid').bootstrapTable({ columns: [{ field: 'id', title: 'ID' }, { field: 'name', title: '名称' }, { field: 'parentId', title: '父级ID' }], treeShowField: 'name', // 指定树形结构显示的字段 parentIdField: 'parentId', // 指定父级ID字段 idField: 'id', // 指定ID字段 treeCollapse: false, // 是否默认折叠树形结构 treeGrid: true, // 启用 Treegrid 插件 expandIcon: 'glyphicon glyphicon-plus', // 展开图标 collapseIcon: 'glyphicon glyphicon-minus' // 折叠图标 }); ``` 4. 数据源的格式需要满足以下要求: * 每个节点需要有一个 ID 字段和一个父级 ID 字段。 * 根节点的父级 ID 字段可以为空。 * 节点之间的层级关系可以通过父级 ID 字段来确定。 以上就是使用 Bootstrap Table Treegrid 的基本步骤,您可以根据实际情况调整参数和数据源格式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值