当前位置:   article > 正文

vue+element ui动态增加表单项并支持删除_vue动态新增表单

vue动态新增表单

需求:点击加号动态添加表单项站点和通道,当站点和通道有多个时允许删除,只有一个时不能删除,删除按钮隐藏

先来看看效果吧

默认初始效果:

点击加号动态新增表单项后:

 上代码:

  1. <template>
  2. <div class="select-site-ipc" v-for="(item,index) in alarmRuleForm.alarmSourceArray"
  3. :key=index>
  4. <div class="site-box">
  5. <el-form-item class="site-item" :prop="'alarmSourceArray.' + index + '.siteId'" :rules="[{ required:true,message:$translate('errorCode.33042'), trigger: 'change' }]">
  6. <el-select
  7. ref='sceneSelect'
  8. class="site-scene"
  9. :label="$translate('site')"
  10. required
  11. v-model="item.siteId"
  12. filterable
  13. :fitDropdownWidth="true"
  14. style="width:368px"
  15. @change="changeSite($event,index)"
  16. @visible-change='visibleChange'
  17. >
  18. <el-option
  19. v-for="item in siteOps"
  20. :key="item.value"
  21. :label="item.label"
  22. :value="item.value"
  23. :title="item.label"
  24. :disabled="item.disabled"
  25. >
  26. <span>{{ item.label }}</span>
  27. </el-option>
  28. </el-select>
  29. <i v-if='alarmRuleForm.alarmSourceArray.length>1' class="el-icon-close del-alarm-source" @click="deleteItem(index)"></i>
  30. </el-form-item>
  31. </div>
  32. <el-form-item :prop="'alarmSourceArray.' + index + '.ipc'" :rules="[{ required:true,message:$translate('errorCode.33017'), trigger: 'change' }]">
  33. <el-select
  34. v-model="item.ipc"
  35. required
  36. multiple
  37. collapse-tags
  38. style="width:368px;"
  39. placeholder="请选择">
  40. <el-option
  41. v-for="item in item.chlList"
  42. :key="item.value"
  43. :label="item.label"
  44. :value="item.value">
  45. </el-option>
  46. </el-select>
  47. </el-form-item>
  48. </div>
  49. <!-- 可添加站点和通道 -->
  50. <el-row>
  51. <el-col><div class="add-site" @click="AddSite"><SvgImg viewBox="0 0 14 14" width="14" height="14"><icon-add /></SvgImg> {{$translate('site')}}</div></el-col>
  52. </el-row>
  53. </div>
  54. </template>
  55. <script>
  56. export default{
  57. data(){
  58. return {
  59. alarmRuleForm:{
  60. alarmSourceArray: [
  61. { siteId: '', ipc: [],chlList: [] }
  62. ],
  63. }
  64. }
  65. },
  66. methods:{
  67. // 点击新增站点
  68. AddSite () {
  69. let index = null
  70. if (this.operationType === 1) {
  71. this.alarmRuleForm.alarmSourceArray.push({
  72. siteId: '',
  73. ipc: [],
  74. chlList: []
  75. })
  76. index = this.alarmRuleForm.alarmSourceArray.length - 1
  77. } else {
  78. index = this.alarmRuleForm.alarmSourceArray.length
  79. }
  80. },
  81. deleteItem (index) {
  82. this.alarmRuleForm.alarmSourceArray.splice(index,1)
  83. },
  84. }
  85. }
  86. </script>

其中,核心代码是表单项prop的写法,首先,先定义一个数组用来存放可动态添加的部分(alarmRuleForm.alarmSourceArray),外层循环该数组,其中每一个表单项的prop就表示成‘数组名.index.变量名’即"'alarmSourceArray.'+index+'.siteId'",这里不能用item来替换数组名alarmSourceArray

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