Skip to content

Commit 088188f

Browse files
committed
version 1.0
1 parent 984581f commit 088188f

File tree

9 files changed

+12638
-76
lines changed

9 files changed

+12638
-76
lines changed

package-lock.json

+11,761
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
"lint": "vue-cli-service lint"
99
},
1010
"dependencies": {
11+
"ant-design-vue": "^1.3.13",
12+
"babel-plugin-import": "^1.12.0",
1113
"core-js": "^2.6.5",
1214
"vue": "^2.6.10"
1315
},

src/App.vue

+9-13
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,24 @@
11
<template>
2-
<div id="app">
3-
<img alt="Vue logo" src="./assets/logo.png">
4-
<HelloWorld msg="Welcome to Your Vue.js App"/>
2+
<div class="demo">
3+
<Editablecell/>
54
</div>
5+
66
</template>
77

88
<script>
9-
import HelloWorld from './components/HelloWorld.vue'
9+
import Editablecell from './view/editablecell.vue'
1010
1111
export default {
1212
name: 'app',
1313
components: {
14-
HelloWorld
14+
Editablecell
1515
}
1616
}
1717
</script>
1818

1919
<style>
20-
#app {
21-
font-family: 'Avenir', Helvetica, Arial, sans-serif;
22-
-webkit-font-smoothing: antialiased;
23-
-moz-osx-font-smoothing: grayscale;
24-
text-align: center;
25-
color: #2c3e50;
26-
margin-top: 60px;
27-
}
20+
.demo {
21+
padding: 24px;
22+
}
23+
2824
</style>

src/components/EditableCell/cell.vue

+206
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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>

src/components/EditableCell/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import EditableCell from './cell.vue';
2+
3+
export { EditableCell };
4+
5+
export default EditableCell;

src/components/HelloWorld.vue

-58
This file was deleted.

src/main.js

+7-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import Vue from 'vue'
22
import App from './App.vue'
3+
import Antd from 'ant-design-vue'
34

4-
Vue.config.productionTip = false
5+
/**
6+
* css 区域
7+
*/
8+
import 'ant-design-vue/dist/antd.css'
9+
10+
Vue.use(Antd)
511

612
new Vue({
713
render: h => h(App),

0 commit comments

Comments
 (0)