form-dlg.vue 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <template>
  2. <el-dialog :title="title" :visible.sync="visible" :width="width" :close-on-click-modal="false" @close="$emit('cancel')">
  3. <el-form ref="form" :model="form" :rules="rules" v-bind="options">
  4. <slot>
  5. <el-form-item
  6. v-for="(item, index) in fields"
  7. :key="'form-field-' + index"
  8. :label="item.field.label"
  9. :prop="item.field.name"
  10. :required="item.field.required"
  11. :rules="item.rules"
  12. v-bind="item.formOpts"
  13. >
  14. <el-select v-if="item.dict" v-model="form[item.field.name]" placeholder="请选择">
  15. <el-option
  16. v-for="(_item, _index) in item.dict"
  17. :key="'option-item-' + _index"
  18. :label="_item.name"
  19. :value="_item.code"
  20. :disabled="_item.status == '1'"
  21. ></el-option>
  22. </el-select>
  23. <el-checkbox
  24. v-else-if="item.formOpts && item.formOpts.inputType == 'checkbox'"
  25. v-model="form[item.field.name]"
  26. :disabled="readonly || item.field.readonly || (!isNew && item.field.editable === false)"
  27. >{{ item.formOpts && item.formOpts.placeholder }}</el-checkbox
  28. >
  29. <el-input
  30. v-else
  31. v-model="form[item.field.name]"
  32. :disabled="readonly || item.field.readonly || (!isNew && item.field.editable === false)"
  33. :placeholder="item.formOpts && item.formOpts.placeholder"
  34. :type="item.formOpts && item.formOpts.inputType"
  35. ></el-input>
  36. </el-form-item>
  37. </slot>
  38. </el-form>
  39. <div slot="footer" class="dialog-footer">
  40. <el-button @click="$emit('cancel')" :size="options.size">取 消</el-button>
  41. <el-button type="primary" @click="handleSave" :size="options.size" v-show="!readonly">确 定</el-button>
  42. </div>
  43. </el-dialog>
  44. </template>
  45. <script>
  46. import { FieldMeta } from './meta-util';
  47. export default {
  48. name: 'data-dlg',
  49. props: {
  50. data: { type: Object, required: true },
  51. meta: { type: Array, required: true },
  52. rules: Object,
  53. readonly: { type: Boolean, default: false } /* 是否只读 */,
  54. isNew: { type: Boolean, default: false } /* 是否新创建 */,
  55. options: {
  56. type: Object,
  57. default: () => ({ size: 'small' }),
  58. } /* form options */,
  59. width: String,
  60. title: String,
  61. visible: { type: Boolean, default: true },
  62. },
  63. data() {
  64. return {
  65. form: { ...this.data },
  66. };
  67. },
  68. methods: {
  69. handleSave() {
  70. this.$refs['form'].validate(valid => {
  71. if (valid) {
  72. this.$emit('save', { isNew: this.isNew, data: this.form });
  73. } else {
  74. console.warn('form validate error!!!');
  75. }
  76. });
  77. },
  78. },
  79. computed: {
  80. fields() {
  81. return this.meta
  82. .map(FieldMeta)
  83. .filter(p => p.slots.form)
  84. .sort((a, b) => b.order - a.order);
  85. },
  86. },
  87. };
  88. </script>