bootstrap-table 的常用方法

一、载入插件

1、官方网址:https://bootstrap-table.com
2、官方文档:https://bootstrap-table.com/docs/api/table-options
3、载入,首先基于bootstrap.css 和jquery.js,然后引入如下三个文件即可:

css引入:
<link rel="stylesheet" href="./bootstrap-table/dist/bootstrap-table.min.css">
js引入:
<script src="./bootstrap-table/dist/bootstrap-table.min.js"></script>
<script src="./bootstrap-table/dist/locale/bootstrap-table-zh-CN.js"></script>

二、基本用法
1、数据请求接口:/article/index/
2、返回数据为json格式如下:

{
    total:120,
    rows: {
        0: {id:1, title: "标题一", create_time: "2020-03-15 20:00:00"},
        1: {id:2, title: "标题二", create_time: "2020-03-15 20:00:00"},
        2: {id:3, title: "标题三", create_time: "2020-03-15 20:00:00"},
        ......
    }
}
注: 此为bootstrap-table.js默认接收的数据格式

3、HTML页面:

<table id="idTable" class="table table-striped table-bordered table-hover"  width="100%"></table>

 注意,这里 id="idTable",全页面不能重复

4、js调取渲染页面代码:

$("#itTable").bootstrapTable({
    //三个必须参数: url、sidePagination、columns
    url: "/article/index",
    sidePagination: "server",
    columns: [
        {field: 'state', checkbox: true, align: 'center'},
        {field: 'id', title: 'ID'},
        {field: 'title', title: '文章标题'},
        {field: 'create_time', title: '创建时间'},
        {
            field: 'status', 
            title: '状态', 
            formatter: function(value, row, index){
                if (value == 'normal'){
                    return '正常';
                }else if(value == 'hidden'){
                    return '隐藏';
                }else if(row.create_time < '2020-01-01'){
                    return '已过期';
                }
            }
        },
        {
            field: 'operate',
            title: '操作',
            events: 'operateEvents', //操作按钮绑定事件
            formatter: function(value, row, index){
                var html = [
                    '<a href="#" class="btn btn-xs btn-success btn-editone">编辑</a>',
                    '<a href="javascript:;" class="btn btn-xs btn-danger btn-delone" >删除</a>',
            
                ];
                return html.join('');
            }

        }

    ]
    
});



//操作列按钮绑定事件
window.operateEvents = {
    //删除按钮事件
    "click .btn-delone": function(event, value, row, index){
        .....
     },
    //编辑按钮事件
    "click .btn-editone": function(event, value, row, index){
        ......
    }
};

OK,基本数据显示完成

三、 columns列中常用参数:

四、表格的常用参数

整理中。。。。

 

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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中嵌入日期控件可以通过使用第三方插件来实现。常用的日期控件插件有bootstrap-datepicker和datetimepicker。以下是使用bootstrap-datepicker插件的步骤: 1. 在页面中引入bootstrap-datepicker的css和js文件。 2. 在表格中需要嵌入日期控件的列中添加data-date-format属性,指定日期格式。 3. 在表格初始化时,对需要嵌入日期控件的列进行配置,使用formatter属性指定日期控件的html代码。 4. 在js代码中初始化日期控件,使用datepicker()方法。 下面是一个示例代码: ```html <!-- 引入bootstrap-datepicker的css和js文件 --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/css/bootstrap-datepicker.min.css"> <script src="https://cdnjs.cloudflare.com/ajax/libs/bootstrap-datepicker/1.9.0/js/bootstrap-datepicker.min.js"></script> <table id="table"> <thead> <tr> <th>姓名</th> <th>生日</th> </tr> </thead> <tbody> <tr> <td>张三</td> <td data-date-format="yyyy-mm-dd" data-field="birthday"></td> </tr> <tr> <td>李四</td> <td data-date-format="yyyy-mm-dd" data-field="birthday"></td> </tr> </tbody> </table> <script> $(function() { // 对需要嵌入日期控件的列进行配置 $('#table').bootstrapTable({ columns: [{ field: 'name', title: '姓名' }, { field: 'birthday', title: '生日', formatter: function(value, row, index) { return '<input type="text" class="form-control datepicker" value="' + value + '">'; } }] }); // 初始化日期控件 $('.datepicker').datepicker({ format: 'yyyy-mm-dd', autoclose: true }); }); </script> ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值