|
| 1 | +--- |
| 2 | +category: examples |
| 3 | +group: plugin |
| 4 | +title: Excel-like Online Editing |
| 5 | +cover: https://lf9-dp-fe-cms-tos.byteorg.com/obj/bit-cloud/VTable/preview/excel-online-editing.gif |
| 6 | +link: plugin/usage |
| 7 | +option: plugins |
| 8 | +--- |
| 9 | + |
| 10 | +# Excel-like Online Editing |
| 11 | + |
| 12 | +Based on VTable's plugin mechanism, this example implements Excel-like online editing functionality. |
| 13 | + |
| 14 | +In this example, we use the following plugins in combination: |
| 15 | +- `AddRowColumnPlugin`: Add rows and columns |
| 16 | +- `ColumnSeriesPlugin`: Column series plugin |
| 17 | +- `RowSeriesPlugin`: Row series plugin |
| 18 | +- `HighlightHeaderWhenSelectCellPlugin`: Highlight selected cells |
| 19 | +- `ExcelEditCellKeyboardPlugin`: Excel-style cell editing keyboard plugin |
| 20 | + |
| 21 | +It's important to note that there are dependencies between plugins. For example, in the `AddRowColumnPlugin`, we call the `columnSeries.resetColumnCount` method to reset the column count, ensuring that after adding columns, the count remains consistent with the column count in the `ColumnSeriesPlugin`. |
| 22 | + |
| 23 | +## Key Configuration |
| 24 | + |
| 25 | +- `editCellTrigger`: Cell editing trigger method |
| 26 | +- `editor`: Editor type |
| 27 | +- `plugins`: Plugin list |
| 28 | + |
| 29 | + |
| 30 | +## Code Demo |
| 31 | + |
| 32 | +```javascript livedemo template=vtable |
| 33 | +// import * as VTable from '@visactor/vtable'; |
| 34 | +// 使用时需要引入插件包@visactor/vtable-plugins |
| 35 | +// import * as VTablePlugins from '@visactor/vtable-plugins'; |
| 36 | +// 正常使用方式 const columnSeries = new VTable.plugins.ColumnSeriesPlugin({}); |
| 37 | +// 官网编辑器中将 VTable.plugins重命名成了VTablePlugins |
| 38 | + |
| 39 | +// 注册编辑器 |
| 40 | +const input_editor = new VTable_editors.InputEditor(); |
| 41 | +VTable.register.editor('input', input_editor); |
| 42 | + |
| 43 | +// 注册插件 |
| 44 | + const addRowColumn = new VTablePlugins.AddRowColumnPlugin({ |
| 45 | + addColumnCallback: col => { |
| 46 | + // 新增列时,重置列数 |
| 47 | + columnSeries.resetColumnCount(columnSeries.pluginOptions.columnCount + 1); |
| 48 | + // 将table实例中的数据源records每一个数组中新增一个空字符串,对应新增的列 |
| 49 | + const newRecords = tableInstance.records.map(record => { |
| 50 | + if (Array.isArray(record)) { |
| 51 | + record.splice(col - 1, 0, ''); |
| 52 | + } |
| 53 | + return record; |
| 54 | + }); |
| 55 | + tableInstance.setRecords(newRecords); |
| 56 | + }, |
| 57 | + addRowCallback: row => { |
| 58 | + // 新增行时,填充空行数据 |
| 59 | + tableInstance.addRecord([], row - tableInstance.columnHeaderLevelCount); |
| 60 | + } |
| 61 | + }); |
| 62 | + |
| 63 | + const columnSeries = new VTablePlugins.ColumnSeriesPlugin({ |
| 64 | + columnCount: 26 |
| 65 | + }); |
| 66 | + const rowSeries = new VTablePlugins.RowSeriesPlugin({ |
| 67 | + rowCount: 100, |
| 68 | + //records数据以外 填充空行数据 |
| 69 | + fillRowRecord: (index) => { |
| 70 | + return []; |
| 71 | + }, |
| 72 | + rowSeriesNumber: { |
| 73 | + width: 'auto' |
| 74 | + } |
| 75 | + }); |
| 76 | + const highlightPlugin = new VTablePlugins.HighlightHeaderWhenSelectCellPlugin({ |
| 77 | + colHighlight: true, |
| 78 | + rowHighlight: true |
| 79 | + }); |
| 80 | + const excelEditCellKeyboardPlugin = new VTablePlugins.ExcelEditCellKeyboardPlugin(); |
| 81 | + const option = { |
| 82 | + // 二维数组的数据 和excel的行列一致 |
| 83 | + records: [ |
| 84 | + ['姓名', '年龄', '地址'], |
| 85 | + ['张三', 18, '北京'], |
| 86 | + ['李四', 20, '上海'], |
| 87 | + ['王五', 22, '广州'], |
| 88 | + ['赵六', 24, '深圳'], |
| 89 | + ['孙七', 26, '成都'] |
| 90 | + ], |
| 91 | + |
| 92 | + padding: 30, |
| 93 | + editor: 'input', |
| 94 | + editCellTrigger: ['api', 'keydown', 'doubleclick'],// 编辑单元格触发方式 |
| 95 | + select: { |
| 96 | + cornerHeaderSelectMode: 'body', |
| 97 | + headerSelectMode: 'body' |
| 98 | + }, |
| 99 | + theme: VTable.themes.DEFAULT.extends({ |
| 100 | + defaultStyle: { |
| 101 | + textAlign: 'left', |
| 102 | + padding: [2, 6, 2, 6] |
| 103 | + }, |
| 104 | + headerStyle: { |
| 105 | + textAlign: 'center' |
| 106 | + } |
| 107 | + }), |
| 108 | + defaultRowHeight: 30, |
| 109 | + plugins: [addRowColumn, columnSeries, rowSeries, highlightPlugin, excelEditCellKeyboardPlugin] |
| 110 | + }; |
| 111 | + const tableInstance = new VTable.ListTable( document.getElementById(CONTAINER_ID),option); |
| 112 | + window.tableInstance = tableInstance; |
| 113 | + |
| 114 | +``` |
0 commit comments