接口写好了吗 2023-02-01 09:39 采纳率: 80.2%
浏览 58
已结题

vue表格点击单元格修改数据怎么实现

我是一个表格,左边第一列是名字,后面是日期,然后我点击某一个单元格,弹出一个框,可以在里面拿到当前这个单元格的数据,并且当前单元格式日期几号的,名字是谁的。然后修改单元格的数据发送给后端。最终刷新完成这个表格中的这个单元格数据更新。但是不知道有什么好办法可以点击单元格的时候获取到当前单元格的数据还有行和列的名称。

  • 写回答

3条回答 默认 最新

  • CodeBytes 2023-02-01 09:47
    关注

    参考下面的方法,还望采纳:
    代码如下:

    <template>
      <div>
        <table>
          <tr v-for="(row, index) in rows" :key="index">
            <td v-for="(col, colIndex) in row" :key="colIndex" @click="editCell(index, colIndex)">{{ col }}</td>
          </tr>
        </table>
        <div v-if="showModal">
          <div>
            <label>当前单元格的数据:</label>
            <input v-model="currentCellData" />
          </div>
          <div>
            <label>行名称:</label>
            <input v-model="currentRowName" />
          </div>
          <div>
            <label>列名称:</label>
            <input v-model="currentColName" />
          </div>
          <button @click="saveChanges">保存修改</button>
        </div>
      </div>
    </template>
    
    <script>
    export default {
      data() {
        return {
          rows: [
            ['Name1', 'Date1', 'Data1'],
            ['Name2', 'Date2', 'Data2'],
            ['Name3', 'Date3', 'Data3']
          ],
          showModal: false,
          currentCellData: '',
          currentRowName: '',
          currentColName: '',
          currentRowIndex: 0,
          currentColIndex: 0
        }
      },
      methods: {
        editCell(rowIndex, colIndex) {
          this.showModal = true
          this.currentCellData = this.rows[rowIndex][colIndex]
          this.currentRowName = this.rows[rowIndex][0]
          this.currentColName = this.rows[0][colIndex]
          this.currentRowIndex = rowIndex
          this.currentColIndex = colIndex
        },
        saveChanges() {
          this.rows[this.currentRowIndex][this.currentColIndex] = this.currentCellData
          this.showModal = false
        }
      }
    }
    </script>
    
    
    
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

问题事件

  • 系统已结题 2月9日
  • 已采纳回答 2月1日
  • 创建了问题 2月1日

悬赏问题

  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?