Bootstrap Table 直接编辑示例 (Bootstrap V3 V5)

Bootstrap V3 代码:

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  3.  
  4. <head>
  5.     <meta charset="UTF-8">
  6.     <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7.     <title>Bootstrap Table 示例 (Bootstrap 3)</title>
  8.     <!– Bootstrap 3 CSS –>
  9.     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  10.     <!– Bootstrap Table CSS –>
  11.     <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap-table.min.css">
  12.     <!– Bootstrap Table Editable CSS –>
  13.     <link rel="stylesheet"
  14.        href="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap3-editable/css/bootstrap-editable.css">
  15.     <!– Bootstrap Table Export CSS –>
  16.     <style>
  17.         .toolbar {
  18.             margin-bottom: 10px;
  19.         }
  20.     </style>
  21. </head>
  22.  
  23. <body>
  24.     <div class="container-fluid">
  25.         <h2>Bootstrap Table 示例 (Bootstrap 3)</h2>
  26.  
  27.         <div class="toolbar">
  28.             <button id="addRow" class="btn btn-primary">添加行</button>
  29.             <button id="removeRow" class="btn btn-danger">删除选中行</button>
  30.         </div>
  31.  
  32.         <table id="table">
  33.         </table>
  34.     </div>
  35.  
  36.     <!– jQuery –>
  37.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.min.js"></script>
  38.     <!– Bootstrap 3 JS –>
  39.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>
  40.     <!– Bootstrap Table JS –>
  41.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap-table.min.js"></script>
  42.     <!– Bootstrap Table 中文语言包 –>
  43.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/locale/bootstrap-table-zh-CN.min.js"></script>
  44.     <!– Bootstrap Table Editable JS –>
  45.     <script
  46.        src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap3-editable/js/bootstrap-editable.min.js"></script>
  47.     <script
  48.        src="https://cdn.jsdelivr.net/npm/[email protected]/dist/extensions/editable/bootstrap-table-editable.min.js"></script>
  49.     <!– Bootstrap Table Export JS –>
  50.     <script src="https://cdn.jsdelivr.net/npm/[email protected]/tableExport.min.js"></script>
  51.     <script
  52.        src="https://cdn.jsdelivr.net/npm/[email protected]/dist/extensions/export/bootstrap-table-export.min.js"></script>
  53.  
  54.     <script>
  55.         $(document).ready(function () {
  56.             // 准备数据
  57.             var data = [
  58.                 { id: 1, name: ‘张三’, position: ‘前端工程师’, office: ‘北京’, age: 28, startDate: ‘2020-03-15’, salary: ‘12000’ },
  59.                 { id: 2, name: ‘李四’, position: ‘后端工程师’, office: ‘上海’, age: 32, startDate: ‘2019-07-01’, salary: ‘15000’ },
  60.                 { id: 3, name: ‘王五’, position: ‘全栈工程师’, office: ‘深圳’, age: 26, startDate: ‘2021-02-20’, salary: ‘18000’ },
  61.                 { id: 4, name: ‘赵六’, position: ‘产品经理’, office: ‘杭州’, age: 30, startDate: ‘2018-11-10’, salary: ‘20000’ },
  62.                 { id: 5, name: ‘钱七’, position: ‘UI设计师’, office: ‘广州’, age: 25, startDate: ‘2022-01-05’, salary: ‘10000’ },
  63.                 { id: 6, name: ‘孙八’, position: ‘前端工程师’, office: ‘成都’, age: 27, startDate: ‘2020-09-18’, salary: ‘13000’ },
  64.                 { id: 7, name: ‘周九’, position: ‘后端工程师’, office: ‘武汉’, age: 29, startDate: ‘2019-05-12’, salary: ‘16000’ },
  65.                 { id: 8, name: ‘吴十’, position: ‘测试工程师’, office: ‘南京’, age: 24, startDate: ‘2021-08-30’, salary: ‘9000’ }
  66.             ];
  67.  
  68.             // 初始化表格并加载数据
  69.             $(‘#table’).bootstrapTable({
  70.                 data: data,
  71.                 columns: [{
  72.                     checkbox: true,
  73.                     visible: true
  74.                 }, {
  75.                     field: ‘id’,
  76.                     title: ‘ID’,
  77.                     sortable: true,
  78.                     editable: true
  79.                 }, {
  80.                     field: ‘name’,
  81.                     title: ‘姓名’,
  82.                     sortable: true,
  83.                     editable: {
  84.                         type: ‘text’,
  85.                         title: ‘姓名’,
  86.                         validate: function (value) {
  87.                             if (!value) {
  88.                                 return ‘姓名不能为空’;
  89.                             }
  90.                             if (value.length > 20) {
  91.                                 return ‘姓名不能超过20个字符’;
  92.                             }
  93.                         }
  94.                     }
  95.                 }, {
  96.                     field: ‘position’,
  97.                     title: ‘职位’,
  98.                     sortable: true,
  99.                     editable: {
  100.                         type: ‘select’,
  101.                         title: ‘职位’,
  102.                         source: [
  103.                             { value: ‘前端工程师’, text: ‘前端工程师’ },
  104.                             { value: ‘后端工程师’, text: ‘后端工程师’ },
  105.                             { value: ‘全栈工程师’, text: ‘全栈工程师’ },
  106.                             { value: ‘产品经理’, text: ‘产品经理’ },
  107.                             { value: ‘UI设计师’, text: ‘UI设计师’ }
  108.                         ]
  109.                     }
  110.                 }, {
  111.                     field: ‘office’,
  112.                     title: ‘办公室’,
  113.                     sortable: true,
  114.                     editable: true
  115.                 }, {
  116.                     field: ‘age’,
  117.                     title: ‘年龄’,
  118.                     sortable: true,
  119.                     editable: {
  120.                         type: ‘number’,
  121.                         title: ‘年龄’,
  122.                         validate: function (value) {
  123.                             if (value && (value < 18 || value > 100)) {
  124.                                return ‘年龄必须在18-100之间’;
  125.                             }
  126.                         }
  127.                     }
  128.                 }, {
  129.                     field: ‘startDate’,
  130.                     title: ‘入职日期’,
  131.                     sortable: true,
  132.                     editable: {
  133.                         type: ‘date’,
  134.                         title: ‘入职日期’
  135.                     }
  136.                 }, {
  137.                     field: ‘salary’,
  138.                     title: ‘薪资’,
  139.                     sortable: true,
  140.                     editable: {
  141.                         type: ‘text’,
  142.                         title: ‘薪资’,
  143.                         validate: function (value) {
  144.                             if (value && !/^\d+(\.\d{1,2})?$/.test(value)) {
  145.                                return ‘请输入正确的薪资格式’;
  146.                             }
  147.                         }
  148.                     }
  149.                 }, {
  150.                     field: ‘operate’,
  151.                     title: ‘操作’,
  152.                     align: ‘center’,
  153.                     events: window.operateEvents,
  154.                     formatter: operateFormatter
  155.                 }],
  156.                 toolbar: ‘.toolbar’,
  157.                 search: true,
  158.                 showRefresh: true,
  159.                 showToggle: true,
  160.                 showFullscreen: true,
  161.                 showColumns: true,
  162.                 showColumnsToggleAll: true,
  163.                 showExport: true,
  164.                 clickToSelect: true,
  165.                 detailFormatter: detailFormatter,
  166.                 minimumCountColumns: 2,
  167.                 showPaginationSwitch: true,
  168.                 pagination: true,
  169.                 idField: ‘id’,
  170.                 pageList: [10, 25, 50, 100, ‘all’],
  171.                 showFooter: false,
  172.                 sidePagination: ‘client’,
  173.                 responseHandler: responseHandler,
  174.                 exportOptions: {
  175.                     fileName: ‘员工信息表’,
  176.                     ignoreColumn: [0, 8],
  177.                     pdfmake: {
  178.                         enabled: true,
  179.                         docDefinition: {
  180.                             pageOrientation: ‘landscape’
  181.                         }
  182.                     }
  183.                 }
  184.             });
  185.  
  186.             // 添加行按钮事件
  187.             $(‘#addRow’).click(function () {
  188.                 var newRow = {
  189.                     id: $(‘#table’).bootstrapTable(‘getData’).length + 1,
  190.                     name: ”,
  191.                     position: ”,
  192.                     office: ”,
  193.                     age: ”,
  194.                     startDate: ”,
  195.                     salary: ”
  196.                 };
  197.                 $(‘#table’).bootstrapTable(‘append’, newRow);
  198.             });
  199.  
  200.             // 删除行按钮事件
  201.             $(‘#removeRow’).click(function () {
  202.                 var selections = $(‘#table’).bootstrapTable(‘getSelections’);
  203.                 if (selections.length === 0) {
  204.                     alert(‘请先选择要删除的行’);
  205.                     return;
  206.                 }
  207.  
  208.                 var ids = $.map(selections, function (row) {
  209.                     return row.id;
  210.                 });
  211.  
  212.                 $(‘#table’).bootstrapTable(‘remove’, {
  213.                     field: ‘id’,
  214.                     values: ids
  215.                 });
  216.             });
  217.         });
  218.  
  219.         // 操作列格式化
  220.         function operateFormatter(value, row, index) {
  221.             return [
  222.                 ‘<a class="edit" href="javascript:void(0)" title="编辑">‘,
  223.                 ‘<i class="glyphicon glyphicon-edit">编辑</i>‘,
  224.                 ‘</a>  ‘,
  225.                 ‘<a class="remove" href="javascript:void(0)" title="删除">‘,
  226.                 ‘<i class="glyphicon glyphicon-remove">删除</i>‘,
  227.                 ‘</a>‘
  228.             ].join(”);
  229.         }
  230.  
  231.         // 操作事件
  232.         window.operateEvents = {
  233.             ‘click .edit’: function (e, value, row, index) {
  234.                 $(‘#table’).bootstrapTable(‘updateRow’, {
  235.                     index: index,
  236.                     row: row
  237.                 });
  238.             },
  239.             ‘click .remove’: function (e, value, row, index) {
  240.                 $(‘#table’).bootstrapTable(‘remove’, {
  241.                     field: ‘id’,
  242.                     values: [row.id]
  243.                 });
  244.             }
  245.         };
  246.  
  247.         // 详情格式化
  248.         function detailFormatter(index, row) {
  249.             var html = [];
  250.             $.each(row, function (key, value) {
  251.                 html.push(‘<p><b>‘ + key + ‘:</b> ‘ + value + ‘</p>‘);
  252.             });
  253.             return html.join(”);
  254.         }
  255.  
  256.         // 数据响应处理
  257.         function responseHandler(res) {
  258.             return res;
  259.         }
  260.     </script>
  261. </body>
  262.  
  263. </html>

原文作者: BG6RSH | 查看原帖

此条目发表在学习笔记分类目录。将固定链接加入收藏夹。

《Bootstrap Table 直接编辑示例 (Bootstrap V3 V5)》有一条回应

  1. BG6RSH说:

    Bootstrap V5代码:

    HTML5Untitled.html411 line
    <!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 5)</title>    <!-- Bootstrap 5 CSS -->    <link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet">    <!-- Bootstrap Icons -->    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/font/bootstrap-icons.css">    <!-- Bootstrap Table CSS -->    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap-table.min.css">    <!-- Bootstrap Table Fixed Columns CSS -->    <link rel="stylesheet"        href="https://cdn.jsdelivr.net/npm/[email protected]/dist/extensions/fixed-columns/bootstrap-table-fixed-columns.min.css">    <!-- Bootstrap Table Export CSS -->    <style>        .toolbar {            margin-bottom: 10px;        }        .editable-input {            width: 100%;            padding: 0.375rem 0.75rem;            font-size: 1rem;            border: 1px solid #ced4da;            border-radius: 0.375rem;        }        .editable-input:focus {            border-color: #86b7fe;            outline: 0;            box-shadow: 0 0 0 0.25rem rgba(13, 110, 253, 0.25);        }        .edit-actions {            display: flex;            gap: 5px;        }        .edit-btn {            padding: 0.1rem 0.3rem;            font-size: 0.8rem;        }        .editing-row .editable {            display: none;        }        /* 添加编辑状态下的样式 */        .is-editing .edit-btn.edit {            background-color: #198754;            border-color: #198754;            color: white;        }        /* 设置表格容器高度 */        .fixed-table-container {            height: 500px !important;        }    </style></head><body>    <div class="container-fluid mt-3">        <h2>Bootstrap Table 示例 (Bootstrap 5)</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/[email protected]/dist/jquery.min.js"></script>    <!-- Bootstrap 5 JS -->    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>    <!-- Bootstrap Table JS -->    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/bootstrap-table.min.js"></script>    <!-- Bootstrap Table 中文语言包 -->    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/locale/bootstrap-table-zh-CN.min.js"></script>    <!-- Bootstrap Table Fixed Columns JS -->    <script        src="https://cdn.jsdelivr.net/npm/[email protected]/dist/extensions/fixed-columns/bootstrap-table-fixed-columns.min.js"></script>    <!-- Bootstrap Table Export JS -->    <script src="https://cdn.jsdelivr.net/npm/[email protected]/tableExport.min.js"></script>    <script        src="https://cdn.jsdelivr.net/npm/[email protected]/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' },                { id: 9, name: '王五', position: '全栈工程师', office: '深圳', age: 26, startDate: '2021-02-20', salary: '18000' },                { id: 10, name: '周九', position: '后端工程师', office: '武汉', age: 29, startDate: '2019-05-12', salary: '16000' },                { id: 11, name: '张三', position: '前端工程师', office: '北京', age: 28, startDate: '2020-03-15', salary: '12000' },                { id: 12, name: '李四', position: '后端工程师', office: '上海', age: 32, startDate: '2019-07-01', salary: '15000' },                { id: 13, name: '王五', position: '全栈工程师', office: '深圳', age: 26, startDate: '2021-02-20', salary: '18000' },                { id: 14, name: '赵六', position: '产品经理', office: '杭州', age: 30, startDate: '2018-11-10', salary: '20000' },                { id: 15, name: '钱七', position: 'UI设计师', office: '广州', age: 25, startDate: '2022-01-05', salary: '10000' },                { id: 16, name: '孙八', position: '前端工程师', office: '成都', age: 27, startDate: '2020-09-18', salary: '13000' },                { id: 17, name: '周九', position: '后端工程师', office: '武汉', age: 29, startDate: '2019-05-12', salary: '16000' },                { id: 18, name: '吴十', position: '测试工程师', office: '南京', age: 24, startDate: '2021-08-30', salary: '9000' },                { id: 19, name: '钱七', position: 'UI设计师', office: '广州', age: 25, startDate: '2022-01-05', salary: '10000' },                { id: 20, name: '孙八', position: '前端工程师', office: '成都', age: 27, startDate: '2020-09-18', salary: '13000' },                { id: 21, name: '张三', position: '前端工程师', office: '北京', age: 28, startDate: '2020-03-15', salary: '12000' },                { id: 22, name: '李四', position: '后端工程师', office: '上海', age: 32, startDate: '2019-07-01', salary: '15000' },                { id: 23, name: '王五', position: '全栈工程师', office: '深圳', age: 26, startDate: '2021-02-20', salary: '18000' },                { id: 24, name: '赵六', position: '产品经理', office: '杭州', age: 30, startDate: '2018-11-10', salary: '20000' },                { id: 25, name: '钱七', position: 'UI设计师', office: '广州', age: 25, startDate: '2022-01-05', salary: '10000' },                { id: 26, name: '孙八', position: '前端工程师', office: '成都', age: 27, startDate: '2020-09-18', salary: '13000' },                { id: 27, name: '周九', position: '后端工程师', office: '武汉', age: 29, startDate: '2019-05-12', salary: '16000' },                { id: 28, 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, // 是否可排序                    fixed: 'left'   // 固定第一列                }, {                    field: 'name',                    title: '姓名',                    sortable: true,                    formatter: editFormatter,   // 单元格格式化函数,用于自定义显示内容                    fixed: 'left'  // 固定第二列                }, {                    field: 'position',                    title: '职位',                    sortable: true,                    formatter: editFormatter                }, {                    field: 'office',                    title: '办公室',                    sortable: true,                    formatter: editFormatter                }, {                    field: 'age',                    title: '年龄',                    sortable: true,                    formatter: editFormatter                }, {                    field: 'startDate',                    title: '入职日期',                    sortable: true,                    formatter: editFormatter                }, {                    field: 'salary',                    title: '薪资',                    sortable: true,                    formatter: editFormatter                }, {                    field: 'operate',                    title: '操作',                    align: 'center',                    events: window.operateEvents,   // 列内元素的事件处理对象,绑定操作事件                    formatter: operateFormatter,                    fixed: 'right'  // 列固定位置,可选 'left'(左固定) 或 'right'(右固定)                }],                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',               // 分页方式,'client'(客户端) 或 'server'(服务器端)                responseHandler: responseHandler,       // 服务器响应数据处理函数                fixedColumns: true,        // 启用固定列功能                fixedNumber: 2,            // 固定前2列                fixedRightNumber: 1,       // 固定右侧1列                height: 500,               // 设置表格高度以启用固定表头                exportOptions: {           // 用于配置数据导出功能的相关参数                    fileName: '员工信息表',                    ignoreColumn: [0, 8],   // 导出时需要忽略的列索引数组,忽略第0列和第8列的数据导出                    pdfmake: {              // PDF生成配置选项                        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);                // 延迟执行以确保数据已添加                setTimeout(function () {                    // 获取总页数并跳转到最后一页                    var options = $('#table').bootstrapTable('getOptions');                    var totalRows = options.totalRows;                    var pageSize = options.pageSize;                    var lastPage = Math.ceil(totalRows / pageSize);                    $('#table').bootstrapTable('selectPage', lastPage);                }, 100);            });            // 删除行按钮事件            $('#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 editFormatter(value, row, index, field) {            return [                '<span class="editable" data-field="' + field + '" data-index="' + index + '">' + (value || '') + '</span>'            ].join('');        }        // 操作列格式化        function operateFormatter(value, row, index) {            // 检查当前行是否处于编辑状态            if (row.isEditing) {                return [                    '<a class="save btn btn-sm btn-success edit-btn" href="javascript:void(0)" title="保存">',                    '保存',                    '</a> ',                    '<a class="cancel btn btn-sm btn-secondary edit-btn" href="javascript:void(0)" title="取消">',                    '取消',                    '</a>'                ].join('');            } else {                return [                    '<a class="edit btn btn-sm btn-outline-primary edit-btn" href="javascript:void(0)" title="编辑">',                    '编辑',                    '</a> ',                    '<a class="remove btn btn-sm btn-outline-danger edit-btn" href="javascript:void(0)" title="删除">',                    '删除',                    '</a>'                ].join('');            }        }        // 操作事件        window.operateEvents = {            'click .edit': function (e, value, row, index) {                startEdit(row, index);                console.log('编辑', index, row);            },            'click .save': function (e, value, row, index) {                saveEdit(row, index);                console.log('保存', index, row);            },            'click .cancel': function (e, value, row, index) {                cancelEdit(row, index);                console.log('取消', index, row);            },            'click .remove': function (e, value, row, index) {                $('#table').bootstrapTable('remove', {                    field: 'id',                    values: [row.id]                });                console.log('删除', index, row);            }        };        // 开始编辑        function startEdit(row, index) {            // 获取表格当前数据            var data = $('#table').bootstrapTable('getData');            // 创建一个干净的原始数据副本            var originalData = $.extend(true, {}, data[index]);            // 确保移除所有与编辑状态相关的属性            delete originalData.isEditing;            delete originalData.originalData;            // 保存原始数据副本            data[index].originalData = originalData;            // 标记行为编辑状态            data[index].isEditing = true;            // 更新行以显示编辑控件            $('#table').bootstrapTable('updateRow', {                index: index,                row: createEditRow(data[index], index)            });        }        // 保存编辑        function saveEdit(row, index) {            var rowData = $('#table').bootstrapTable('getData')[index];            // 获取编辑后的值            var $tr = $('#table').find('tbody tr[data-index="' + index + '"]');            $tr.find('input, select').each(function () {                var field = $(this).data('field');                if (field) {                    rowData[field] = $(this).val();                }            });            // 移除编辑状态标记和原始数据            delete rowData.isEditing;            delete rowData.originalData;            // 更新行            $('#table').bootstrapTable('updateRow', {                index: index,                row: rowData            });        }        // 取消编辑        function cancelEdit(row, index) {            // 获取表格当前数据            var data = $('#table').bootstrapTable('getData');            // 获取原始数据            var originalRow = data[index].originalData;            // 确保移除编辑状态标记            originalRow.isEditing = false;            delete originalRow.originalData;            // 恢复原始数据            $('#table').bootstrapTable('updateRow', {                index: index,                row: originalRow            });            console.log('取消', index, originalRow);        }        // 创建编辑行        function createEditRow(row, index) {            var editRow = $.extend(true, {}, row);            editRow.name = '<input type="text" class="editable-input" data-field="name" value="' + (row.name || '') + '">';            editRow.position = '<select class="editable-input" data-field="position">' +                '<option value="前端工程师" ' + (row.position === '前端工程师' ? 'selected' : '') + '>前端工程师</option>' +                '<option value="后端工程师" ' + (row.position === '后端工程师' ? 'selected' : '') + '>后端工程师</option>' +                '<option value="全栈工程师" ' + (row.position === '全栈工程师' ? 'selected' : '') + '>全栈工程师</option>' +                '<option value="产品经理" ' + (row.position === '产品经理' ? 'selected' : '') + '>产品经理</option>' +                '<option value="UI设计师" ' + (row.position === 'UI设计师' ? 'selected' : '') + '>UI设计师</option>' +                '<option value="测试工程师" ' + (row.position === '测试工程师' ? 'selected' : '') + '>测试工程师</option>' +                '</select>';            editRow.office = '<input type="text" class="editable-input" data-field="office" value="' + (row.office || '') + '">';            editRow.age = '<input type="number" class="editable-input" data-field="age" value="' + (row.age || '') + '">';            editRow.startDate = '<input type="date" class="editable-input" data-field="startDate" value="' + (row.startDate || '') + '">';            editRow.salary = '<input type="text" class="editable-input" data-field="salary" value="' + (row.salary || '') + '">';            return editRow;        }        // 详情格式化        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>
    Original Codebox Plus © 2014 o0johntam0o (Huynh Buu Tam)phpBB 3.3 version © 2026 Salvo CortesianoGitHub repository

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

这个站点使用 Akismet 来减少垃圾评论。了解你的评论数据如何被处理。