文章

Vue-element实现表格内编辑行功能

Vue+element 实现表格内编辑行功能

Vue+element 实现表格内编辑行功能

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
  <el-button type="primary" @click="save">保存</el-button>
</div>
<el-table :data="tableData" border show-summary :summary-method="getSummaries" style="width: 800px">
  <el-table-column prop="name" label="销售经理"></el-table-column>
  <el-table-column prop="total" label="人员年度目标"></el-table-column>
  <el-table-column prop="amount1" sortable label="1月">
    <template v-slot="scoped">
      <custom-input :show-input="scoped.row.showInput" :value.sync="scoped.row.amount1" @getValue="getValue($event, scoped.$index, 'amount1')"/>
    </template>
  </el-table-column>
  <el-table-column prop="amount2" sortable label="2月">
    <template v-slot="scoped">
      <custom-input :show-input="scoped.row.showInput" :value.sync="scoped.row.amount2" @getValue="getValue($event, scoped.$index, 'amount2')"/>
    </template>
  </el-table-column>
  <el-table-column prop="amount3" sortable label="3月">
    <template v-slot="scoped">
      <custom-input :show-input="scoped.row.showInput" :value.sync="scoped.row.amount3" @getValue="getValue($event, scoped.$index, 'amount3')"/>
    </template>
  </el-table-column>
  <el-table-column label="操作">
    <template v-slot="scoped">
      <el-button type="text" @click="edit(scoped.$index)" v-if="!scoped.row.showInput">edit</el-button>
      <el-button type="text" @click="save(scoped.$index)" v-if="scoped.row.showInput">save</el-button>
      <el-button type="text" @click="cancel(scoped.$index)" v-if="scoped.row.showInput">cancel</el-button>
    </template>
  </el-table-column>
</el-table>

</div> </template>

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
```vue
<template>
  <div class="hello">
    <el-input v-model="inputValue" @input="changeInput" style="width: 80px;" v-if="showInput"/>

    <i v-else>{{ value }}</i>

  </div>
</template>

<script>
export default {
  name: 'HelloWorld',
  props: {
    showInput: {
      type: Boolean,
      default: true,
    },
    value: String
  },
  watch: {
    showInput(val) {
      if (val) {
        this.inputValue = this.value;
      }
    }
  },
  data() {
    return {
      inputValue: '',
    }
  },
  methods: {
    changeInput(val) {
      this.$emit('getValue', val);
    }
  }
}
</script>

<style scoped>

</style>

本文由作者按照 CC BY 4.0 进行授权