当前位置:   article > 正文

Ant Design of Vue 表单封装_antdesignvue封装form表单

antdesignvue封装form表单

1.通过v-decorator设置初始值

2.在自定义组件上使用v-decorator

3.点击重置按钮,自定义组件恢复初始值

效果:

目录:

Form.vue:

  1. <template>
  2. <a-form
  3. :form="form"
  4. :label-col="{ span: 5 }"
  5. :wrapper-col="{ span: 12 }"
  6. @submit="handleSubmit"
  7. >
  8. <a-form-item label="Note">
  9. <a-input v-decorator="Array.isArray(test) ? test : ''" />
  10. </a-form-item>
  11. <a-form-item label="Gender">
  12. <a-select
  13. v-decorator="[
  14. 'gender',
  15. {
  16. rules: [{ required: true, message: 'Please select your gender!' }],
  17. },
  18. ]"
  19. placeholder="Select a option and change input text above"
  20. @change="handleSelectChange"
  21. >
  22. <a-select-option value="male">
  23. male
  24. </a-select-option>
  25. <a-select-option value="female">
  26. female
  27. </a-select-option>
  28. </a-select>
  29. </a-form-item>
  30. <a-form-item label="Time">
  31. <RangePicker v-decorator="time"></RangePicker>
  32. </a-form-item>
  33. <a-form-item label="Name">
  34. <Input v-decorator="['name', {initialValue: 'xu'}]"></Input>
  35. </a-form-item>
  36. <a-form-item :wrapper-col="{ span: 12, offset: 5 }">
  37. <a-button type="primary" html-type="submit">
  38. Submit
  39. </a-button>
  40. <a-button :style="{ marginLeft: '8px' }" @click="handleReset">
  41. Clear
  42. </a-button>
  43. </a-form-item>
  44. </a-form>
  45. </template>
  46. <script>
  47. import RangePicker from "../components/RangePicker"
  48. import Input from "../components/Input"
  49. import { oneDay } from '../utils/time'
  50. export default {
  51. components: {
  52. RangePicker,
  53. Input,
  54. },
  55. data() {
  56. return {
  57. formLayout: "horizontal",
  58. form: this.$form.createForm(this, { name: "coordinated" }),
  59. test: [
  60. "note",
  61. {
  62. rules: [{ required: true, message: "Please input your note!" }],
  63. initialValue: "Hi!",
  64. },
  65. ],
  66. time: [
  67. "time",
  68. {
  69. initialValue: oneDay(),
  70. },
  71. ],
  72. }
  73. },
  74. methods: {
  75. handleSubmit(e) {
  76. e.preventDefault()
  77. this.form.validateFields((err, values) => {
  78. if (!err) {
  79. console.log("Received values of form: ", values)
  80. }
  81. })
  82. let values = this.form.getFieldsValue()
  83. console.log(values)
  84. },
  85. handleReset() {
  86. this.form.resetFields();
  87. let values = this.form.getFieldsValue()
  88. console.log(values)
  89. },
  90. handleSelectChange(value) {
  91. console.log(value)
  92. this.form.setFieldsValue({
  93. note: `Hi, ${value === "male" ? "man" : "lady"}!`,
  94. })
  95. },
  96. },
  97. mounted() {
  98. // this.form.setFieldsValue({
  99. // note: `Hi!`,
  100. // })
  101. console.log(this.time)
  102. },
  103. }
  104. </script>

RangePicker.vue:

  1. <template>
  2. <a-range-picker
  3. :value="defaultValue"
  4. @change="change"
  5. :showTime="showTime"
  6. ></a-range-picker>
  7. </template>
  8. <script>
  9. import moment from "moment"
  10. export default {
  11. props: {
  12. value: {
  13. type: Array,
  14. },
  15. },
  16. data() {
  17. let date = this.value
  18. if (date && date.length)
  19. date = [moment(this.value[0]), moment(this.value[1])]
  20. return {
  21. defaultValue: date,
  22. showTime: {
  23. format: "HH:mm:ss",
  24. defaultValue: [
  25. moment("00:00:00", "HH:mm:ss"),
  26. moment("23:59:59", "HH:mm:ss"),
  27. ],
  28. },
  29. }
  30. },
  31. methods: {
  32. change(val) {
  33. let date = val
  34. if (val.length)
  35. date = [moment(val[0]).valueOf(), moment(val[1]).valueOf()]
  36. this.$emit("change", date)
  37. this.$emit("input", date)
  38. },
  39. },
  40. watch: {
  41. value(val) {
  42. if (val && val.length) {
  43. this.defaultValue = [moment(val[0]), moment(val[1])]
  44. }
  45. },
  46. },
  47. }
  48. </script>
  49. <style scoped></style>

Input.vue:

  1. <template>
  2. <input :value="defaultValue" @input="handleInput" placeholder="请输入"/>
  3. </template>
  4. <script>
  5. export default {
  6. props: ['value'],
  7. data() {
  8. return {
  9. defaultValue: this.value
  10. }
  11. },
  12. methods: {
  13. handleInput(e) {
  14. this.defaultValue = e.target.value
  15. this.$emit('change', this.defaultValue)
  16. this.$emit('input', this.defaultValue)
  17. }
  18. },
  19. watch: {
  20. value() {
  21. this.defaultValue= this.value
  22. }
  23. }
  24. }
  25. </script>
  26. <style>
  27. </style>

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

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