[Vue/element] el-table implements dynamic addition/insertion/deletion of table rows and editable cells

el-table implements dynamic addition/insertion/deletion of table rows and editable cells

The effect is as follows:

  • Click "Add a new row" to add a new row at the end of the table, and the cell content can be edited
    insert image description here
  • Click the green + button to insert a row after the specified row
    insert image description here
  • Click the red - button to delete the specified row.
    insert image description here
    Principle: The table el-tableis generated through a dynamic cycle tableData. As long as tableDatathe array is added and deleted, the effect can be achieved.
    The element is used here el-table, and the original one can also be used. The reason is the same.
<template>
  <div class="container">
    <el-button
      type="primary"
      size="mini"
      icon="el-icon-circle-plus-outline"
      @click="addRow"
      style="margin-bottom: 20px"
      >新增一行</el-button
    >
    <el-table :data="tableData" border>
      <el-table-column
        label="序号"
        align="center"
        type="index"
        fixed
        sortable
      />

      <el-table-column label="列1" prop="colcumn1" align="center">
        <template slot-scope="scope">
          <div class="flex align-center">
            <el-input v-model="scope.row.colcumn1" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="列2" prop="colcumn1" align="center">
        <template slot-scope="scope">
          <div class="flex align-center">
            <el-input v-model="scope.row.colcumn2" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            type="success"
            icon="el-icon-plus"
            circle
            size="small"
            @click="addRowIndex(scope.$index)"
          ></el-button>
          <el-button
            type="danger"
            icon="el-icon-minus"
            circle
            size="small"
            @click="delRow(scope.$index)"
          ></el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
export default {
      
      
  name: "test",

  data() {
      
      
    return {
      
      
      tableData: [],
    };
  },

  mounted() {
      
      },

  methods: {
      
      
    // 增加一行
    addRow() {
      
      
      const row = {
      
      
        colcumn1: "",
        colcumn2: "",
      };
      this.tableData.push(row);
    },
    // 删除指定行
    delRow(index) {
      
      
      this.tableData.splice(index, 1);
    },
    // 指定位置插入行
    addRowIndex(index) {
      
      
      const row = {
      
      
        colcumn1: "",
        colcumn2: "",
      };
      this.tableData.splice(index + 1, 0, row);
    },
  },
};
</script>

<style scoped>
.container {
      
      
  padding: 50px;
}
</style>

Guess you like

Origin blog.csdn.net/qq_23073811/article/details/131188338