vue 实现可以合并拆分插入行和列的表格

时间: 2024-05-15 07:19:24 浏览: 20
以下是一个可以合并拆分插入行和列的表格的 Vue 实现示例: ```html <template> <table> <thead> <tr> <th></th> <th v-for="col in cols">{{ col }}</th> </tr> </thead> <tbody> <tr v-for="(row, rowIndex) in rows" :key="rowIndex"> <td>{{ row }}</td> <td v-for="(col, colIndex) in cols" :key="colIndex"> <span v-if="!isMerged(rowIndex, colIndex)">{{ data[rowIndex][colIndex] }}</span> <span v-else>{{ getMergedValue(rowIndex, colIndex) }}</span> </td> </tr> </tbody> </table> </template> <script> export default { data() { return { rows: ["row1", "row2", "row3"], cols: ["col1", "col2", "col3"], data: [ ["data1", "data2", "data3"], ["data4", "data5", "data6"], ["data7", "data8", "data9"] ], mergedCells: [ { startRow: 0, endRow: 1, startCol: 1, endCol: 2, value: "merged1" }, { startRow: 1, endRow: 2, startCol: 0, endCol: 1, value: "merged2" } ] }; }, methods: { isMerged(rowIndex, colIndex) { return this.mergedCells.some( cell => rowIndex >= cell.startRow && rowIndex <= cell.endRow && colIndex >= cell.startCol && colIndex <= cell.endCol ); }, getMergedValue(rowIndex, colIndex) { const mergedCell = this.mergedCells.find( cell => rowIndex >= cell.startRow && rowIndex <= cell.endRow && colIndex >= cell.startCol && colIndex <= cell.endCol ); return mergedCell ? mergedCell.value : ""; }, merge() { const selectedCells = this.getSelectedCells(); if (selectedCells.length > 1) { const startRow = Math.min(...selectedCells.map(cell => cell.row)); const endRow = Math.max(...selectedCells.map(cell => cell.row)); const startCol = Math.min(...selectedCells.map(cell => cell.col)); const endCol = Math.max(...selectedCells.map(cell => cell.col)); const value = this.data[startRow][startCol]; this.mergedCells.push({ startRow, endRow, startCol, endCol, value }); this.removeSelectedCells(selectedCells); } }, split() { const selectedCell = this.getSelectedCells()[0]; if (selectedCell) { const mergedCellIndex = this.mergedCells.findIndex( cell => selectedCell.row >= cell.startRow && selectedCell.row <= cell.endRow && selectedCell.col >= cell.startCol && selectedCell.col <= cell.endCol ); if (mergedCellIndex !== -1) { const mergedCell = this.mergedCells[mergedCellIndex]; this.mergedCells.splice(mergedCellIndex, 1); for (let row = mergedCell.startRow; row <= mergedCell.endRow; row++) { for (let col = mergedCell.startCol; col <= mergedCell.endCol; col++) { if (row !== selectedCell.row || col !== selectedCell.col) { this.data[row][col] = ""; } } } } } }, insertRow() { const selectedCell = this.getSelectedCells()[0]; if (selectedCell) { const rowIndex = selectedCell.row; this.rows.splice(rowIndex + 1, 0, `newRow${this.rows.length}`); this.data.splice(rowIndex + 1, 0, new Array(this.cols.length).fill("")); this.mergedCells.forEach(cell => { if (cell.startRow >= rowIndex) { cell.startRow++; cell.endRow++; } else if (cell.startRow <= rowIndex && cell.endRow >= rowIndex) { const newCell = { ...cell }; newCell.endRow++; this.mergedCells.push(newCell); } }); } }, insertCol() { const selectedCell = this.getSelectedCells()[0]; if (selectedCell) { const colIndex = selectedCell.col; this.cols.splice(colIndex + 1, 0, `newCol${this.cols.length}`); this.data.forEach(row => row.splice(colIndex + 1, 0, "")); this.mergedCells.forEach(cell => { if (cell.startCol >= colIndex) { cell.startCol++; cell.endCol++; } else if (cell.startCol <= colIndex && cell.endCol >= colIndex) { const newCell = { ...cell }; newCell.endCol++; this.mergedCells.push(newCell); } }); } }, deleteRow() { const selectedCell = this.getSelectedCells()[0]; if (selectedCell) { const rowIndex = selectedCell.row; this.rows.splice(rowIndex, 1); this.data.splice(rowIndex, 1); this.mergedCells = this.mergedCells.filter( cell => cell.startRow !== rowIndex && cell.endRow !== rowIndex ); this.mergedCells.forEach(cell => { if (cell.startRow > rowIndex) { cell.startRow--; cell.endRow--; } else if (cell.startRow <= rowIndex && cell.endRow >= rowIndex) { const newCell = { ...cell }; newCell.endRow--; this.mergedCells.push(newCell); } }); } }, deleteCol() { const selectedCell = this.getSelectedCells()[0]; if (selectedCell) { const colIndex = selectedCell.col; this.cols.splice(colIndex, 1); this.data.forEach(row => row.splice(colIndex, 1)); this.mergedCells = this.mergedCells.filter( cell => cell.startCol !== colIndex && cell.endCol !== colIndex ); this.mergedCells.forEach(cell => { if (cell.startCol > colIndex) { cell.startCol--; cell.endCol--; } else if (cell.startCol <= colIndex && cell.endCol >= colIndex) { const newCell = { ...cell }; newCell.endCol--; this.mergedCells.push(newCell); } }); } }, getSelectedCells() { const selectedCells = []; const selectedRows = Array.from(this.$el.querySelectorAll(".selected-row")); selectedRows.forEach(selectedRow => { const row = selectedRow.getAttribute("data-row"); const selectedCols = Array.from(selectedRow.querySelectorAll(".selected-col")); selectedCols.forEach(selectedCol => { const col = selectedCol.getAttribute("data-col"); selectedCells.push({ row: +row, col: +col }); }); }); return selectedCells; }, removeSelectedCells(selectedCells) { selectedCells.forEach(selectedCell => { const selectedRow = this.$el.querySelector(`[data-row="${selectedCell.row}"]`); if (selectedRow) { const selectedCol = selectedRow.querySelector(`[data-col="${selectedCell.col}"]`); if (selectedCol) { selectedCol.classList.remove("selected-col"); if (!Array.from(selectedRow.querySelectorAll(".selected-col")).length) { selectedRow.classList.remove("selected-row"); } } } }); } }, mounted() { document.addEventListener("mouseup", () => { const selectedCells = this.getSelectedCells(); if (selectedCells.length === 1) { const selectedCell = selectedCells[0]; this.$el.querySelectorAll(".selected-row").forEach(selectedRow => { if (selectedRow.getAttribute("data-row") !== String(selectedCell.row)) { selectedRow.classList.remove("selected-row"); } }); this.$el.querySelectorAll(".selected-col").forEach(selectedCol => { if (selectedCol.getAttribute("data-col") !== String(selectedCell.col)) { selectedCol.classList.remove("selected-col"); } }); } }); } }; </script> <style> table { border-collapse: collapse; font-size: 14px; } th, td { border: 1px solid #ccc; padding: 8px; text-align: center; } th { background-color: #eee; } .selected-row td { background-color: #e0e0e0; } .selected-col { background-color: #e0e0e0; } </style> ``` 该示例中,`rows` 和 `cols` 数组分别用于渲染表格的行和列,`data` 数组用于存储每个单元格的数据,`mergedCells` 数组用于存储合并的单元格。`isMerged` 方法用于判断某个单元格是否属于合并的单元格,`getMergedValue` 方法用于获取某个合并的单元格的值。`merge` 方法用于合并选中的单元格,`split` 方法用于拆分选中的合并单元格,`insertRow` 和 `insertCol` 方法用于插入行和列,`deleteRow` 和 `deleteCol` 方法用于删除行和列。`getSelectedCells` 方法用于获取选中的单元格,`removeSelectedCells` 方法用于取消选中的单元格。在 mounted 钩子中,监听鼠标抬起事件,取消多选时的不必要选中状态。 在模板中,使用 `v-for` 渲染表格的行和列,使用 `v-if` 和 `v-else` 显示单元格的值或合并单元格的值。使用 `data-row` 和 `data-col` 属性标记行和列的索引,使用 `selected-row` 和 `selected-col` 类名标记选中的行和列。

相关推荐

最新推荐

recommend-type

Vue实现数据表格合并列rowspan效果

在本文中,我们将深入探讨如何使用Vue框架来实现数据表格中的列合并,特别是利用`rowspan`属性来达到这一效果。在实际应用中,合并列的需求常见于展示一对多关系的数据,例如显示一个学生的所有课程及其成绩。我们将...
recommend-type

Vue多种方法实现表头和首列固定的示例代码

有时表格太大,滚动时信息查看不方便,需要对表格进行全局表头、首列固定, 上效果: 一、创建多个表格进行覆盖 思路:当页面滚动到临界值时,出现固定表头、首列 先创建一个活动表格 &lt;!DOCTYPE html&gt; &...
recommend-type

Vue实现表格批量审核功能实例代码

在Vue.js中实现表格的批量审核功能,主要是通过与用户交互和后端API进行数据交换来完成的。这里我们将详细探讨实现这一功能的关键步骤和涉及的技术点。 首先,前端部分的实现主要涉及到Vue组件的使用,包括表格(`...
recommend-type

vue实现form表单与table表格的数据关联功能示例

在Vue.js中,实现表单(form)与表格(table)的数据关联是一项常见的需求,它涉及到数据双向绑定、事件监听以及动态操作数组等核心概念。本示例将详细讲解如何在Vue环境中完成这一功能。 首先,HTML部分创建了一个...
recommend-type

vue+element 模态框表格形式的可编辑表单实现

在Vue.js框架中,Element UI库提供了一套丰富的UI组件,其中包括模态框(Modal)和表格(Table),这些组件可以方便地构建...通过合理利用这些组件和Vue的响应式特性,我们可以轻松实现模态框内表格形式的可编辑表单。
recommend-type

BSC绩效考核指标汇总 (2).docx

BSC(Balanced Scorecard,平衡计分卡)是一种战略绩效管理系统,它将企业的绩效评估从传统的财务维度扩展到非财务领域,以提供更全面、深入的业绩衡量。在提供的文档中,BSC绩效考核指标主要分为两大类:财务类和客户类。 1. 财务类指标: - 部门费用的实际与预算比较:如项目研究开发费用、课题费用、招聘费用、培训费用和新产品研发费用,均通过实际支出与计划预算的百分比来衡量,这反映了部门在成本控制上的效率。 - 经营利润指标:如承保利润、赔付率和理赔统计,这些涉及保险公司的核心盈利能力和风险管理水平。 - 人力成本和保费收益:如人力成本与计划的比例,以及标准保费、附加佣金、续期推动费用等与预算的对比,评估业务运营和盈利能力。 - 财务效率:包括管理费用、销售费用和投资回报率,如净投资收益率、销售目标达成率等,反映公司的财务健康状况和经营效率。 2. 客户类指标: - 客户满意度:通过包装水平客户满意度调研,了解产品和服务的质量和客户体验。 - 市场表现:通过市场销售月报和市场份额,衡量公司在市场中的竞争地位和销售业绩。 - 服务指标:如新契约标保完成度、续保率和出租率,体现客户服务质量和客户忠诚度。 - 品牌和市场知名度:通过问卷调查、公众媒体反馈和总公司级评价来评估品牌影响力和市场认知度。 BSC绩效考核指标旨在确保企业的战略目标与财务和非财务目标的平衡,通过量化这些关键指标,帮助管理层做出决策,优化资源配置,并驱动组织的整体业绩提升。同时,这份指标汇总文档强调了财务稳健性和客户满意度的重要性,体现了现代企业对多维度绩效管理的重视。
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

【进阶】Flask中的会话与用户管理

![python网络编程合集](https://media.geeksforgeeks.org/wp-content/uploads/20201021201514/pythonrequests.PNG) # 2.1 用户注册和登录 ### 2.1.1 用户注册表单的设计和验证 用户注册表单是用户创建帐户的第一步,因此至关重要。它应该简单易用,同时收集必要的用户信息。 * **字段设计:**表单应包含必要的字段,如用户名、电子邮件和密码。 * **验证:**表单应验证字段的格式和有效性,例如电子邮件地址的格式和密码的强度。 * **错误处理:**表单应优雅地处理验证错误,并提供清晰的错误消
recommend-type

卷积神经网络实现手势识别程序

卷积神经网络(Convolutional Neural Network, CNN)在手势识别中是一种非常有效的机器学习模型。CNN特别适用于处理图像数据,因为它能够自动提取和学习局部特征,这对于像手势这样的空间模式识别非常重要。以下是使用CNN实现手势识别的基本步骤: 1. **输入数据准备**:首先,你需要收集或获取一组带有标签的手势图像,作为训练和测试数据集。 2. **数据预处理**:对图像进行标准化、裁剪、大小调整等操作,以便于网络输入。 3. **卷积层(Convolutional Layer)**:这是CNN的核心部分,通过一系列可学习的滤波器(卷积核)对输入图像进行卷积,以
recommend-type

BSC资料.pdf

"BSC资料.pdf" 战略地图是一种战略管理工具,它帮助企业将战略目标可视化,确保所有部门和员工的工作都与公司的整体战略方向保持一致。战略地图的核心内容包括四个相互关联的视角:财务、客户、内部流程和学习与成长。 1. **财务视角**:这是战略地图的最终目标,通常表现为股东价值的提升。例如,股东期望五年后的销售收入达到五亿元,而目前只有一亿元,那么四亿元的差距就是企业的总体目标。 2. **客户视角**:为了实现财务目标,需要明确客户价值主张。企业可以通过提供最低总成本、产品创新、全面解决方案或系统锁定等方式吸引和保留客户,以实现销售额的增长。 3. **内部流程视角**:确定关键流程以支持客户价值主张和财务目标的实现。主要流程可能包括运营管理、客户管理、创新和社会责任等,每个流程都需要有明确的短期、中期和长期目标。 4. **学习与成长视角**:评估和提升企业的人力资本、信息资本和组织资本,确保这些无形资产能够支持内部流程的优化和战略目标的达成。 绘制战略地图的六个步骤: 1. **确定股东价值差距**:识别与股东期望之间的差距。 2. **调整客户价值主张**:分析客户并调整策略以满足他们的需求。 3. **设定价值提升时间表**:规划各阶段的目标以逐步缩小差距。 4. **确定战略主题**:识别关键内部流程并设定目标。 5. **提升战略准备度**:评估并提升无形资产的战略准备度。 6. **制定行动方案**:根据战略地图制定具体行动计划,分配资源和预算。 战略地图的有效性主要取决于两个要素: 1. **KPI的数量及分布比例**:一个有效的战略地图通常包含20个左右的指标,且在四个视角之间有均衡的分布,如财务20%,客户20%,内部流程40%。 2. **KPI的性质比例**:指标应涵盖财务、客户、内部流程和学习与成长等各个方面,以全面反映组织的绩效。 战略地图不仅帮助管理层清晰传达战略意图,也使员工能更好地理解自己的工作如何对公司整体目标产生贡献,从而提高执行力和组织协同性。