• el-form内容重置(解决点击保存关闭后再点击新增会有编辑携带的数据的问题)


    主要代码:  this.$refs['ruleForm'].resetFields()

    1. <template>
    2. <div class="add-edit-coupon">
    3. <el-dialog title="商品优惠券" top="10vh" :visible.sync="dialogVisible" width="660px" :before-close="handleClose">
    4. <div class="add-edit-coupon-cont">
    5. <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
    6. <el-form-item label="优惠券类型" prop="couponType">
    7. <div class="add-edit-coupon-item-cont">商品优惠券</div>
    8. </el-form-item>
    9. <el-form-item label="优惠券名称" prop="couponName">
    10. <el-input v-model="ruleForm.couponName" maxlength="14" placeholder="请输入优惠券名称" show-word-limit></el-input>
    11. </el-form-item>
    12. <el-form-item label="优惠券简介" prop="briefIntroduction">
    13. <el-input type="textarea" maxlength="40" placeholder="请输入优惠券简介" v-model="ruleForm.briefIntroduction" show-word-limit></el-input>
    14. </el-form-item>
    15. <el-form-item label="优惠方式" prop="couponType">
    16. <el-radio-group v-model="ruleForm.couponType">
    17. <el-radio :label="1">折扣券</el-radio>
    18. </el-radio-group>
    19. </el-form-item>
    20. <el-form-item label="折扣力度" prop="deductionPrice">
    21. <el-input type="number" v-model="ruleForm.deductionPrice">
    22. <template slot="append"></template>
    23. </el-input>
    24. </el-form-item>
    25. <el-form-item label="使用范围" prop="toolApplicationIdList">
    26. <el-checkbox-group v-model="ruleForm.toolApplicationIdList">
    27. <el-checkbox label="5" name="toolApplicationIdList">现场投票</el-checkbox>
    28. <el-checkbox label="2" name="toolApplicationIdList">H5聚合页</el-checkbox>
    29. <el-checkbox label="4" name="toolApplicationIdList">投票评选</el-checkbox>
    30. <el-checkbox label="1" name="toolApplicationIdList">大屏抽奖</el-checkbox>
    31. </el-checkbox-group>
    32. </el-form-item>
    33. <el-form-item label="领取方式" prop="payment">
    34. <el-radio-group v-model="ruleForm.payment">
    35. <el-radio :label="1">自动发放</el-radio>
    36. </el-radio-group>
    37. </el-form-item>
    38. <el-form-item label="可用日期" prop="availabilityDate">
    39. <el-input type="number" v-model="ruleForm.availabilityDate">
    40. <template slot="prepend">领劵后</template>
    41. <template slot="append"></template>
    42. </el-input>
    43. </el-form-item>
    44. <el-form-item label="发放条件" prop="distributionConditions">
    45. <el-select v-model="ruleForm.distributionConditions" placeholder="完成首单后发放">
    46. <el-option label="完成首单后发放" value="1"></el-option>
    47. </el-select>
    48. </el-form-item>
    49. <el-form-item label="是否上线" prop="status">
    50. <el-radio-group v-model="ruleForm.status">
    51. <el-radio :label="1"></el-radio>
    52. <el-radio :label="0"></el-radio>
    53. </el-radio-group>
    54. </el-form-item>
    55. </el-form>
    56. <div slot="footer" class="dialog-footer flex-center">
    57. <el-button @click="dialogVisible = false">取 消</el-button>
    58. <!-- 编辑改保存 -->
    59. <el-button type="primary" @click="submitForm('ruleForm')">{{ isAdd ? '创 建' : '保 存' }}</el-button>
    60. </div>
    61. </div>
    62. </el-dialog>
    63. </div>
    64. </template>
    65. <script>
    66. export default {
    67. components: {},
    68. props: {
    69. value: {
    70. type: Boolean,
    71. },
    72. isAdd: {
    73. type: Number,
    74. default: 0,
    75. },
    76. productCouponItem: {
    77. type: Object,
    78. default: () => {},
    79. },
    80. },
    81. watch: {
    82. productCouponItem: {
    83. handler(value, oldValue) {
    84. if (value) {
    85. this.ruleForm = value
    86. if (value.distributionConditions == 1) {
    87. this.ruleForm.distributionConditions = '完成首单后发放'
    88. }
    89. }
    90. },
    91. },
    92. isAdd: {
    93. handler(value, oldValue) {
    94. if (value == 1) {
    95. // 1为新增,新增时重置表单数据,ruleForm为form的ref的名称
    96. // 1为新增,新增时重置表单数据,ruleForm为form的ref的名称
    97. this.ruleForm = {
    98. toolCouponId: '',
    99. couponName: '', //优惠券名称
    100. briefIntroduction: '', //优惠券简介
    101. couponType: 1, //优惠方式:1 折扣券
    102. deductionPrice: '', //折扣力度
    103. payment: 1, //领取方式:1 自动发放
    104. toolApplicationIdList: [], //使用范围
    105. availabilityDate: '', //可用日期
    106. distributionConditions: '', //发放条件
    107. status: '', //优惠码状态: 1上线 0下架
    108. }
    109. if (typeof this.$refs.ruleForm !== 'undefined') {
    110. this.$refs['ruleForm'].resetFields()
    111. }
    112. }
    113. },
    114. },
    115. 'ruleForm.deductionPrice': {
    116. handler(value, oldValue) {
    117. if (value) {
    118. this.ruleForm.deductionPrice = value.toString().replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1') //保留一位小数
    119. }
    120. },
    121. },
    122. 'ruleForm.availabilityDate': {
    123. handler(value, oldValue) {
    124. if (value) {
    125. this.ruleForm.deductionPrice = value.toString().replace(/^\D*(\d*(?:\.\d{0,1})?).*$/g, '$1') //保留一位小数
    126. }
    127. },
    128. },
    129. },
    130. data() {
    131. return {
    132. ruleForm: {
    133. toolCouponId: '',
    134. couponName: '', //优惠券名称
    135. briefIntroduction: '', //优惠券简介
    136. couponType: 1, //优惠方式:1 折扣券
    137. deductionPrice: '', //折扣力度
    138. payment: 1, //领取方式:1 自动发放
    139. toolApplicationIdList: [], //使用范围
    140. availabilityDate: '', //可用日期
    141. distributionConditions: '', //发放条件
    142. status: '', //优惠码状态: 1上线 0下架
    143. },
    144. rules: {
    145. couponName: [
    146. { required: true, message: '请输入优惠券名称', trigger: 'blur' },
    147. { min: 1, max: 14, message: '长度在 1 到 14个字符', trigger: 'blur' },
    148. ],
    149. briefIntroduction: [
    150. { required: true, message: '请输入优惠券名称', trigger: 'blur' },
    151. { min: 1, max: 40, message: '长度在 1 到 40个字符', trigger: 'blur' },
    152. ],
    153. couponType: [{ required: true, message: '请选择优惠方式', trigger: 'change' }],
    154. deductionPrice: [{ required: true, message: '请输入折扣力度', trigger: 'blur' }],
    155. toolApplicationIdList: [{ type: 'array', required: true, message: '请至少选择一个工具', trigger: 'change' }],
    156. payment: [{ required: true, message: '请选择领取方式', trigger: 'change' }],
    157. availabilityDate: [{ required: true, message: '请填写可用日期', trigger: 'blur' }],
    158. distributionConditions: [{ required: true, message: '请选择发放条件', trigger: 'change' }],
    159. status: [{ required: true, message: '请选择是否上线', trigger: 'change' }],
    160. },
    161. }
    162. },
    163. computed: {
    164. dialogVisible: {
    165. get() {
    166. return this.value
    167. },
    168. set(val) {
    169. this.$emit('input', val)
    170. },
    171. },
    172. },
    173. methods: {
    174. submitForm(formName) {
    175. this.$refs[formName].validate((valid) => {
    176. if (valid) {
    177. this.$emit('submitCoupon', this.ruleForm)
    178. } else {
    179. console.log('error submit!!')
    180. return false
    181. }
    182. })
    183. },
    184. handleClose() {
    185. this.dialogVisible = false
    186. this.$refs['ruleForm'].resetFields()
    187. },
    188. },
    189. created() {
    190. console.log(Object.keys(this.productCouponItem).length != 0, 'productCouponItem')
    191. },
    192. mounted() {},
    193. beforeCreate() {},
    194. beforeMount() {},
    195. beforeUpdate() {},
    196. updated() {},
    197. beforeDestroy() {},
    198. destroyed() {},
    199. activated() {},
    200. }
    201. </script>
    202. <style lang='scss' scoped>
    203. .add-edit-coupon {
    204. .add-edit-coupon-item {
    205. margin-bottom: 16px;
    206. }
    207. }
    208. </style>

  • 相关阅读:
    VC++初始化结构体数组
    1688API接入说明(商品详情数据示例)
    数据库操作QTableView保存小数点的位数
    02、Python 字符串
    动态规划最长公共子序列(LCS)问题(Java实现)
    Linux系统编程:文件描述符以及IO多路复用
    QT报错,QObject::setParent: Cannot set parent, new parent is in a different Thread
    视频教程下载:ChatGPT驱动的SEO、网络营销、生产力提升
    containerd 源码分析:创建 container(一)
    ExtraTrees之ExtraTreesRegressor参数详解以及调参
  • 原文地址:https://blog.csdn.net/leng0920/article/details/132686665