bootstrap table

本文介绍如何利用Bootstrap Table插件在网页中创建动态表格,支持添加、删除和查询功能。详细展示了如何配置分页选项,包括服务器分页和客户端分页的切换方法,以及如何自定义列显示和操作按钮。
<!DOCTYPE html>
<html>
<meta charset="utf-8">
<head th:include="include :: header"></head>
<body class="gray-bg">
    <div class="wrapper wrapper-content ">
        <div class="col-sm-12">
            <div class="ibox">
                <div class="ibox-body">
                    <div class="fixed-table-toolbar">
                        <div class="columns pull-left">
                            <button type="button" class="btn  btn-primary" οnclick="add()">
                                <i class="fa fa-plus" aria-hidden="true"></i>添加
                            </button>
                            <button type="button" class="btn  btn-danger"
                                οnclick="batchRemove()">
                                <i class="fa fa-trash" aria-hidden="true"></i>删除
                            </button>
                        </div>
                        <div class="columns pull-right">
                            <button class="btn btn-success" οnclick="reLoad()">查询</button>
                        </div>
                        <div class="columns pull-right">
                            <input id="searchName" type="text" class="form-control"
                                placeholder="">
                        </div>
                    </div>
                    <table id="exampleTable" data-mobile-responsive="true">
                    </table>
                </div>
            </div>
        </div>
    </div>
    <div th:include="include :: footer"></div>
    <script type="text/javascript"
        src="/js/appjs/common/job/job.js"></script>
</body>
</html>

复制代码

 

JS文件:

复制代码

    $('#exampleTable')
        .bootstrapTable(
            {
                method: 'get', // 服务器数据的请求方式 get or post
                url: prefix + "/list", // 服务器数据的加载地址
                // showRefresh : true,
                // showToggle : true,
                // showColumns : true,
                iconSize: 'outline',
                toolbar: '#exampleToolbar',
                striped: true, // 设置为true会有隔行变色效果
                dataType: "json", // 服务器返回的数据类型
                pagination: true, // 设置为true会在底部显示分页条
                // queryParamsType : "limit",
                // //设置为limit则会发送符合RESTFull格式的参数
                singleSelect: false, // 设置为true将禁止多选
                // contentType : "application/x-www-form-urlencoded",
                // //发送到服务器的数据编码类型
                pageSize: 10, // 如果设置了分页,每页数据条数
                pageNumber: 1, // 如果设置了分布,首页页码
                // search : true, // 是否显示搜索框
                showColumns: false, // 是否显示内容下拉框(选择显示的列)
                sidePagination: "server", // 设置在哪里进行分页,可选值为"client" 或者
                // "server"
                queryParams: function (params) {
                    return {
                        // 说明:传入后台的参数包括offset开始索引,limit步长,sort排序列,order:desc或者,以及所有列的键值对
                        limit: params.limit,
                        offset: params.offset
                        // name:$('#searchName').val(),
                        // username:$('#searchName').val()
                    };
                },
                // //请求服务器数据时,你可以通过重写参数的方式添加一些额外的参数,例如 toolbar 中的参数 如果
                // queryParamsType = 'limit' ,返回参数必须包含
                // limit, offset, search, sort, order 否则, 需要包含:
                // pageSize, pageNumber, searchText, sortName,
                // sortOrder.
                // 返回false将会终止请求
                columns: [
                    {
                        checkbox: true
                    },
                    {
                        field: 'id',
                        title: 'id'
                    },
                    {
                        field: 'jobName',
                        title: '任务名称'
                    },
                    {
                        field: 'jobGroup',
                        title: '任务分组'
                    },
                    {
                        field: 'beanClass',
                        title: '任务类'
                    },
                    {
                        field: 'cronExpression',
                        title: 'cron表达式'
                    },
                    {
                        visible: false,
                        field: 'methodName',
                        title: '方法名称'
                    },
                    {
                        visible: false,
                        field: 'isConcurrent',
                        title: '任务是否有状态'
                    },
                    {
                        visible: false,
                        field: 'description',
                        title: '任务描述'
                    },
                    {
                        visible: false,
                        field: 'updateBy',
                        title: '更新者'
                    },

                    {
                        visible: false,
                        field: 'createDate',
                        title: '创建时间'
                    },


                    {
                        visible: false,
                        field: 'updateDate',
                        title: '更新时间'
                    },
                    {
                        visible: false,
                        field: 'createBy',
                        title: '创建者'
                    },
                    {
                        visible: false,
                        field: 'springBean',
                        title: 'Spring bean'
                    },

                    {
                        field: 'jobStatus',
                        title: '停起操作',
                        formatter: function (value, row, index) {
                            var e = '<a class="btn btn-success btn-xs" href="#" mce_href="#" title="点击开启" οnclick="changeStatus(\''
                                + row.id + '\',\'' + row.jobStatus
                                + '\')"><i class="fa fa-hourglass-start"></i>开启</a> ';
                            var f = '<a class="btn btn-danger btn-xs" href="#" mce_href="#" title="点击关闭" οnclick="changeStatus(\''
                                + row.id + '\',\'' + row.jobStatus
                                + '\')"><i class="fa fa-square-o">关闭</i></a> ';
                            if (row.jobStatus == 0) {
                                return e;
                            } else {
                                return f;
                            }

                        }
                    },

                    {
                        title: '操作',
                        field: 'id',
                        align: 'center',
                        formatter: function (value, row, index) {
                            var e = '<a class="btn btn-primary btn-sm" href="#" mce_href="#" title="编辑" οnclick="edit(\''
                                + row.id + '\',\'' + row.jobStatus
                                + '\')"><i class="fa fa-edit"></i></a> ';
                            var d = '<a class="btn btn-warning btn-sm" href="#" title="删除"  mce_href="#" οnclick="remove(\''
                                + row.id
                                + '\')"><i class="fa fa-remove"></i></a> ';
                            var f = '<a class="btn btn-success btn-sm" href="#" title="开启"  mce_href="#" οnclick="resetPwd(\''
                                + row.id
                                + '\')"><i class="fa fa-key"></i></a> ';
                            return e + d;
                        }
                    }]
            });

复制代码

 

 

效果如下:

 

 

 这里关于分页,特别强调一下:

服务器分页/客户端分页的转换,table刷新

bootstrap默认是客户端分页 ,可通过html标签

data-side-pagination:"client"

或者js中的

sidePagination: 'server'

 

 

指定。注意,这两种后台传过来的json数据格式也不一样 
client : 正常的json array格式 [{},{},{}] 
server: {“total”:0,”rows”:[]} 其中total表示查询的所有数据条数,后面的rows是指当前页面展示的数据量。

有事需要根据情况改变分页方式,就要用到Methods中的 
‘refreshOptions’ //设置更新时候的options 
‘refresh’ //设置更新时的 url ,query(往后台传参数)

 $("#tablelist").bootstrapTable('refreshOptions', {
        sidePagination: 'client'  //改为客户端分页
                });
 $("#tablelist").bootstrapTable('refresh', {
         url: "${ctxAdmin}/user/getsearchuserinfo", //重设数据来源
         query: {username: $('#sea-username').val(),realname: $("#sea-realname").val(),mobile: $("#sea-mobile").val()}//传到后台的参数
                   });
在 Django 项目中实现 Bootstrap Table 的冻结列功能,主要依赖于前端模板中对 Bootstrap Table 插件的配置,以及对静态资源的正确引入。Django 作为基于 Python 的 Web 框架,其核心组件包括对象关系映射、URL 设计、模板语言等,使得开发者能够高效构建数据库驱动的 Web 应用程序[^1]。 为了实现冻结列功能,首先需要在 Django 模板中引入 BootstrapBootstrap Table 的相关资源文件。这些资源可以通过 CDN 快速引入,确保在前端页面中加载必要的 CSS 和 JS 文件,从而支持 Bootstrap Table 的功能扩展。例如: ```html <!-- 引入 Bootstrap CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css"> <!-- 引入 Bootstrap Table CSS --> <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-table@1.21.2/dist/bootstrap-table.min.css"> <!-- 引入 jQuery --> <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script> <!-- 引入 Bootstrap JS --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script> <!-- 引入 Bootstrap Table JS --> <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.21.2/dist/bootstrap-table.min.js"></script> ``` 在 HTML 模板中定义一个 `<table>` 元素,并通过设置 `data-fixed-columns="true"` 和 `data-fixed-number="3"` 来启用冻结前三列的功能。这样可以确保用户在水平滚动表格时,指定的列始终保持在视图左侧,不会随其他列一起滚动。 ```html <table id="table" data-toggle="table" data-url="/api/data/" <!-- 指向你的数据接口 --> data-pagination="true" data-side-pagination="server" data-fixed-columns="true" data-fixed-number="3" class="table table-bordered table-striped" > <thead> <tr> <th data-field="id">ID</th> <th data-field="name">姓名</th> <th data-field="age">年龄</th> <th data-field="address">地址</th> <th data-field="email">邮箱</th> </tr> </thead> </table> ``` 后端方面,Django 需要提供一个返回表格数据的 API 接口。可以使用 Django REST Framework 快速构建数据接口,以支持 Bootstrap Table 的分页、排序等特性。 ```python from rest_framework.decorators import api_view from rest_framework.response import Response from .models import Person from .serializers import PersonSerializer @api_view(['GET']) def get_table_data(request): page = int(request.GET.get('offset', 0)) limit = int(request.GET.get('limit', 10)) queryset = Person.objects.all()[page:page + limit] serializer = PersonSerializer(queryset, many=True) return Response({ 'total': Person.objects.count(), 'rows': serializer.data }) ``` 同时,在 `urls.py` 中配置对应的 URL 路由: ```python from django.urls import path from .views import get_table_data urlpatterns = [ path('api/data/', get_table_data, name='get_table_data'), ] ``` 在使用过程中需要注意以下几点: - **高度限制问题**:如果设置了表格的 `height` 属性,可能会导致冻结列功能异常,因此建议避免设置 `height` 参数。 - **响应式布局**:若需要支持移动端或响应式设计,可以结合 Bootstrap 的响应式表格类 `.table-responsive` 来实现更好的兼容性。 - **浏览器兼容性**:Bootstrap Table 的冻结列功能在现代浏览器中表现良好,但在 Internet Explorer 中可能存在兼容性问题,建议测试并根据需要进行适配。 ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值