BootStrap Table:表格参数

引入BootStrap Table

引入 bootstrap.css 和 bootstrap-table.css 到 <head> 标签下。
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" href="bootstrap.min.css">
<link rel="stylesheet" href="bootstrap-table.min.css">
</head>
引入 jquery.js bootstrap.js 和 bootstrap-table.js 到 <head> 标签下或者在 <body> 标签关闭之前(推荐)。
<body>
//页面内容
<script src="jquery.min.js"></script>
<script src="bootstrap.min.js"></script>
<script src="bootstrap-table.min.js"></script>
<script src="bootstrap-table-zh-CN.min.js"></script>
</body>

表格参数

表格的参数定义在  jQuery.fn.bootstrapTable.defaults
名称 标签 类型 默认 描述
data-toggle String 'table' 不用写 JavaScript 直接启用表格
classes data-classes String 'table table-hover' 设置表格的 class 属性。
'
table-bordered' :有边框(默认)  
'
table-no-bordered' :无边框
sortClass data-sort-class String undefined 被排序的td元素的类名
height data-height Number undefined 定义表格的高度
undefinedText data-undefined-text String '-' 当数据为 undefined 时显示的字符
striped data-striped Boolean false 设置为 true 会有隔行变色效果
sortName data-sort-name String undefined 定义排序列,通过url方式获取数据填写字段名,否则填写下标
sortOrder data-sort-order String 'asc' 定义排序方式,'asc' 或者 'desc'
sortStable data-sort-stable Boolean false 设置为 true 将获得稳定的排序,我们会添加\_position属性到 row 数据中
iconsPrefix data-icons-prefix String 'glyphicon' 定义字体库 ('Glyphicon' or 'fa' for FontAwesome),使用"fa"时需引用 FontAwesome,并且配合 icons 属性实现效果。
Glyphicon 集成于Bootstrap可免费使用,参考: http://glyphicons.com/
FontAwesome 参考: http://fortawesome.github.io/
icons data-icons Object {
  paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
  paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
  refresh: 'glyphicon-refresh icon-refresh'
  toggle: 'glyphicon-list-alt icon-list-alt'
  columns: 'glyphicon-th icon-th'
  detailOpen: 'glyphicon-plus icon-plus'
  detailClose: 'glyphicon-minus icon-minus'
}
自定义图标
columns - Array [] 列配置项,详情请查看 列参数 表格
data - Array [] 加载json格式的数据
ajax data-ajax Function undefined 自定义 AJAX 方法,须实现 jQuery AJAX API
method data-method String 'get' 服务器数据的请求方式 'get' 或 'post'
url data-url String undefined 服务器数据的加载地址
cache data-cache Boolean true 设置为 false 禁用 AJAX 数据缓存
contentType data-content-type String 'application/json' 发送到服务器的数据编码类型
dataType data-data-type String 'json' 服务器返回的数据类型
ajaxOptions data-ajax-options Object {} 提交ajax请求时的附加参数,可用参数列请查看http://api.jquery.com/jQuery.ajax
queryParams data-query-params Function function(params) {
return params;
}
请求服务器数据时,你可以通过重写参数的方式添加一些额外的参数,例如 toolbar 中的参数 如果 queryParamsType = 'limit' ,返回参数必须包含
limit, offset, search, sort, order
否则, 需要包含: 
pageSize, pageNumber, searchText, sortName, sortOrder. 
返回false将会终止请求
queryParamsType data-query-params-type String 'limit' 设置为 'limit' 则会发送符合 RESTFul 格式的参数
responseHandler data-response-handler Function
function(res) {
return res;
}
加载服务器数据之前的处理程序,可以用来格式化数据。
参数:res为从服务器请求到的数据
pagination data-pagination Boolean false 设置为 true 会在表格底部显示分页条。
paginationLoop data-pagination-loop Boolean true 设置为 true 启用分页条无限循环的功能。
onlyInfoPagination data-only-info-pagination Boolean false 设置为 true 只显示总数据数,而不显示分页按钮。需要设置 pagination='true'。
sidePagination data-side-pagination String 'client' 设置在哪里进行分页,可选值为 'client' 或者 'server'。设置 'server'时,必须设置服务器数据地址(url)或者重写ajax方法。
pageNumber data-page-number Number 1 如果设置了分页,首页页码。
pageSize data-page-size Number 10 如果设置了分页,页面数据条数。
pageList data-page-list Array [10, 25, 50, 100, All] 如果设置了分页,设置可供选择的页面数据条数。设置为 All 或者 Unlimited,则显示所有记录。
selectItemName data-select-item-name String 'btSelectItem' radio 或者 checkbox 的字段 name 名。
smartDisplay data-smart-display Boolean true 设置为 true 是程序自动判断显示分页信息和 card 视图。
escape data-escape Boolean false 转义HTML字符串,替换 &, <, >, ", \`, 和 ' 字符。
search data-search Boolean false 是否启用搜索框。
searchOnEnterKey data-search-on-enter-key Boolean false 设置为 true时,按回车触发搜索方法,否则自动触发搜索方法。
strictSearch data-strict-search Boolean false 设置为 true启用全匹配搜索,否则为模糊搜索。
searchText data-search-text String '' 初始化搜索文字。
searchTimeOut data-search-time-out Number 500 设置搜索超时时间。
trimOnSearch data-trim-on-search Boolean true 设置为 true 将自动去掉搜索字符的前后空格。
showHeader data-show-header Boolean true 是否显示列头。
showFooter data-show-footer Boolean false 是否显示列脚。
showColumns data-show-columns Boolean false 是否显示内容列下拉框。
showRefresh data-show-refresh Boolean false 是否显示刷新按钮。
showToggle data-show-toggle Boolean false 是否显示切换视图(table/card)按钮。
showPaginationSwitch data-show-pagination-switch Boolean false 是否显示切换分页按钮。
showFullscreen data-show-fullscreen Boolean false 是否显示全屏按钮。
minimumCountColumns data-minimum-count-columns Number 1 最小隐藏列的数量。
idField data-id-field String undefined 指定主键列。
uniqueId data-unique-id String undefined 对每一行指定唯一标识符。
cardView data-card-view Boolean false 设置为 true将显示card视图,适用于移动设备。否则为table试图,适用于pc端。
detailView data-detail-view Boolean false 设置为 true 可以显示详细页面模式。
detailFormatter data-detail-formatter Function function(index, row) {
return '';
}
格式化详细页面模式的视图。
searchAlign data-search-align String 'right' 指定 搜索框 水平方向的位置。'left' 或 'right'。
buttonsAlign data-buttons-align String 'right' 指定 按钮栏 水平方向的位置。'left' 或 'right'。
toolbarAlign data-toolbar-align String 'left' 指定 toolbar 水平方向的位置。'left' 或 'right'。
paginationVAlign data-pagination-v-align String 'bottom' 指定 分页条 在垂直方向的位置。'top','bottom' 或 'both'。
paginationHAlign data-pagination-h-align String 'right' 指定 分页条 在水平方向的位置。'left' 或 'right'。
paginationDetailHAlign data-pagination-detail-h-align String 'left' 指定 分页详细信息 在水平方向的位置。'left' 或 'right'。
paginationPreText data-pagination-pre-text String '<' 指定分页条中上一页按钮的图标或文字。
paginationNextText data-pagination-next-text String '>' 指定分页条中下一页按钮的图标或文字。
clickToSelect data-click-to-select Boolean false 设置 true 将在点击行时,自动选择 rediobox 和 checkbox。
ignoreClickToSelectOn data-ignore-click-to-select-on Function { return $.inArray(element.tagName, ['A', 'BUTTON']); } 包含一个参数:
element: 点击的元素。
返回 true 是点击事件会被忽略,返回 false 将会自动选中。该选项只有在 clickToSelect 为 true 时才生效。
singleSelect data-single-select Boolean false 设置 true 将禁止多选。
toolbar data-toolbar String undefined 一个jQuery 选择器,指明自定义的 toolbar。例如:
#toolbar, .toolbar.
buttonsToolbar data-buttons-toolbar String | Node undefined 一个jQuery 选择器,指明自定义的 buttons toolbar。例如:
    #buttons-toolbar, .buttons-toolbar 或 DOM 节点。
checkboxHeader data-checkbox-header Boolean true 设置 false 将在列头隐藏全选复选框。
maintainSelected data-maintain-selected Boolean false 设置为 true 在点击分页按钮或搜索按钮时,将记住checkbox的选择项。
sortable data-sortable Boolean true 设置为false 将禁止所有列的排序。
silentSort data-silent-sort Boolean true 设置为 false 将在点击分页按钮时,自动记住排序项。仅在 sidePagination设置为 server时生效。
rowStyle data-row-style Function function(row,index) {
return class;
}
自定义行样式 参数为:
row: 行数据
index: 行下标
返回值可以为class或者css
rowAttributes data-row-attributes Function function(row,index) {
return attributes;
}
自定义行属性 参数为:
row: 行数据
index: 行下标
返回值可以为class或者css 支持所有自定义属性
customSearch data-custom-search Function $.noop 自定义搜索方法来替代内置的搜索功能,它包含一个参数:
        text:搜索文字。
用法示例:
function customSearch(text) {
//Search logic here.
//You must use `this.data` array in order to filter the data. NO use `this.options.data`.
}

customSort data-custom-sort Function
$.noop
自定义排序方法来替代内置的排序功能,它包含一个参数:
sortName: 排序名。
sortOrder: 排序顺序。
用法示例:
function customSort(sortName, sortOrder) {
//Sort logic here.
//You must use `this.data` array in order to sort the data. NO use `this.options.data`.
}

应用示例

无需编写 JavaScript 启用 bootstrap table,仅需对普通的 table 设置 data-toggle="table" 即可。
<table data-toggle="table" data-classes="table table-bordered table-hover" data-striped="true"> 
  <thead> 
    <tr> 
      <th>员工编号</th>  
      <th>员工姓名</th>  
      <th>所在部门</th>  
      <th>武功等级</th> 
    </tr> 
  </thead>  
  <tbody> 
    <tr> 
      <td>89757</td>  
      <td>姬如雪</td>  
      <td>幻音坊</td>  
      <td>24</td> 
    </tr>  
    <tr> 
      <td>89756</td>  
      <td>叶星云</td>  
      <td>天元神宗</td>  
      <td>31</td> 
    </tr>  
    <tr> 
      <td>89755</td>  
      <td>唐三</td>  
      <td>史莱克学院</td>  
      <td>33</td> 
    </tr> 
  </tbody> 
</table>
此外也可以通过设置远程的 url 如 data-url='返回json格式内容的url' 来加载数据。
<table data-toggle="table" data-classes="table table-bordered table-hover" data-url="http://localhost:8080/employee/list" data-striped="true" data-sort-name="id" data-sort-order="desc"> 
  <thead> 
    <tr> 
      <th data-field="id">员工编号</th>  
      <th data-field="name">员工姓名</th>  
      <th data-field="deptName">所在部门</th>  
      <th data-field="level">武功等级</th> 
    </tr> 
  </thead> 
</table>
在 JavaScript 中通过 <table> 的 id 属性来启用 bootstrap table 。
<table id="example_table"/>
$('#example_table').bootstrapTable({
    data: [{
        "id": 89757,
        "name": "姬如雪",
        "deptName": "幻音坊",
        "level": 24
    },
    {
        "id": 89756,
        "name": "叶星云",
        "deptName": "天元神宗",
        "level": 31
    },
    {
        "id": 89755,
        "name": "唐三",
        "deptName": "史莱克学院",
        "level": 33
    }],
    columns: [{
        field: 'id',
        title: '员工编号'
    },
    {
        field: 'name',
        title: '员工姓名'
    },
    {
        field: 'deptName',
        title: '所属部门'
    },
    {
        field: 'level',
        title: '武功等级'
    }]

});
同样地,在JavaScript中也可以通过设置远程的url如url: '返回json格式内容的url'来加载数据。
$('#example_table').bootstrapTable({
    url: 'http://localhost:8080/employee/list',
    method: 'get',
    contentType: 'application/json',
    dataType: 'json',
    columns: [{
        field: 'id',
        title: '员工编号'
    },
    {
        field: 'name',
        title: '员工姓名'
    },
    {
        field: 'deptName',
        title: '所属部门'
    },
    {
        field: 'level',
        title: '武功等级'
    }]
});
各项参数示例:
$(function() {
    $('#example_table').bootstrapTable({
        //设置表格的 class 属性
        classes: 'table table-bordered table-hover',
        //设置表格的高度
        height: 500,
        //设置当数据为 undefined 时显示的字符
        undefinedText: '空',
        //设置表格隔行变色效果
        striped: true,
        //设置表格使用的字体库
        iconsPrefix: 'glyphicon',
        //自定义表格中的图标
        icons: {
            paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
            paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
            refresh: 'glyphicon-refresh icon-refresh',
            toggle: 'glyphicon-list-alt icon-list-alt',
            columns: 'glyphicon-th icon-th',
            detailOpen: 'glyphicon-plus icon-plus',
            detailClose: 'glyphicon-minus icon-minus'
        },
        //设置数据的加载地址
        url: 'http://localhost:8080/employee/list',
        //设置数据的请求方式
        method: 'get',
        //设置发送到服务器的数据编码类型
        contentType: 'application/json',
        //设置服务器返回的数据类型
        dataType: 'json',
        //发送符合 RESTFul 格式的参数
        queryParamsType: 'limit',
        //设置数据请求的额外参数
        queryParams: function(params) {
            var param = {};
            param['offset'] = params.offset; // 页码
            param['limit'] = params.limit; // 条数
            param['search'] = '搜索内容'; // 搜索内容
            param['sort'] = '排序字段'; // 排序字段
            param['order'] = '排序方式'; // 排序方式
            return param;
        },
        //对请求到的数据进行处理
        responseHandler: function(res) {
            for (var index in res) {
                var id = res[index]['id'];
                res[index]['id'] = 'No.' + id;
            };
            res[0]['deptName'] = undefined;
            return res;
        },
        //对返回数据中的特殊字符进行转义
		escape: true,
		//自定义Ajax请求
        //ajax : ajaxReqData(),
		//禁用Ajax数据缓存
        //cache : false,
		//Ajax请求时的附加参数
        //ajaxOptions : {},
        //设置排序列
        sortName: 'level',
        //设置排序方式
        sortOrder: 'desc',
        //自定义Ajax请求
        sortClass: 'sort_class',
        //获得稳定的排序
        sortStable: true,
        //表格底部显示分页条
        pagination: true,
        //启用分页条无限循环的功能
        paginationLoop: true,
        //只显示总数据数
        onlyInfoPagination: true,
        //采用 client 分页模式
        sidePagination: 'client',
        //首页页码
        pageNumber: 1,
        //每页数据条数
        pageSize: 2,
        //设置可供选择的页面数据条数
        pageList: [2, 5, 10],
        //radio 或者 checkbox 的字段 name 名
        selectItemName: 'btSelectItem',
        //自动判断显示分页信息和 card 视图
        smartDisplay: true,
        //启用搜索框
        search: true,
        //按回车触发搜索方法
        searchOnEnterKey: true,
        //e启用全匹配搜索
        strictSearch: true,
        //初始化搜索文字
        searchText: '',
        //设置搜索超时时间
        searchTimeOut: 500,
        //自动去掉搜索字符的前后空格
        trimOnSearch: true,
        //显示列头
        showHeader: true,
        //显示列脚
        showFooter: true,
        //显示内容列下拉框
        showColumns: true,
        //显示刷新按钮
        showRefresh: true,
        //显示切换视图(table/card)按钮
        showToggle: true,
        //显示切换分页按钮
        showPaginationSwitch: true,
        //显示全屏按钮
        showFullscreen: true,
        //最小隐藏列的数量
        minimumCountColumns: 1,
        //指定主键列
        idField: 'id',
        //对每一行指定唯一标识符
        uniqueId: 'id',
        //使用table试图,适用于pc端
        cardView: false,
        //显示详细页面模式
        detailView: true,
        //格式化详细页面模式的视图
        detailFormatter: function(index, row) {
            return '<span style="color:blue;font-weight:bold">员工编号</span>:' + row.id + ',<span style="color:red;font-weight:bold">员工姓名:</span>' + row.name
        },
        //指定 搜索框 水平方向的位置
        searchAlign: 'right',
        //指定 按钮栏 水平方向的位置
        buttonsAlign: 'right',
        //指定 toolbar 水平方向的位置
        toolbarAlign: 'right',
        //指定 分页条 在垂直方向的位置
        paginationVAlign: 'bottom',
        //指定 分页条 在水平方向的位置
        paginationHAlign: 'right',
        //指定 分页详细信息 在水平方向的位置
        paginationDetailHAlign: 'right',
        //指定分页条中上一页按钮的图标或文字
        paginationPreText: '<',
        //指定分页条中下一页按钮的图标或文字
        paginationNextText: '>',
        //点击行时自动选择 rediobox 和 checkbox
        clickToSelect: true,
        //返回 true 时点击事件会被忽略
        ignoreClickToSelectOn: function(ele) {
            return $.inArray(ele.tagName, ['A', 'BUTTON']);
        },
        //禁止多选
        singleSelect: true,
        //自定义 toolbar
        toolbar: '#my_toolbar',
        //自定义 buttons toolbar
        buttonsToolbar: '#my_buttonsToolbar',
        //列头隐藏全选复选框
        checkboxHeader: false,
        //点击分页按钮或搜索按钮时,记住checkbox的选择项
        maintainSelected: true,
        //启用排序
        sortable: true,
		//静默排序
        //silentSort : true,
        //自定义行样式
        rowStyle: function(row, index) {
            return {
                css: {
                    "color": "gray"
                }
            }
        },
        //自定义行属性
        rowAttributes: function(row, index) {
            return {
                css: {
                    "row_index": index
                }
            }
        },
        //自定义搜索方法
        customSearch: function(text) {
            console.log(text);
        },
        //自定义排序方法
        customSort: function customSort(sortName, sortOrder) {
            console.log(sortName);
            console.log(sortOrder);
        },
		//设置显示字段内容
        columns: [{
            field: 'id',
            title: '员工编号'
        },
        {
            field: 'name',
            title: '员工姓名'
        },
        {
            field: 'deptName',
            title: '所属部门'
        },
        {
            field: 'level',
            title: '武功等级'
        }]

    });
});
function ajaxReqData() {
    $.ajax({
        type: "GET",
        url: "http://localhost:8080/employee/list",
        contentType: "application/json;charset=utf-8",
        dataType: "json",
        data: "",
        success: function(data) {
            $('#example_table').bootstrapTable('load', data);
        },
        error: function(data) {
            console.log("数据加载错误");
        }
    })
}


猜你喜欢

转载自blog.csdn.net/pengjunlee/article/details/80630172