form.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <template>
  2. <div id="add">
  3. <el-form
  4. ref="form"
  5. :model="form"
  6. :rules="rules"
  7. :label-width="labelWidth"
  8. class="form"
  9. size="small"
  10. @submit.native.prevent
  11. :style="styles"
  12. :inline="inline"
  13. >
  14. <template v-for="(item, index) in fields">
  15. <template v-if="!loading">
  16. <el-form-item v-if="display(item)" :key="'form-field-' + index" :label="getField('label', item)" :prop="item.model" :required="item.required">
  17. <template v-if="!item.custom">
  18. <template v-if="item.type !== 'text'">
  19. <!-- <el-tooltip class="item" effect="dark" :content="item.tip" placement="top-start" :disabled="!item.tip"> -->
  20. <template v-if="item.type === `date` || item.type === `datetime`">
  21. <el-date-picker
  22. v-model="form[item.model]"
  23. :type="item.type"
  24. placeholder="选择择"
  25. format="yyyy-MM-dd"
  26. value-format="yyyy-MM-dd"
  27. v-bind="item.options"
  28. >
  29. </el-date-picker>
  30. </template>
  31. <template v-else-if="item.type === `year` || item.type === `week` || item.type === `day`">
  32. <el-date-picker
  33. v-model="form[item.model]"
  34. :type="item.type"
  35. placeholder="选择择"
  36. :format="`${item.type === 'year' ? 'yyyy' : item.type === 'week' ? 'MM' : 'dd'}`"
  37. :value-format="`${item.type === 'year' ? 'yyyy' : item.type === 'week' ? 'MM' : 'dd'}`"
  38. v-bind="item.options"
  39. >
  40. </el-date-picker>
  41. </template>
  42. <template v-else-if="item.type === 'radio'">
  43. <el-radio-group v-model="form[item.model]" size="mini" v-bind="item.options">
  44. <slot name="radios" v-bind="{ item, form, fieldChange }"></slot>
  45. </el-radio-group>
  46. </template>
  47. <template v-else-if="item.type === 'select'">
  48. <el-select v-model="form[item.model]" v-bind="item.options" filterable clearable>
  49. <slot name="options" v-bind="{ item, form, fieldChange }"></slot>
  50. </el-select>
  51. </template>
  52. <template v-else-if="item.type === 'textarea'">
  53. <el-input clearable v-model="form[item.model]" type="textarea" :autosize="{ minRows: 3, maxRows: 5 }"></el-input>
  54. </template>
  55. <template v-else-if="item.type === 'editor'">
  56. <wang-editor v-model="form[item.model]"></wang-editor>
  57. </template>
  58. <template v-else-if="item.type === 'checkbox'">
  59. <el-checkbox-group v-model="form[item.model]" v-bind="item.options">
  60. <slot name="checkboxs" v-bind="{ item, form, fieldChange }"></slot>
  61. </el-checkbox-group>
  62. </template>
  63. <template v-else>
  64. <el-input
  65. clearable
  66. v-model="form[item.model]"
  67. :type="getField('type', item)"
  68. :placeholder="getField('placeholder', item)"
  69. :show-password="getField('type', item) === 'password'"
  70. v-bind="item.options"
  71. ></el-input>
  72. </template>
  73. <!-- </el-tooltip> -->
  74. </template>
  75. <template v-else>
  76. {{ form[item.model] }}
  77. </template>
  78. </template>
  79. <template v-else>
  80. <slot name="custom" v-bind="{ item, form, fieldChange }"></slot>
  81. </template>
  82. </el-form-item>
  83. </template>
  84. </template>
  85. <el-form-item v-if="needSave" class="btn">
  86. <el-row type="flex" align="middle" justify="space-around">
  87. <el-col :span="6">
  88. <el-button type="primary" @click="save">{{ submitText }}</el-button>
  89. </el-col>
  90. </el-row>
  91. </el-form-item>
  92. <el-form-item v-else>
  93. <slot name="submit"></slot>
  94. </el-form-item>
  95. </el-form>
  96. </div>
  97. </template>
  98. <script>
  99. import _ from 'lodash';
  100. import wangEditor from './wang-editor.vue';
  101. export default {
  102. name: 'add',
  103. props: {
  104. fields: { type: Array, default: () => [] },
  105. rules: { type: Object, default: () => {} },
  106. isNew: { type: Boolean, default: true },
  107. data: null,
  108. styles: { type: Object, default: () => {} },
  109. needSave: { type: Boolean, default: true },
  110. labelWidth: { type: String, default: '120px' },
  111. useEnter: { type: Boolean, default: true },
  112. submitText: { type: String, default: '保存' },
  113. inline: { type: Boolean, default: false },
  114. reset: { type: Boolean, default: true },
  115. },
  116. components: {
  117. wangEditor,
  118. },
  119. data: () => ({
  120. form: {},
  121. show: false,
  122. dateShow: false,
  123. loading: true,
  124. }),
  125. created() {
  126. if (this.useEnter) {
  127. document.onkeydown = () => {
  128. let key = window.event.keyCode;
  129. if (key == 13) {
  130. this.save();
  131. }
  132. };
  133. }
  134. },
  135. computed: {},
  136. mounted() {},
  137. watch: {
  138. fields: {
  139. handler(val) {
  140. this.checkType();
  141. },
  142. immediate: true,
  143. },
  144. data: {
  145. handler(val) {
  146. this.loading = true;
  147. if (val) this.$set(this, `form`, this.data);
  148. this.loading = false;
  149. },
  150. immediate: true,
  151. deep: true,
  152. },
  153. },
  154. methods: {
  155. getField(item, data) {
  156. let res = _.get(data, item, null);
  157. if (item === 'type') res = res === null ? `text` : res;
  158. if (item === 'placeholder') res = res === null ? `请输入${data.label}` : res;
  159. if (item === 'required') res = res === null ? false : res;
  160. if (item === `error`) res = res === null ? `${data.label}错误` : res;
  161. return res;
  162. },
  163. save() {
  164. this.$refs['form'].validate(valid => {
  165. if (valid) {
  166. this.$emit(`save`, { isNew: this.isNew, data: JSON.parse(JSON.stringify(this.form)) });
  167. if (this.reset) this.$refs.form.resetFields();
  168. } else {
  169. console.warn('form validate error!!!');
  170. }
  171. });
  172. },
  173. fieldChange({ model, value }) {
  174. this.$set(this.form, model, value);
  175. },
  176. checkType() {
  177. let arr = this.fields.filter(fil => fil.type === 'checkbox');
  178. if (arr.length > 0 && this.isNew) {
  179. for (const item of arr) {
  180. this.$set(this.form, `${item.model}`, []);
  181. }
  182. }
  183. },
  184. display(field) {
  185. let dis = _.get(field, `display`);
  186. if (!_.isFunction(dis)) return true;
  187. else return dis(field, this.form);
  188. },
  189. },
  190. };
  191. </script>
  192. <style lang="less" scoped>
  193. .form {
  194. padding: 2rem 1rem;
  195. background: #fff;
  196. border-radius: 20px;
  197. }
  198. /deep/.btn .el-form-item__content {
  199. margin-left: 0 !important;
  200. }
  201. </style>