form.vue 3.2 KB

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