el-table动态添加行,删除行

el-table动态添加行,删除行

<template>
  <div class="table-demo">
    <el-button type="primary" @click="addRow" style="margin-bottom: 10px;">
      添加行
    </el-button>
    
    <el-table
      :data="tableData"
      border
      style="width: 100%"
    >
      <el-table-column prop="name" label="姓名" width="180">
        <template #default="scope">
          <el-input v-model="scope.row.name" placeholder="请输入姓名"></el-input>
        </template>
      </el-table-column>
      
      <el-table-column prop="age" label="年龄" width="180">
        <template #default="scope">
          <el-input v-model.number="scope.row.age" type="number" placeholder="请输入年龄"></el-input>
        </template>
      </el-table-column>
      
      <el-table-column prop="address" label="地址">
        <template #default="scope">
          <el-input v-model="scope.row.address" placeholder="请输入地址"></el-input>
        </template>
      </el-table-column>
      
      <el-table-column label="操作" width="120">
        <template #default="scope">
          <el-button 
            type="danger" 
            size="small" 
            @click="deleteRow(scope.$index)"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { ElTable, ElTableColumn, ElButton, ElInput } from 'element-plus'

// 表格数据源
const tableData = ref([
  { name: '张三', age: 20, address: '北京市' },
  { name: '李四', age: 25, address: '上海市' }
])

// 添加行
const addRow = () => {
  tableData.value.push({})
}

// 删除行
const deleteRow = (index) => {
  tableData.value.splice(index, 1)
}
</script>

<style scoped>
.table-demo {
  width: 800px;
  margin: 20px auto;
  padding: 20px;
  border: 1px solid #eee;
  border-radius: 4px;
}
</style>
`el-table` 是 Element UI 中的一个表格组件,它允许你在运动态添加删除或修改列。当你在表格中动态添加新的列并希望保持其高度适应内容时,可能会遇到表格超出屏幕的问题。这是因为表格的高度默认是固定的,如果内容增加导致数增多,超出部分可能会被隐藏。 解决这个问题通常有以下几种方式: 1. **虚拟滚动**(v-loading="virtual" 或者使用 element-ui 的虚拟滚动插件 `el-virtual-scroller`):这可以提高性能并只渲染当前可见区域的数据,超出的部分不会占用额外的空间。 2. **设置高度自适应**:你可以通过监听数据变化,更新表格高度使其随内容调整。例如,你可以计算所有的高度总和再加上表头的高度作为表格容器的 height 属性。 ```html <template> <div ref="tableWrapper" :style="{ height: getTableHeight() + 'px' }"></div> </template> <script> export default { computed: { getTableHeight() { // 计算高总和和表头高度 const headerHeight = ...; const rowHeightsSum = ...; return headerHeight + rowHeightsSum; } }, mounted() { this.updateTableHeight(); }, methods: { updateTableHeight() { this.$nextTick(() => { this.getTableHeight && this.$refs.tableWrapper.style.height = this.getTableHeight() + 'px'; }); }, handleDataChange() { this.updateTableHeight(); } } } ``` 3. **调整 CSS 样式**:你可以设置表格的 overflow 装饰属性(如 overflow-y: auto;),这样超出的内容会显示滚动条。 记得处理好数据加载和渲染的时机,以便在数据准备好后再展示完整的表格。同时,在
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

代码工人笔记

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值