Bootstrap Table 直接编辑示例 (Bootstrap V3 V5)
发表于 : 周五 7月 25, 2025 4:01 pm
Bootstrap V3 代码:
Code: [全选] [Expand/Collapse]
- <!DOCTYPE html>
- <html lang="zh-CN">
- <head>
- <meta charset="UTF-8">
- <meta name="viewport" content="width=device-width, initial-scale=1.0">
- <title>Bootstrap Table 示例 (Bootstrap 3)</title>
- <!-- Bootstrap 3 CSS -->
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/css/bootstrap.min.css">
- <!-- Bootstrap Table CSS -->
- <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/bootstrap-table.min.css">
- <!-- Bootstrap Table Editable CSS -->
- <link rel="stylesheet"
- href="https://cdn.jsdelivr.net/npm/x-editable@1.5.1/dist/bootstrap3-editable/css/bootstrap-editable.css">
- <!-- Bootstrap Table Export CSS -->
- <style>
- .toolbar {
- margin-bottom: 10px;
- }
- </style>
- </head>
- <body>
- <div class="container-fluid">
- <h2>Bootstrap Table 示例 (Bootstrap 3)</h2>
- <div class="toolbar">
- <button id="addRow" class="btn btn-primary">添加行</button>
- <button id="removeRow" class="btn btn-danger">删除选中行</button>
- </div>
- <table id="table">
- </table>
- </div>
- <!-- jQuery -->
- <script src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js"></script>
- <!-- Bootstrap 3 JS -->
- <script src="https://cdn.jsdelivr.net/npm/bootstrap@3.4.1/dist/js/bootstrap.min.js"></script>
- <!-- Bootstrap Table JS -->
- <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/bootstrap-table.min.js"></script>
- <!-- Bootstrap Table 中文语言包 -->
- <script src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/locale/bootstrap-table-zh-CN.min.js"></script>
- <!-- Bootstrap Table Editable JS -->
- <script
- src="https://cdn.jsdelivr.net/npm/x-editable@1.5.1/dist/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
- <script
- src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/extensions/editable/bootstrap-table-editable.min.js"></script>
- <!-- Bootstrap Table Export JS -->
- <script src="https://cdn.jsdelivr.net/npm/tableexport.jquery.plugin@1.28.0/tableExport.min.js"></script>
- <script
- src="https://cdn.jsdelivr.net/npm/bootstrap-table@1.22.1/dist/extensions/export/bootstrap-table-export.min.js"></script>
- <script>
- $(document).ready(function () {
- // 准备数据
- var data = [
- { id: 1, name: '张三', position: '前端工程师', office: '北京', age: 28, startDate: '2020-03-15', salary: '12000' },
- { id: 2, name: '李四', position: '后端工程师', office: '上海', age: 32, startDate: '2019-07-01', salary: '15000' },
- { id: 3, name: '王五', position: '全栈工程师', office: '深圳', age: 26, startDate: '2021-02-20', salary: '18000' },
- { id: 4, name: '赵六', position: '产品经理', office: '杭州', age: 30, startDate: '2018-11-10', salary: '20000' },
- { id: 5, name: '钱七', position: 'UI设计师', office: '广州', age: 25, startDate: '2022-01-05', salary: '10000' },
- { id: 6, name: '孙八', position: '前端工程师', office: '成都', age: 27, startDate: '2020-09-18', salary: '13000' },
- { id: 7, name: '周九', position: '后端工程师', office: '武汉', age: 29, startDate: '2019-05-12', salary: '16000' },
- { id: 8, name: '吴十', position: '测试工程师', office: '南京', age: 24, startDate: '2021-08-30', salary: '9000' }
- ];
- // 初始化表格并加载数据
- $('#table').bootstrapTable({
- data: data,
- columns: [{
- checkbox: true,
- visible: true
- }, {
- field: 'id',
- title: 'ID',
- sortable: true,
- editable: true
- }, {
- field: 'name',
- title: '姓名',
- sortable: true,
- editable: {
- type: 'text',
- title: '姓名',
- validate: function (value) {
- if (!value) {
- return '姓名不能为空';
- }
- if (value.length > 20) {
- return '姓名不能超过20个字符';
- }
- }
- }
- }, {
- field: 'position',
- title: '职位',
- sortable: true,
- editable: {
- type: 'select',
- title: '职位',
- source: [
- { value: '前端工程师', text: '前端工程师' },
- { value: '后端工程师', text: '后端工程师' },
- { value: '全栈工程师', text: '全栈工程师' },
- { value: '产品经理', text: '产品经理' },
- { value: 'UI设计师', text: 'UI设计师' }
- ]
- }
- }, {
- field: 'office',
- title: '办公室',
- sortable: true,
- editable: true
- }, {
- field: 'age',
- title: '年龄',
- sortable: true,
- editable: {
- type: 'number',
- title: '年龄',
- validate: function (value) {
- if (value && (value < 18 || value > 100)) {
- return '年龄必须在18-100之间';
- }
- }
- }
- }, {
- field: 'startDate',
- title: '入职日期',
- sortable: true,
- editable: {
- type: 'date',
- title: '入职日期'
- }
- }, {
- field: 'salary',
- title: '薪资',
- sortable: true,
- editable: {
- type: 'text',
- title: '薪资',
- validate: function (value) {
- if (value && !/^\d+(\.\d{1,2})?$/.test(value)) {
- return '请输入正确的薪资格式';
- }
- }
- }
- }, {
- field: 'operate',
- title: '操作',
- align: 'center',
- events: window.operateEvents,
- formatter: operateFormatter
- }],
- toolbar: '.toolbar',
- search: true,
- showRefresh: true,
- showToggle: true,
- showFullscreen: true,
- showColumns: true,
- showColumnsToggleAll: true,
- showExport: true,
- clickToSelect: true,
- detailFormatter: detailFormatter,
- minimumCountColumns: 2,
- showPaginationSwitch: true,
- pagination: true,
- idField: 'id',
- pageList: [10, 25, 50, 100, 'all'],
- showFooter: false,
- sidePagination: 'client',
- responseHandler: responseHandler,
- exportOptions: {
- fileName: '员工信息表',
- ignoreColumn: [0, 8],
- pdfmake: {
- enabled: true,
- docDefinition: {
- pageOrientation: 'landscape'
- }
- }
- }
- });
- // 添加行按钮事件
- $('#addRow').click(function () {
- var newRow = {
- id: $('#table').bootstrapTable('getData').length + 1,
- name: '',
- position: '',
- office: '',
- age: '',
- startDate: '',
- salary: ''
- };
- $('#table').bootstrapTable('append', newRow);
- });
- // 删除行按钮事件
- $('#removeRow').click(function () {
- var selections = $('#table').bootstrapTable('getSelections');
- if (selections.length === 0) {
- alert('请先选择要删除的行');
- return;
- }
- var ids = $.map(selections, function (row) {
- return row.id;
- });
- $('#table').bootstrapTable('remove', {
- field: 'id',
- values: ids
- });
- });
- });
- // 操作列格式化
- function operateFormatter(value, row, index) {
- return [
- '<a class="edit" href="javascript:void(0)" title="编辑">',
- '<i class="glyphicon glyphicon-edit">编辑</i>',
- '</a> ',
- '<a class="remove" href="javascript:void(0)" title="删除">',
- '<i class="glyphicon glyphicon-remove">删除</i>',
- '</a>'
- ].join('');
- }
- // 操作事件
- window.operateEvents = {
- 'click .edit': function (e, value, row, index) {
- $('#table').bootstrapTable('updateRow', {
- index: index,
- row: row
- });
- },
- 'click .remove': function (e, value, row, index) {
- $('#table').bootstrapTable('remove', {
- field: 'id',
- values: [row.id]
- });
- }
- };
- // 详情格式化
- function detailFormatter(index, row) {
- var html = [];
- $.each(row, function (key, value) {
- html.push('<p><b>' + key + ':</b> ' + value + '</p>');
- });
- return html.join('');
- }
- // 数据响应处理
- function responseHandler(res) {
- return res;
- }
- </script>
- </body>
- </html>