当前位置:   article > 正文

【实用模板】Vue代码文件常用创建或编辑抽屉

【实用模板】Vue代码文件常用创建或编辑抽屉
  1. <template>
  2. <div
  3. :class="$options.name"
  4. v-loading="loading"
  5. :element-loading-text="elementLoadingText"
  6. v-if="showDrawer"
  7. >
  8. <el-drawer
  9. :custom-class="`创建或编辑抽屉-el-drawer`"
  10. :append-to-body="true"
  11. :close-on-press-escape="true"
  12. :destroy-on-close="true"
  13. :show-close="true"
  14. :size="`400px`"
  15. :title="`${(form || {}).ID ? `修改` : `新增`}XXX`"
  16. :wrapperClosable="false"
  17. :visible.sync="showDrawer"
  18. >
  19. <div>
  20. <!--抽屉内容-->
  21. <div class="form-body" @keyup.ctrl.enter="save">
  22. <el-form
  23. @submit.native.prevent
  24. :disabled="disabledForm"
  25. label-position="right"
  26. size=""
  27. >
  28. <el-form-item :label="`名称`" label-width="80px" required>
  29. <el-input
  30. v-model.trim="form.MC"
  31. ref="MC"
  32. @focus="$refs.MC.select()"
  33. :placeholder="`输入名称`"
  34. :maxlength="20"
  35. clearable
  36. show-word-limit
  37. />
  38. </el-form-item>
  39. <el-form-item :label="`备注`" label-width="80px">
  40. <el-input
  41. type="textarea"
  42. v-model="form.BZ"
  43. ref="BZ"
  44. @focus="$refs.BZ.select()"
  45. :placeholder="`请输入内容`"
  46. :maxlength="200"
  47. :rows="3"
  48. show-word-limit
  49. />
  50. </el-form-item>
  51. </el-form>
  52. </div>
  53. <div class="form-footer">
  54. <el-button @click="cancel">取消</el-button>
  55. <el-button type="primary" @click="save" v-if="!disabledForm">保存</el-button>
  56. </div>
  57. </div>
  58. </el-drawer>
  59. </div>
  60. </template>
  61. <script>
  62. export default {
  63. name: "创建或编辑抽屉",
  64. components: {},
  65. data() {
  66. return {
  67. loading: false, //加载状态
  68. elementLoadingText: ``, //加载提示文字
  69. showDrawer: false, //是否显示
  70. form: {}, //表单信息
  71. disabledForm: false,
  72. };
  73. },
  74. props: [
  75. "value", //是否显示
  76. "disabled", //是否禁用
  77. "data",
  78. "readonly",
  79. ],
  80. computed: {},
  81. watch: {
  82. loading(newValue, oldValue) {
  83. newValue || (this.elementLoadingText = ``);
  84. },
  85. value: {
  86. handler(d) {
  87. this.showDrawer = d;
  88. },
  89. deep: true,
  90. immediate: true,
  91. },
  92. showDrawer(d) {
  93. this.$emit("input", d);
  94. }, //是否显示(双向绑定)
  95. readonly: {
  96. handler(d) {
  97. this.disabledForm = d;
  98. },
  99. deep: true,
  100. immediate: true,
  101. },
  102. data: {
  103. handler(newValue, oldValue) {
  104. //console.log('深度监听:', newValue, oldValue);
  105. if (newValue && Object.keys(newValue).length) {
  106. this.form = JSON.parse(JSON.stringify(newValue));
  107. } else {
  108. this.form = {
  109. // 字段名: 默认值, //默认字段、属性值
  110. };
  111. }
  112. },
  113. deep: true, //深度监听
  114. immediate: true, //立即执行
  115. },
  116. },
  117. created() {},
  118. mounted() {},
  119. destroyed() {},
  120. methods: {
  121. valid(d) {
  122. if (!this.form.MC)
  123. return this.$message.error(this.$refs.MC.$el.querySelector("input").placeholder);
  124. },
  125. save() {
  126. if (this.valid()) return;
  127. if (this.form.ID) {
  128. // 修改
  129. } else {
  130. // 新增
  131. }
  132. let data = {
  133. ...this.form,
  134. sgLog: `前端请求来源:${this.$options.name}新建或修改`,
  135. };
  136. this.$d.保存数据接口({
  137. data,
  138. r: {
  139. s: (d) => {
  140. this.$emit(`save`, this.form);
  141. this.cancel(this.form);
  142. },
  143. },
  144. });
  145. },
  146. cancel(d) {
  147. this.showDrawer = false;
  148. this.$emit(`hide`, d);
  149. },
  150. },
  151. };
  152. </script>
  153. <style lang="scss" scoped>
  154. .创建或编辑抽屉 {
  155. }
  156. </style>
  157. <style lang="scss">
  158. .创建或编辑抽屉-el-drawer {
  159. }
  160. </style>

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

闽ICP备14008679号