2022-12-11_bootstrap-table详细使用说明

2022-12-11  本文已影响0人  微笑碧落

前言

1.安装

    <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/css/bootstrap.min.css" rel="stylesheet" >
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-icons@1.7.2/font/bootstrap-icons.css">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.21.1/dist/bootstrap-table.min.css">
    <script src="https://cdn.jsdelivr.net/npm/jquery/dist/jquery.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.1.3/dist/js/bootstrap.bundle.min.js"></script>
    <script src="https://unpkg.com/bootstrap-table@1.21.1/dist/bootstrap-table.min.js"></script>

2.使用

2.1 js方法

<table id="table"></table>
$('#table').bootstrapTable({
  columns: [{
    field: 'id',
    title: 'Item ID'
  }, {
    field: 'name',
    title: 'Item Name'
  }, {
    field: 'price',
    title: 'Item Price'
  }],
  data: [{
    id: 1,
    name: 'Item 1',
    price: '$1'
  }, {
    id: 2,
    name: 'Item 2',
    price: '$2'
  }]
})

3. 列定义columns

columns: [{
            field: 'id',
            title: 'Item ID',
            align: 'left',
            sortable: true,
            titleTooltip: "this is name"
            checkbox: true,
            width: 20,//宽度
            align: "center",//水平
            valign: "middle"//垂直
        }, {
            field: 'name',
            title: 'Item Name'
        }]

3.1列数据自定义formatter

columns: [{
  field: 'info',
  title: '信息',
  formatter:function (value,row,index,field) {
  return row.id + row.name + row.age;
}]
image.png

3.2 表格底部自定义footerFormatter

{
  field: 'name',
  title: 'Item Name',
  footerFormatter:function (data,value) {
     return data.length;
 }
function stockNumFormatter(data) {
    var total = 0;
    var field = this.field;
    for (var i = 0; i < data.length; i++) {
        total = total + parseInt(data[i][field]);
    }
    return "总计:" + total
}

4.数据data

data: [{
            id: 1,
            name: 'Item 1',
            price: '$1'
        }, {
            id: 2,
            name: 'Item 2',
            price: '$2'
        }]

5.服务端分页

{
"total":200,
"rows":[
  {"id":1, "name":"sallency", "age": 26},
  {"id":2, "name":"sallency", "age": 26},
  {"id":3, "name":"sallency", "age": 26},
  {"id":4, "name":"sallency", "age": 26},
  {"id":5, "name":"sallency", "age": 26}]
}

5.1前端

{
  dataField:'rows'//后端返回的数据的键值,默认是rows
  pageNumber: 1//默认显示的页面,默认值是1
  url:'dms/PSSVEZ',
  method:'get',
  dataType: "json",
  contentType: 'application/json,charset=utf-8',
  pageSize: 5,
  pagination: true,//是否分页
  pageList: [5, 10, 20, 50],//分页步进值
  sidePagination: "server",//服务端分页
}

5.2 后端

public String PSSVEZ(Integer limit, Integer offset, String searchText) {}

5.3 传递给后端的查询参数

 queryParams: function getParams(params) {
                //params obj
                params.other = "otherInfo";
                return params;
            },

6. 客户端分页

[{},{},{},]
{
  url:'dms/PSSVEZ',
  method:'get',
  dataType: "json",//期待返回的数据格式
  contentType: 'application/json,charset=utf-8',//请求类型
  pageSize: 5,
  pagination: true,//是否分页
  pageList: [5, 10, 20, 50],//分页步进值
}

7.详细页detail

{
  detailView: true,
  detailFormatter:function (index, row) {
      var html = []
      $.each(row, function (key, value) {
          html.push('<p><b>' + key + ':</b> ' + value + '</p>')
      })
      return html.join('')
  }
}

8. 列选择相关参数

9. 搜索相关参数

10. 按钮相关参数

11. 事件

//row: the record corresponding to the clicked row.
//$element: the tr element.
//field: the field name corresponding to the clicked cell.
{
        onClickRow: function(row, $element,field) {
            //$element是当前tr的jquery对象
            $element.css("background-color", "green");
            alert(row.id);
        },//单击row事件

        onCheck: function (row,$element) {
            $element.css("background-color", "green");
            alert(row.id);
        }
}

12.表格外观自定义

13. 单选或多选框

{
  idField:'id',//id列给checkbox的值
  columns: [{checkbox: true}] //增加一个checkbox列
}

14. 中文支持或界面文字自定义

14.1 中文支持(使用官方语言包)

{
  locale:'zh-CN',
}
<script src="bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
image.png

14.2界面文字自定义

$('#table').bootstrapTable({ 
 //加载数据时提示
formatLoadingMessage:function()
{
return "数据加载中...";
},
//每页显示
formatRecordsPerPage:function(pageNumber)
{
     return pageNumber+'行每页';
  },
//页码
  formatShowingRows:function(pageFrom, pageTo, totalRows)
          {
            return "第"+pageFrom+"-"+pageTo+"行,总共"+totalRows+"";
   },
 
formatSRPaginationPreText :function(){
return '上一页';
},
 
formatSRPaginationPageText:function(page){
return '跳转至'+page;
},
 formatSRPaginationNextText:function(){
return '下一页';
},
 
 formatDetailPagination:function(totalRows)
 {
 return "总计:"+totalRows;
 }
,
formatSearch:function()
{
return  "搜索";
}
,
formatClearSearch:function()
{
return  "清除搜索";
}
 ,
formatNoMatches:function()
{
return  "未查询到相关记录";
} ,
  formatPaginationSwitch:function()
  {
  return "隐藏显示分页";
  }
,
formatPaginationSwitchDown:function()
  {
  return "显示分页";
  },
formatPaginationSwitchUp:function()
  {
  return "隐藏分页";
  } ,
formatRefresh:function()
  {
  return "刷新";
  }  ,
formatToggle:function()
  {
  return "展开";
  }  ,
formatToggleOn:function()
  {
  return "显示卡片视图";
  } ,  
  formatToggleOff :function()
  {
  return "隐藏卡片视图";
  },
formatColumns:function(){
return "列视图";
}
,
formatColumnsToggleAll:function()
{
return 'Toggle all';
}
 ,
formatFullscreen:function()
{
return '全屏';
} ,
formatAllRows function()
{
return '全部';
}
 
});

15. 常用插件

15.1 Table Resizable

<link rel="stylesheet" href="jquery-reiszable-columns/jquery.resizableColumns.css">
<script src="jquery-reiszable-columns/jquery.resizableColumns.min.js"></script>
<script src="bootstrap-table/extensions/resizable/bootstrap-table-resizable.js"></script>
resizable:true //Set true to allow the resize in each column.

15.2 Sticky Header

<link rel="stylesheet" href="extensions/sticky-header/bootstrap-table-sticky-header.css">
<script src="extensions/sticky-header/bootstrap-table-sticky-header.js"></script>
stickyHeader: true //Set true to use sticky header.
stickyHeaderOffsetLeft:50//Set the left offset of the sticky header container. If the body padding left is 60px, this value would be 60
stickyHeaderOffsetRight
stickyHeaderOffsetY

15.3 Toolbar

<script src="extensions/toolbar/bootstrap-table-toolbar.js"></script>
advancedSearch:true,
idTable:'table',//必须指定
formatAdvancedSearch:function () {
  return '高级搜索'; //自定义按钮名称
},

15.4 Print

<script src="extensions/print/bootstrap-table-print.js"></script>
showPrint:true,//显示打印按钮
printSortColumn://排序行的名称
printSortOrder//Valid values: ‘asc’, ‘desc’. Relevant only if printSortColumn is set

15.5 Page Jump To

<link rel="stylesheet" href="extensions/page-jump-to/bootstrap-table-jump-to.css"></style>
<script src="extensions/page-jump-to/bootstrap-table-jump-to.js"></script>
showJumpTo:true //显示跳转框
showJumpToByPages:5//当页面超过20页才显示跳转框

15.6 Export

<script src="tableExport.jquery.plugin-master/tableExport.min.js"></script>
<script src="extensions/export/bootstrap-table-export.js"></script>
showExport:true//显示导出按钮
exportDataType//Export data type, support: 'basic', 'all', 'selected'.
exportFooter:true//是否导出表脚
exportOptions//导出选项。主要是定义文件名
exportOptions: {
    fileName: function () {
       return 'exportName'
    }
 }
exportTypes:Export types, support types: ['json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'].

15.7 Westsyan/bootstrap-advanced-sortable

image.png image.png
<script src="select2/js/select2.js"></script>
<script src="bootstrap-advanced-sortable/js/bootstrap-advanced-sortable-en.js"></script>
<link rel="stylesheet" href="select2/css/select2.css">
<link rel="stylesheet" href="bootstrap-advanced-sortable/css/bootstrap-advanced-sortable.css">
$('#table').bootstrapTable({
   advancedSortable:true, //打开高级筛选
    columns:[{
        field:"testText",
        title:"TestText",
        sortable:"true",//必须打开这个开关,否则无法弹出筛选对话框
        searchType:"text"
    },{
        field:"testRadio",
        title:"TestRadio",
        sortable:"true",
        searchType:"radio",
        searchSelect:["test1","test2","test3"]
    }]
});

参考文章

  1. AJAX POST请求中参数以form data和request payload形式在servlet中的获取方式
  2. Bootstrap table 服务器端分页示例
  3. bootstrap-table官方文档
  4. bootstrap table轻松实现数据表格
  5. bootstrap table中文设置
  6. 表格组件神器:bootstrap table详细使用指南
  7. 设置bootstrap table footerStyle表格底部样式
    8.Westsyan/bootstrap-advanced-sortable
上一篇下一篇

猜你喜欢

热点阅读