&l..._vue v-simple-table 增加行 删除行">
当前位置:   article > 正文

vue中用原生table写可编辑表格 , 点击按钮添加行 , 点击序号删除行_vue v-simple-table 增加行 删除行

vue v-simple-table 增加行 删除行

主要功能 :

1 , 一个表格 , 单元格内容是循环渲染的 , 点击每个单元格弹出一个输入框 , 编辑内容 

2 , 点击按钮添加行 , 点击序号删除行 

刚开始想用插件写的 ,网上找了半天,  研究好久搞不出来需要的效果 , 最后还是自己用table标签手写实现的 

  1. <template>
  2. <div>
  3. <table class="tb1">
  4. <tr class="th">
  5. <td></td>
  6. <td>工作事项</td>
  7. <td>工作内容</td>
  8. <td>备注</td>
  9. </tr>
  10. <tr v-for="(item,index) in list" :key="index">
  11. <td @click="del(index)">{{index+1}}</td>
  12. <td>
  13. <input type="text" v-model="item.item" @click="modifyItem(item,index)">
  14. </td>
  15. <td>
  16. <input type="text" v-model="item.content" @click="modifyContent(item,index)">
  17. </td>
  18. <td>
  19. <input type="text" v-model="item.remark" @click="modifyRemark(item,index)">
  20. </td>
  21. </tr>
  22. </table>
  23. <van-button type="info" size="small" @click="addRow" class="addBtn" v-if="this.$store.state.isHeadShow">+ 添加行
  24. (点序号删除行)
  25. </van-button>
  26. <van-button type="info" size="small" class="btn" @click="submit" >提交
  27. </van-button>
  28. <!-- 编辑工作事项 -->
  29. <van-popup v-model="isItemShow" position="bottom" :style="{ height: '65%' }">
  30. <van-nav-bar title="工作事项" left-arrow @click-left="isItemShow=false" right-text="确定" @click-right="confirmItem" />
  31. <van-field v-model="itemTxt" rows="10" autosize type="textarea" placeholder="输入工作事项" :readonly="readonly" />
  32. </van-popup>
  33. <!-- 编辑工作内容 -->
  34. <van-popup v-model="isContentShow" position="bottom" :style="{ height: '65%' }">
  35. <van-nav-bar title="工作内容" left-arrow @click-left="isContentShow=false" right-text="确定" @click-right="confirmContent" />
  36. <van-field v-model="contentTxt" rows="10" autosize type="textarea" placeholder="输入工作内容"
  37. :readonly="readonly" />
  38. </van-popup>
  39. <!-- 编辑备注 -->
  40. <van-popup v-model="isRemarkShow" position="bottom" :style="{ height: '65%' }">
  41. <van-nav-bar title="备注" left-arrow @click-left="isRemarkShow=false" right-text="确定" @click-right="confirmRemark" />
  42. <van-field v-model="remarkTxt" rows="10" autosize type="textarea" placeholder="输入备注" :readonly="readonly" />
  43. </van-popup>
  44. </div>
  45. </template>
  46. <script>
  47. export default {
  48. data() {
  49. return {
  50. index: "",
  51. list: [{
  52. item: "",
  53. content: "",
  54. remark: ""
  55. }, {
  56. item: "",
  57. content: "",
  58. remark: ""
  59. }, {
  60. item: "",
  61. content: "",
  62. remark: ""
  63. }],
  64. isItemShow: false,
  65. itemTxt: "",
  66. isContentShow: false,
  67. contentTxt: "",
  68. isRemarkShow: false,
  69. remarkTxt: "",
  70. }
  71. },
  72. methods: {
  73. // 添加行
  74. addRow() {
  75. this.list.push({})
  76. },
  77. // 删除行
  78. del(index) {
  79. this.list.splice(index, 1)
  80. },
  81. // 编辑工作内容
  82. modifyContent(val, index) {
  83. console.log(val);
  84. this.isContentShow = true;
  85. this.contentTxt = val.content;
  86. this.index = index
  87. },
  88. // 编辑工作事项
  89. modifyItem(val, index) {
  90. this.isItemShow = true;
  91. this.itemTxt = val.item;
  92. this.index = index
  93. },
  94. //编辑备注
  95. modifyRemark(val, index) {
  96. this.isRemarkShow = true;
  97. this.remarkTxt = val.remark;
  98. this.index = index
  99. },
  100. confirmItem() {
  101. this.list[this.index].item = this.itemTxt;
  102. this.isItemShow = false;
  103. },
  104. confirmContent() {
  105. this.list[this.index].content = this.contentTxt;
  106. this.isContentShow = false;
  107. },
  108. confirmRemark() {
  109. this.list[this.index].remark = this.remarkTxt;
  110. this.isRemarkShow = false
  111. }
  112. }
  113. }
  114. </script>
  115. <style lang="scss" scoped>
  116. .tb1 {
  117. width: 95%;
  118. margin: auto;
  119. margin-top: 0.5rem;
  120. text-align: center;
  121. td {
  122. font-size: 0.21rem;
  123. }
  124. td:nth-child(1) {
  125. width: 0.67rem;
  126. }
  127. input {
  128. width: 100%;
  129. border: none;
  130. outline: none;
  131. }
  132. .th {
  133. background-color: #f3f6fd;
  134. }
  135. }
  136. /deep/ .van-nav-bar__text {
  137. color: white;
  138. }
  139. .btn {
  140. position: absolute;
  141. right: 0.5rem;
  142. margin-top: 1rem;
  143. }
  144. .addBtn {
  145. position: absolute;
  146. left: 0.5rem;
  147. margin-top: 1.1rem;
  148. }
  149. </style>

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/花生_TL007/article/detail/692038
推荐阅读
相关标签
  

闽ICP备14008679号