当前位置:   article > 正文

elementui动态添加表格_element ui 动态表格

element ui 动态表格

显示效果

二、代码

全部粘贴上去即可 

  1. <template>
  2. <div>
  3. <el-button @click="addRow">添加行</el-button>
  4. <el-button @click="saveAll" type="primary" style="margin-left: 10px">保存全部</el-button>
  5. <el-table :data="tableData" :empty-text="'没有数据'" size="mini" style="width: 600px">
  6. <el-table-column prop="date" label="日期" width="180">
  7. <template slot-scope="scope">
  8. <el-input
  9. v-model="scope.row.date"
  10. placeholder="请输入日期"
  11. v-show="scope.row.editing"
  12. ></el-input>
  13. <span v-show="!scope.row.editing">{{ scope.row.date }}</span>
  14. </template>
  15. </el-table-column>
  16. <el-table-column prop="name" label="姓名" width="180">
  17. <template slot-scope="scope">
  18. <el-input
  19. v-model="scope.row.name"
  20. placeholder="请输入姓名"
  21. v-show="scope.row.editing"
  22. ></el-input>
  23. <span v-show="!scope.row.editing">{{ scope.row.name }}</span>
  24. </template>
  25. </el-table-column>
  26. <el-table-column prop="food" label="食物">
  27. <template slot-scope="scope">
  28. <el-select
  29. v-model="scope.row.food"
  30. placeholder="请选择食物"
  31. v-show="scope.row.editing"
  32. >
  33. <el-option
  34. v-for="item in options"
  35. :key="item.value"
  36. :label="item.label"
  37. :value="item.value"
  38. ></el-option>
  39. </el-select>
  40. <span v-show="!scope.row.editing">{{ scope.row.food }}</span>
  41. </template>
  42. </el-table-column>
  43. <el-table-column label="操作" width="100">
  44. <template slot-scope="scope">
  45. <el-button
  46. v-if="!scope.row.editing"
  47. @click="startEditing(scope.row)"
  48. type="text"
  49. size="small"
  50. >
  51. 编辑
  52. </el-button>
  53. <el-button
  54. v-else
  55. @click="save(scope.row)"
  56. type="text"
  57. size="small"
  58. >
  59. 保存
  60. </el-button>
  61. </template>
  62. </el-table-column>
  63. </el-table>
  64. </div>
  65. </template>
  66. <script>
  67. export default {
  68. data() {
  69. return {
  70. tableData: [],
  71. options: [
  72. { value: 'apple', label: '苹果' },
  73. { value: 'banana', label: '香蕉' },
  74. { value: 'orange', label: '橙子' }
  75. ]
  76. };
  77. },
  78. methods: {
  79. addRow() {
  80. const newRow = {
  81. date: '',
  82. name: '',
  83. food: '',
  84. editing: true
  85. };
  86. this.tableData.push(newRow);
  87. },
  88. startEditing(row) {
  89. row.editing = true;
  90. },
  91. save(row) {
  92. row.editing = false;
  93. },
  94. saveAll() {
  95. this.tableData.forEach(row => {
  96. row.editing = false;
  97. });
  98. // const payload = this.tableData.map(row => ({
  99. // date: row.date,
  100. // name: row.name,
  101. // food: row.food
  102. // }));
  103. // 发送POST请求到后端API端点
  104. // axios.post('/api/save-data', payload)
  105. // .then(response => {
  106. // // 根据后端的响应进行相应的处理
  107. // console.log(response.data); // 可以打印响应结果或做其他处理
  108. // })
  109. // .catch(error => {
  110. // // 处理请求错误
  111. // console.error(error);
  112. // });
  113. }
  114. }
  115. };
  116. </script>
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/91322
推荐阅读