当前位置:   article > 正文

antd动态增减表单_增减输入框

增减输入框

 1.官网示例(Ant Design Vue

 官网已经写好了测试用例。但是这个只是增减表单并点击提交后获取提交的值。作为表单,少不了的是初始渲染。没有初始渲染,光有提交的表单是不完整的表单。下面这个才是我想实现的效果。

2.本地代码完善

 注意:antd版本别搞错了,这里用的是v1.xx版本的。最新的已经到3点几了。

 下面是完整代码,直接引入即用。

  1. <template>
  2. <a-form :form="form" @submit="handleSubmit" v-if="show">
  3. <a-form-item
  4. v-for="(k, index) in form.getFieldValue('keys')"
  5. :key="k"
  6. v-bind="index === 0 ? formItemLayout : formItemLayoutWithOutLabel"
  7. :label="index === 0 ? 'URL' : ''"
  8. :required="false"
  9. >
  10. <a-input
  11. v-decorator="[
  12. `names[${k}]`,
  13. {
  14. validateTrigger: ['change', 'blur'],
  15. rules: [
  16. {
  17. required: true,
  18. whitespace: true,
  19. message: '请输入正确的URL信息',
  20. },
  21. ],
  22. },
  23. ]"
  24. placeholder="请输入URL信息"
  25. style="width: 60%; margin-right: 8px"
  26. />
  27. <a-icon
  28. v-if="form.getFieldValue('keys').length > 1"
  29. class="dynamic-delete-button"
  30. type="minus-circle-o"
  31. :disabled="form.getFieldValue('keys').length === 1"
  32. @click="() => remove(k)"
  33. />
  34. </a-form-item>
  35. <a-form-item v-bind="formItemLayoutWithOutLabel">
  36. <a-button type="dashed" style="width: 60%" @click="add">
  37. <a-icon type="plus" /> 添加
  38. </a-button>
  39. </a-form-item>
  40. <a-form-item v-bind="formItemLayoutWithOutLabel">
  41. <a-button type="primary" html-type="submit">
  42. 提交
  43. </a-button>
  44. </a-form-item>
  45. </a-form>
  46. </template>
  47. <script>
  48. let id = 0;
  49. export default {
  50. data() {
  51. return {
  52. show:false,
  53. project:'', //项目名称
  54. formItemLayout: {
  55. labelCol: {
  56. xs: { span: 24 },
  57. sm: { span: 4 },
  58. },
  59. wrapperCol: {
  60. xs: { span: 24 },
  61. sm: { span: 20 },
  62. },
  63. },
  64. formItemLayoutWithOutLabel: {
  65. wrapperCol: {
  66. xs: { span: 24, offset: 0 },
  67. sm: { span: 20, offset: 4 },
  68. },
  69. },
  70. };
  71. },
  72. mounted() {
  73. this.form = this.$form.createForm(this);
  74. this.geturl(); //获取url地址进行渲染
  75. },
  76. methods: {
  77. geturl(){
  78. // 通过setTimeout模拟网络请求 url :${host} /v1/spanFilter/queryByProject
  79. let data={
  80. "code":"200",
  81. "msg":"查询成功",
  82. "data":{
  83. "spanFilter":{
  84. "project":"cpaas",
  85. "spanNames":[
  86. "http://xianheaihca/aaaa",
  87. "https://*naca*",
  88. "https://woe/wochifan/henaicha"]
  89. }
  90. }
  91. }
  92. this.project=data.data.spanFilter.project;
  93. let keys=Array.from(Array(data.data.spanFilter.spanNames.length),(v,k)=>k);
  94. let names=data.data.spanFilter.spanNames;
  95. let obj={keys:keys,names:names};
  96. console.log(keys)
  97. setTimeout((res) => {
  98. this.form.getFieldDecorator('keys', { initialValue: res.keys, preserve: true });
  99. this.show = true;
  100. this.$nextTick(() => {
  101. this.form.setFieldsValue(res);
  102. });
  103. }, 2000, obj);
  104. },
  105. remove(k) {
  106. const { form } = this;
  107. // can use data-binding to get
  108. const keys = form.getFieldValue('keys');
  109. // We need at least one passenger
  110. if (keys.length === 1) {
  111. return;
  112. }
  113. // can use data-binding to set
  114. form.setFieldsValue({
  115. keys: keys.filter(key => key !== k),
  116. });
  117. },
  118. add() {
  119. const { form } = this;
  120. // can use data-binding to get
  121. const keys = form.getFieldValue('keys');
  122. var newid=keys[keys.length-1]+1;
  123. const nextKeys = keys.concat(newid);
  124. // can use data-binding to set
  125. // important! notify form to detect changes
  126. console.log("添加",keys,nextKeys);
  127. form.setFieldsValue({
  128. keys: nextKeys,
  129. });
  130. },
  131. handleSubmit(e) {
  132. e.preventDefault();
  133. this.form.validateFields((err, values) => {
  134. if (!err) {
  135. const { keys, names } = values;
  136. console.log('提交的值: ', values);
  137. console.log(
  138. '数组值:',
  139. keys.map(key => names[key]),
  140. );
  141. console.log("项目名称",this.project);
  142. }
  143. });
  144. },
  145. },
  146. };
  147. </script>
  148. <style>
  149. .dynamic-delete-button {
  150. cursor: pointer;
  151. position: relative;
  152. top: 4px;
  153. font-size: 24px;
  154. color: #999;
  155. transition: all 0.3s;
  156. }
  157. .dynamic-delete-button:hover {
  158. color: #777;
  159. }
  160. .dynamic-delete-button[disabled] {
  161. cursor: not-allowed;
  162. opacity: 0.5;
  163. }
  164. </style>

 

 

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