c-form.vue 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. <template>
  2. <div id="c-form">
  3. <el-form ref="form" :model="form" :rules="rules" :label-width="labelWidth" class="form" size="small" @submit.native.prevent>
  4. <template v-for="(item, index) in fields">
  5. <el-form-item v-if="display(item)" :key="'form-field-' + index" :label="getField('label', item)" :prop="item.model" :required="item.required">
  6. <template v-if="!item.custom">
  7. <template v-if="item.type === `date` || item.type === `datetime`">
  8. <el-date-picker
  9. v-model="form[item.model]"
  10. :type="item.type"
  11. placeholder="请选择"
  12. :format="item.type === 'date' ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss'"
  13. :value-format="item.type === 'date' ? 'yyyy-MM-dd' : 'yyyy-MM-dd HH:mm:ss'"
  14. v-bind="item.options"
  15. style="width: 100%"
  16. @change="dataChange(item.model)"
  17. >
  18. </el-date-picker>
  19. </template>
  20. <template v-else-if="item.type === `year` || item.type === `week` || item.type === `day`">
  21. <el-date-picker
  22. v-model="form[item.model]"
  23. :type="item.type"
  24. placeholder="请选择"
  25. :format="`${item.type === 'year' ? 'yyyy' : item.type === 'week' ? 'MM' : 'dd'}`"
  26. :value-format="`${item.type === 'year' ? 'yyyy' : item.type === 'week' ? 'MM' : 'dd'}`"
  27. v-bind="item.options"
  28. style="width: 100%"
  29. @change="dataChange(item.model)"
  30. >
  31. </el-date-picker>
  32. </template>
  33. <template v-else-if="item.type === 'time'">
  34. <el-time-picker
  35. v-model="form[item.model]"
  36. placeholder="请选择时间"
  37. format="HH:mm"
  38. value-format="HH:mm"
  39. @change="dataChange(item.model)"
  40. ></el-time-picker>
  41. </template>
  42. <template v-else-if="item.type === 'radio'">
  43. <el-radio-group v-model="form[item.model]" size="mini" @change="dataChange(item.model)" v-bind="item.options">
  44. <slot :name="item.model" v-bind="{ item }"></slot>
  45. </el-radio-group>
  46. </template>
  47. <template v-else-if="item.type === 'checkbox'">
  48. <el-checkbox-group v-model="form[item.model]" @change="dataChange(item.model)" v-bind="item.options">
  49. <slot :name="item.model" v-bind="{ item }"></slot>
  50. </el-checkbox-group>
  51. </template>
  52. <template v-else-if="item.type === 'select'">
  53. <el-select
  54. v-model="form[item.model]"
  55. v-bind="item.options"
  56. filterable
  57. clearable
  58. @change="dataChange(item.model)"
  59. style="width: 100%"
  60. :disabled="item.readonly"
  61. >
  62. <slot :name="item.model" v-bind="{ item }"></slot>
  63. </el-select>
  64. </template>
  65. <template v-else-if="item.type === 'textarea'">
  66. <el-input clearable v-model="form[item.model]" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }" @change="dataChange(item.model)"></el-input>
  67. </template>
  68. <template v-else-if="item.type === 'upload'">
  69. </template>
  70. <template v-else>
  71. <el-input
  72. clearable
  73. v-model="form[item.model]"
  74. :type="getField('type', item)"
  75. :placeholder="getField('placeholder', item)"
  76. :show-password="getField('type', item) === 'password'"
  77. v-bind="item.options"
  78. :readonly="item.readonly"
  79. @change="dataChange(item.model)"
  80. ></el-input>
  81. </template>
  82. </template>
  83. <template v-else>
  84. <slot :name="item.model" v-bind="{ item }"></slot>
  85. </template>
  86. </el-form-item>
  87. </template>
  88. <el-form-item label="" class="btn">
  89. <slot name="submit">
  90. <el-row type="flex" align="middle" justify="start">
  91. <el-col :span="6">
  92. <el-button type="primary" @click="save" v-use="'save'">{{ submitText }}</el-button>
  93. </el-col>
  94. </el-row>
  95. </slot>
  96. </el-form-item>
  97. </el-form>
  98. </div>
  99. </template>
  100. <script>
  101. export default {
  102. name: 'c-form',
  103. props: {
  104. fields: { type: Array, default: () => [{ readonly: false }] },
  105. rules: { type: Object, default: () => {} },
  106. labelWidth: { type: String, default: '120px' },
  107. submitText: { type: String, default: '保存' },
  108. form: null,
  109. reset: { type: Boolean, default: false },
  110. },
  111. model: {
  112. prop: 'form',
  113. event: 'change',
  114. },
  115. components: {},
  116. data: function () {
  117. return {};
  118. },
  119. methods: {
  120. getField(item, data) {
  121. let res = _.get(data, item, null);
  122. if (item === 'type') res = res === null ? `text` : res;
  123. if (item === 'placeholder') res = res === null ? `请输入${data.label}` : res;
  124. if (item === 'required') res = res === null ? false : res;
  125. if (item === `error`) res = res === null ? `${data.label}错误` : res;
  126. return res;
  127. },
  128. save() {
  129. this.$refs['form'].validate((valid) => {
  130. if (valid) {
  131. this.$emit(`save`, { data: JSON.parse(JSON.stringify(this.form)) });
  132. if (this.reset) this.$refs.form.resetFields();
  133. } else {
  134. console.warn('form validate error!!!');
  135. }
  136. });
  137. },
  138. display(field) {
  139. let dis = _.get(field, `display`);
  140. if (!_.isFunction(dis)) return true;
  141. else return dis(field, this.form);
  142. },
  143. dataChange(model) {
  144. const value = JSON.parse(JSON.stringify(this.form[model]));
  145. this.$emit('dataChange', { model, value });
  146. },
  147. },
  148. };
  149. </script>
  150. <style lang="less" scoped></style>