-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathEdit.vue
98 lines (85 loc) · 3.37 KB
/
Edit.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
<template>
<div v-loading="loading" :element-loading-text="loadingText" loading-custom-class="test" style="min-height:100px;">
<el-form v-if="entity" ref="form" :model="entity" :rules="rules" label-width="150px">
<div v-for="column in columns" :key="column">
<slot :name="column" :entity="entity">
<el-crud-field :name="column" :fields="fields" :titles="titles" v-model="entity[column]"></el-crud-field>
</slot>
</div>
<div style="margin-top:1em; text-align:right;">
<el-button type="primary" v-on:click="submitForm()">Update</el-button>
</div>
</el-form>
</div>
</template>
<script>
import ElCrudField from './Field.vue'
import Helpers from './Helpers.vue'
export default {
mixins: [ Helpers ],
components: { ElCrudField },
props: {
endpoint: String,
columns: Array,
titles: { type: Object, default: () => { return {}; } },
rules: Object,
params: { type: Object, default: () => { return {}; } },
fields: { type: Object, default: () => { return {}; } },
after: { type: Function, default: null },
},
data () {
return {
loading: false,
loadingText: 'Loading...',
loadingClass: '',
entity: null,
}
},
created() {
this.fetchData();
},
methods: {
/**
* Fetch data from the server
*/
fetchData: function() {
this.loading = true;
var self = this;
this.$http.get(this.endpoint+'/edit').then(function(response) {
self.entity = Object.assign({}, response.data.entity);
self.loading = false;
}).catch(function(error) {
self.loadingText = error.response.data.message;
self.loadingClass = 'error';
self.loading = false;
});
},
/**
* Submit the form (create entity)
*/
submitForm: function(callback) {
// Validate form
this.$refs['form'].validate((valid) => {
if (valid) {
this.loading = true;
this.$http.put(this.endpoint, this.entity, { 'params': this.params }).then(response => {
this.entity = Object.assign({}, response.data.entity);
this.$notify.success( {title: 'Success', message: response.data.message });
this.loading = false;
// Perform callback
if (this.after) {
this.after(this.entity);
}
}, error => {
this.$notify.error( {title: 'Error', message: error.response.data.message });
this.loading = false;
});
} else {
this.$notify.error( {title: 'Cannot submit', message: 'Form contains errors.' });
return false;
}
});
},
}
}
</script>