BootStrap Table超好用的表格组件基础入门

右侧导航条有目录哟,看着更方便

  • 快速入门
  • 表格构建
  • API简单介绍
  • 主要研究功能介绍

基本JS<script src="https://unpkg.com/bootstrap-table@1.15.2/dist/bootstrap-table.min.js"></script>

注意点:

百科定义:<!DOCTYPE> 声明必须位于 HTML5 文档中的第一行,也就是位于 标签之前。该标签告知浏览器文档所使用的HTML规范。

官方说明: Bootstrap Table需要使用HTML5 doctype。没有它,你会看到一些时髦的不完整造型

快速入门模板,按照这个模板导入文件就可以满足基本需求了

<!doctype html>
<html >
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Hello, Bootstrap Table!</title>

    <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css" integrity="sha384-GJzZqFGwb1QTTN6wy59ffF1BuGJpLSa9DkKMp0DgiMDm4iYMj70gZWKYbI706tWS" crossorigin="anonymous">
    <link rel="stylesheet" href="https://use.fontawesome.com/releases/v5.6.3/css/all.css" integrity="sha384-UHRtZLI+pbxtHCWp1t77Bi1L4ZtiqrqD80Kn4Z8NTSRyMA2Fd33n5dQ8lWUE00s/" crossorigin="anonymous">
    <link rel="stylesheet" href="https://unpkg.com/bootstrap-table@1.15.2/dist/bootstrap-table.min.css">
  </head>
  <body>
    <table data-toggle="table">
      <thead>
        <tr>
          <th>Item ID</th>
          <th>Item Name</th>
          <th>Item Price</th>
        </tr>
      </thead>
      <tbody>
        <tr>
          <td>1</td>
          <td>Item 1</td>
          <td>$1</td>
        </tr>
        <tr>
          <td>2</td>
          <td>Item 2</td>
          <td>$2</td>
        </tr>
      </tbody>
    </table>

    <script src="https://code.jquery.com/jquery-3.3.1.min.js" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8=" crossorigin="anonymous"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js" integrity="sha384-wHAiFfRlMFy6i5SRaxvfOCifBUQy1xHdJ/yoi7FRNXMRBu5WHdZYu1hA6ZOblgut" crossorigin="anonymous"></script>
    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.min.js" integrity="sha384-B0UglyR+jN6CkvvICOB2joaf5I4l3gm9GU6Hc1og6Ls7i6U/mkkaduKaBhlAXv9k" crossorigin="anonymous"></script>
    <script src="https://unpkg.com/bootstrap-table@1.15.2/dist/bootstrap-table.min.js"></script>
  </body>
</html>

表格的构建(自动生成表格的方式)

通过数据直接编写(没啥用)

<table data-toggle="table">
  <thead>
    <tr>
      <th>Item ID</th>
      <th>Item Name</th>
      <th>Item Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>1</td>
      <td>Item 1</td>
      <td>$1</td>
    </tr>
    <tr>
      <td>2</td>
      <td>Item 2</td>
      <td>$2</td>
    </tr>
  </tbody>
</table>

通过数据接口(可以用) Bootstrap会根据数据和表头data-field自动填充表格

<table
  data-toggle="table"
  data-url="data1.json">
  <thead>
    <tr>
      <th data-field="id">Item ID</th>
      <th data-field="name">Item Name</th>
      <th data-field="price">Item Price</th>
    </tr>
  </thead>
</table>

表格属性通过table内的属性设置(设置属性是Bootstrap Table应用的主要方式)

<table
  data-toggle="table"
  data-url="data1.json"
  data-pagination="true"
  data-search="true">
  <thead>
    <tr>
      <th data-sortable="true" data-field="id">Item ID</th>
      <th data-field="name">Item Name</th>
      <th data-field="price">Item Price</th>
    </tr>
  </thead>
</table>

通过JavaScript构建(推荐)

<table ></table>
$(\'#table\').bootstrapTable({
  url: \'data1.json\',
  pagination: true,
  search: true,
  columns: [{
    field: \'id\',
    title: \'Item ID\'
  }, {
    field: \'name\',
    title: \'Item Name\'
  }, {
    field: \'price\',
    title: \'Item Price\'
  }]
})

表格的具体用法API

HTML书写js书写含义默认例子
data-toggletoggle无需编写JavaScript即可激活引导表‘table’https://examples.bootstrap-table.com/#welcomes/from-html.html
data-heightheight表的高度undefinedhttps://examples.bootstrap-table.com/#options/table-height.html
data-classesclasses表的class属性,如果没有自己定义,则默认有边框,并且当鼠标悬浮在那一行,背景会变为浅灰色.table table-hoverhttps://examples.bootstrap-table.com/#options/thead-classes.html

太多了就不一一copy了,官方文档是最全的资料。实在看不懂网页翻译,Nothing is impossible

官网API地址https://bootstrap-table.com/docs/api/table-options/

官网实例地址https://examples.bootstrap-table.com/

主要研究功能介绍

1、固定列

需要导入文件

<link rel="stylesheet" src="extensions/fixed-columns/bootstrap-table-fixed-columns.css">
<script src="extensions/fixed-columns/bootstrap-table-fixed-columns.js"></script>

属性

属性类型默认值介绍
fixedNumberBooleanfalse是否打开固定列
fixedNumberNumber1固定几列

该属性位于扩展中,有可能还不成熟,用的时候遇到了列错位问题

2、列可调整大小(就是列可拖动宽度)

需要导入文件

<script src="extensions/resizable/bootstrap-table-resizable.js"></script>

注意 使用这个插件需要依赖于另一个插件

Use Plugin: bootstrap-table-resizable Dependence: jquery-resizable-columns v0.2.3

属性类型默认值介绍例子
resizableBooleanfalse是否打开列可拖动https://examples.bootstrap-table.com/#extensions/resizable.html

This plugin does not work when data-height is set.

设置data-height时,此插件不起作用。

官网例子源码,点击官网例子左上角View可见

<link href="https://unpkg.com/jquery-resizable-columns@0.2.3/dist/jquery.resizableColumns.css" rel="stylesheet">
<link href="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.css" rel="stylesheet">

<script src="https://unpkg.com/jquery-resizable-columns@0.2.3/dist/jquery.resizableColumns.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/bootstrap-table.min.js"></script>
<script src="https://unpkg.com/bootstrap-table@1.15.3/dist/extensions/resizable/bootstrap-table-resizable.min.js"></script>

<table 
  data-show-columns="true"
  data-search="true"
  data-show-toggle="true"
  data-pagination="true"
  data-url="json/data1.json"
  data-resizable="true">
  <thead>
    <tr>
      <th data-field="id" data-sortable="true">ID</th>
      <th data-field="name" data-sortable="true">Item Name</th>
      <th data-field="price" data-sortable="true">Item Price</th>
    </tr>
  </thead>
</table>

<script>
  $(function() {
    $(\'#table\').bootstrapTable()
  })
</script>

3、修改返回后返回原来分页页面(记住原来页面信息)Table Cookie

需要导入文件

<script src="extensions/cookie/bootstrap-table-cookie.js"></script>
属性类型默认值介绍
cookieBooleanfalse设置true保存表的状态(其分页位置,排序状态,每页记录)

结语:很多创新性的东西目前都在扩展里,还不是特别稳定,就如固定列,选中一条信息,被固定的几列有明显不同,还有经常发生的一件事就是列错位。暂时还不适用于生产环境