|
| 1 | +/** |
| 2 | +* @Description: |
| 3 | +* @Author: yanhui.gao |
| 4 | +* @Date: 2019/7/24 14:45 |
| 5 | +*/ |
| 6 | +<template> |
| 7 | + <div class="editable-cell"> |
| 8 | + <div v-if="editable" :class="['editable-cell-input-wrapper',singleEdit ? 'single-edit' : '']"> |
| 9 | + <a-form :form="form"> |
| 10 | + <a-form-item :colon="false" class="no-validate"> |
| 11 | + <a-input v-if="showFormItem('input')" v-decorator="decorator" :placeholder="placeholder" @change="handleChange" autocomplete="off" /> |
| 12 | + <a-input-number :min="min" :max="max" v-if="showFormItem('number')" v-decorator="decorator" :placeholder="placeholder" /> |
| 13 | + <a-switch v-if="showFormItem('switch')" v-decorator="decorator" :placeholder="placeholder" @change="handleChange" :value="value"/> |
| 14 | + <a-date-picker :showTime="showTime" :format="format" v-if="showFormItem('datePicker')" v-decorator="decorator" :placeholder="placeholder" /> |
| 15 | + <a-range-picker :showTime="showTime" :format="format" v-if="showFormItem('rangePicker')" v-decorator="decorator" :placeholder="placeholder" /> |
| 16 | + <a-month-picker :showTime="showTime" :format="format" v-if="showFormItem('monthPicker')" v-decorator="decorator" :placeholder="placeholder" /> |
| 17 | + <a-select v-if="showFormItem('select')" v-decorator="decorator" :placeholder="placeholder" @change="handleChange" > |
| 18 | + <slot/> |
| 19 | + </a-select> |
| 20 | + <a-auto-complete |
| 21 | + v-if="showFormItem('autoComplete')" v-decorator="decorator" :placeholder="placeholder" @search="handleSearch" optionLabelProp="text" :disabled="notEdit" |
| 22 | + > |
| 23 | + <template slot="dataSource"> |
| 24 | + <slot/> |
| 25 | + </template> |
| 26 | + </a-auto-complete> |
| 27 | + </a-form-item> |
| 28 | + </a-form> |
| 29 | + <a-icon v-if="singleEdit" type="check" class="editable-cell-icon-check" @click="check(true)" /> |
| 30 | + </div> |
| 31 | + <div v-else :class="['editable-cell-text-wrapper',singleEdit ? 'single-edit' : '']"> |
| 32 | + {{ getText || ' ' }} |
| 33 | + <a-icon v-if="singleEdit" type="edit" class="editable-cell-icon" @click="edit" /> |
| 34 | + </div> |
| 35 | + </div> |
| 36 | +</template> |
| 37 | +<script> |
| 38 | +export default { |
| 39 | + data() { |
| 40 | + return { |
| 41 | + form: this.$form.createForm(this), |
| 42 | + editable: this.defaultEditable, |
| 43 | + }; |
| 44 | + }, |
| 45 | + props: { |
| 46 | + // 非编辑模式下,显示文本 |
| 47 | + text: { |
| 48 | + type: [Number, String, Array, Object], |
| 49 | + }, |
| 50 | + // 编辑模式下组件值 |
| 51 | + value: { |
| 52 | + message: { type: [Number, String, Array, Object], default: '' }, |
| 53 | + }, |
| 54 | + // 表单组件类型 |
| 55 | + formType: { |
| 56 | + validator(value) { |
| 57 | + return ['input', 'number', 'switch', 'datePicker', 'rangePicker', 'select', 'monthPicker', 'autoComplete'].indexOf(value) !== -1; |
| 58 | + }, |
| 59 | + default: 'input', |
| 60 | + }, |
| 61 | + // 表单校验 |
| 62 | + decoratorOptions: { |
| 63 | + type: [Array, Object], |
| 64 | + }, |
| 65 | + // number 类型输入框属性 |
| 66 | + min: { type: Number, default: -Infinity }, |
| 67 | + max: { type: Number, default: Infinity }, |
| 68 | + // 表单默认提示 |
| 69 | + placeholder: { type: String, default: '请输入' }, |
| 70 | + // 支持单元格单个编辑 |
| 71 | + singleEdit: { type: Boolean, default: false }, |
| 72 | + // 默认编辑模式 |
| 73 | + defaultEditable: { type: Boolean, default: false }, |
| 74 | + // 是否自动保存 |
| 75 | + autoSave: { type: Boolean, default: false }, |
| 76 | + // 日期组件设置 |
| 77 | + format: { type: String, default: 'YYYY-MM-DD' }, |
| 78 | + showTime: { type: [Boolean, Object], default: false }, |
| 79 | + // 是否可编辑 |
| 80 | + notEdit: { type: Boolean, default: false }, |
| 81 | + }, |
| 82 | + computed: { |
| 83 | + // 格式化文本 |
| 84 | + getText() { |
| 85 | + if (typeof this.text === 'function') { |
| 86 | + return this.text(); |
| 87 | + } |
| 88 | + return this.text; |
| 89 | + }, |
| 90 | + // 根据属性,合成表单验证装饰器 |
| 91 | + decorator() { |
| 92 | + if (this.decoratorOptions) { |
| 93 | + const newOptions = JSON.parse(JSON.stringify(this.decoratorOptions)); |
| 94 | + newOptions.initialValue = this.value; |
| 95 | + const itemDecorator = ['formName']; |
| 96 | + itemDecorator.push(newOptions); |
| 97 | + return itemDecorator; |
| 98 | + } |
| 99 | + const itemDecorator = ['formName']; |
| 100 | + const options = {}; |
| 101 | + options.initialValue = this.value; |
| 102 | + itemDecorator.push(options); |
| 103 | + return itemDecorator; |
| 104 | + }, |
| 105 | + }, |
| 106 | + watch: { |
| 107 | + // 监视父页面是否可编辑属性,及时响应控件内切换 |
| 108 | + defaultEditable(value) { |
| 109 | + this.editable = value; |
| 110 | + }, |
| 111 | + }, |
| 112 | + methods: { |
| 113 | + // 判断表单是否 |
| 114 | + showFormItem(formType) { |
| 115 | + return this.formType === formType; |
| 116 | + }, |
| 117 | + // 表单change事件 |
| 118 | + handleChange(value) { |
| 119 | + if (this.autoSave) { |
| 120 | + if (this.formType === 'input') { |
| 121 | + this.$emit('valueChange', value.target.value); |
| 122 | + } else if (this.formType === 'select') { |
| 123 | + this.$emit('valueChange', value); |
| 124 | + } |
| 125 | + } |
| 126 | + // this.check(false); |
| 127 | + }, |
| 128 | + // auto-complete 搜索 |
| 129 | + handleSearch(value) { |
| 130 | + this.$emit('autoCompleteSearch', value); |
| 131 | + }, |
| 132 | + // 表单校验 |
| 133 | + validate() { |
| 134 | + const result = { |
| 135 | + success: false, |
| 136 | + value: '', |
| 137 | + }; |
| 138 | + this.form.validateFields((err, values) => { |
| 139 | + if (!err) { |
| 140 | + result.success = true; |
| 141 | + result.value = values.formName; |
| 142 | + } |
| 143 | + }); |
| 144 | + return result; |
| 145 | + }, |
| 146 | + // 表单赋值 |
| 147 | + setValue(value) { |
| 148 | + this.form.setFieldsValue({ |
| 149 | + formName: value, |
| 150 | + }); |
| 151 | + }, |
| 152 | + // 点击"对号"图标,校验通过后触发通知父组件 |
| 153 | + check(flag) { |
| 154 | + this.form.validateFields((err, values) => { |
| 155 | + if (!err) { |
| 156 | + if (flag) { |
| 157 | + // 切换到只读模式 |
| 158 | + this.editable = false; |
| 159 | + } |
| 160 | + this.$emit('valueChange', values.formName); |
| 161 | + } |
| 162 | + }); |
| 163 | + }, |
| 164 | + // 切换到编辑模式 |
| 165 | + edit() { |
| 166 | + this.editable = true; |
| 167 | + }, |
| 168 | + }, |
| 169 | +}; |
| 170 | +</script> |
| 171 | + |
| 172 | +<style scoped> |
| 173 | + .editable-cell { |
| 174 | + position: relative; |
| 175 | + margin-top: -9px; |
| 176 | + margin-bottom: -9px; |
| 177 | + } |
| 178 | +
|
| 179 | + .editable-cell:hover .editable-cell-icon { |
| 180 | + display: inline-block; |
| 181 | + } |
| 182 | + .editable-cell-input-wrapper.single-edit, |
| 183 | + .editable-cell-text-wrapper.single-edit { |
| 184 | + padding-right: 24px; |
| 185 | + } |
| 186 | +
|
| 187 | + .editable-cell-icon, |
| 188 | + .editable-cell-icon-check { |
| 189 | + position: absolute; |
| 190 | + right: 0; |
| 191 | + width: 20px; |
| 192 | + cursor: pointer; |
| 193 | + } |
| 194 | + .editable-cell-icon { |
| 195 | + top: 3px; |
| 196 | + line-height: 18px; |
| 197 | + display: none; |
| 198 | + } |
| 199 | + .editable-cell-icon-check { |
| 200 | + top: 12px; |
| 201 | + } |
| 202 | +
|
| 203 | + .no-validate { |
| 204 | + margin-bottom: 0; |
| 205 | + } |
| 206 | +</style> |
0 commit comments