index.vue 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. <!--
  2. 常规表单组件 - 供新增编辑使用
  3. @Author: linqian
  4. @Date: 2021-05-28 14:43
  5. -->
  6. <template>
  7. <div>
  8. <dg-row>
  9. <el-form :model="form" ref="form" label-suffix=":" :label-width="labelWidth">
  10. <dg-col v-for="(item, index) in formConfig" :key="index" :span="item.span || 12">
  11. <el-form-item :label="item.label" :prop="item.prop" :rules="item.rules">
  12. <component :is="item.component" v-model="form[item.prop]" v-bind="item.attr" clearable />
  13. </el-form-item>
  14. </dg-col>
  15. </el-form>
  16. </dg-row>
  17. <div v-footer>
  18. <dg-button @click="handleClose">取消</dg-button>
  19. <dg-button type="primary" @click="handleSave">确定</dg-button>
  20. </div>
  21. </div>
  22. </template>
  23. <script>
  24. import _ from 'lodash';
  25. export default {
  26. props: {
  27. config: {
  28. type: Array,
  29. default: () => []
  30. },
  31. labelWidth: {
  32. type: String,
  33. default: '100px'
  34. },
  35. // 类型,add || update
  36. type: String,
  37. // 详情
  38. detailInfo: {
  39. type: Object,
  40. default: () => {}
  41. }
  42. },
  43. components: {},
  44. data() {
  45. return {
  46. formConfig: this.config,
  47. form: {}
  48. };
  49. },
  50. computed: {},
  51. methods: {
  52. handleClose() {
  53. this.$emit('close');
  54. },
  55. handleSave() {
  56. this.$refs.form.validate((valid) => {
  57. if (valid) {
  58. this.$emit('success', this.form, this.type);
  59. }
  60. });
  61. }
  62. },
  63. created() {
  64. // 编辑,字段回填
  65. if (Object.keys(this.detailInfo).length > 0) {
  66. this.form = _.cloneDeep(this.detailInfo);
  67. } else {
  68. for (let j = 0; j < this.formConfig.length; j++) {
  69. let { prop, value } = this.formConfig[j];
  70. this.$set(this.form, prop, value);
  71. }
  72. }
  73. },
  74. mounted() {}
  75. };
  76. </script>
  77. <style lang='scss'>
  78. </style>