12
12
</b-form-group >
13
13
<b-form-group
14
14
label =" Author:"
15
- label-for =" author " >
15
+ label-for =" user_name " >
16
16
<!-- <b-form-select id="author" name="author" :class="{ 'is-invalid': form.errors.has('author') }" v-model="form.author" :options="options"></b-form-select>-->
17
- <b-form-input id =" author " name =" author " :class =" { 'is-invalid': form.errors.has('author ') }" v-model =" form.user " list =" my-list-id" ></b-form-input >
17
+ <b-form-input id =" user_name " name =" user_name " :class =" { 'is-invalid': form.errors.has('user_id ') }" v-model =" form.user_name " list =" my-list-id" ></b-form-input >
18
18
<datalist id =" my-list-id" >
19
- <option v-for =" option in options " >{{ option.text }}</option >
19
+ <option v-for =" author in authors " >{{ author.name }}</option >
20
20
</datalist >
21
- <has-error :form =" form" field =" author " />
21
+ <has-error :form =" form" field =" user_id " />
22
22
</b-form-group >
23
23
<b-form-group
24
24
label =" Description:"
40
40
type =" submit"
41
41
variant =" primary"
42
42
class =" float-right mr-2"
43
+ @click =" create_post()"
43
44
>
44
45
Save
45
46
</b-button >
56
57
</b-form-group >
57
58
<b-form-group
58
59
label =" Author:"
59
- label-for =" author " >
60
+ label-for =" user_name " >
60
61
<!-- <b-form-select id="user" name="user" :class="{ 'is-invalid': editform.errors.has('user') }" v-model="editform.user" :options="options"></b-form-select>-->
61
- <b-form-input id =" author " name =" author " :class =" { 'is-invalid': form .errors.has('author ') }" v-model =" form.author " list =" my-list-id" ></b-form-input >
62
+ <b-form-input id =" user_name " name =" user_name " :class =" { 'is-invalid': editform .errors.has('user_id ') }" v-model =" editform.user_name " list =" my-list-id" ></b-form-input >
62
63
<datalist id =" my-list-id" >
63
- <option v-for =" option in options " >{{ option.text }}</option >
64
+ <option v-for =" author in authors " >{{ author.name }}</option >
64
65
</datalist >
65
- <has-error :form =" editform" field =" author " />
66
+ <has-error :form =" editform" field =" user_id " />
66
67
</b-form-group >
67
68
<b-form-group
68
69
label =" Description:"
84
85
type =" submit"
85
86
variant =" primary"
86
87
class =" float-right mr-2"
88
+ @click =" save_post"
87
89
>
88
90
Save
89
91
</b-button >
90
92
</div >
91
93
</template >
92
94
</b-modal >
93
- <b-table striped hover :items =" items" >
94
- <template #cell (action )="data">
95
+ <b-table striped hover :items =" items" :fields = " fields " >
96
+ <template #cell (id )="data">
95
97
<b-button size =" sm" @click =" openModal(data.value)" variant =" info" >Edit</b-button >
96
- <b-button size =" sm" @click =" delete (data.value)" variant =" danger" >Delete</b-button >
98
+ <b-button size =" sm" @click =" deleteItem (data.value)" variant =" danger" >Delete</b-button >
97
99
</template >
98
100
</b-table >
99
101
</b-card >
@@ -108,40 +110,109 @@ export default {
108
110
return {
109
111
create_modal_show: false ,
110
112
edit_modal_show: false ,
111
- items: [
112
- { title: ' Title1' , author: ' Dickerson' , description: ' Macdonald' , action: 2 },
113
- { title: ' Title2' , author: ' Larsen' , description: ' Shaw' , action: 1 },
114
- { title: ' Title3' , author: ' Geneva' , description: ' Wilson' , action: 3 },
115
- { title: ' Title4' , author: ' Jami' , description: ' Carney' , action: 4 }
116
- ],
117
- options: [
118
- { value: ' a' , text: ' This is First option' },
119
- { value: ' b' , text: ' Selected Option' }
120
- ],
113
+ fields: [' title' , ' user_name' , ' description' , ' action' ],
114
+ items: [],
115
+ authors: [],
121
116
form: new Form ({
122
117
title: ' ' ,
123
- author: " a" ,
118
+ user_name: ' ' ,
119
+ user_id: 0 ,
124
120
description: ' '
125
121
}),
126
122
editform: new Form ({
127
- id: ' ' ,
123
+ id: 0 ,
128
124
title: ' ' ,
129
- author: " Geneva" ,
125
+ user_name: ' ' ,
126
+ user_id: 0 ,
130
127
description: ' '
131
128
})
132
129
}
133
130
},
134
131
methods: {
135
- openModal (id ) {
132
+ async loadAuth () {
133
+ const { data } = await this .form .get (' /api/users' )
134
+ this .authors = data;
135
+ },
136
+ async loadPosts () {
137
+ const { data } = await this .form .get (' /api/posts' )
138
+ this .items = data;
139
+ },
140
+ openModal (item_id ) {
141
+ let item = this .items .find (({id})=> id=== item_id)
136
142
this .edit_modal_show = true
137
- this .editform .id = " Title3 "
138
- this .editform .title = " Title3 "
139
- this .editform .author = " Geneva "
140
- this .editform .description = " Wilson "
143
+ this .editform .id = item . id
144
+ this .editform .title = item . title
145
+ this .editform .user_name = item . user_name
146
+ this .editform .description = item . description
141
147
},
142
- delete (id ) {
143
-
148
+ async create_post () {
149
+ let user = this .authors .find (({name})=> name=== this .form .user_name );
150
+ let user_id = 0
151
+ if (user) user_id = user .id
152
+ this .form .user_id = user_id;
153
+ const { data } = await this .form .post (' /api/posts' )
154
+ if (data .id > 0 )
155
+ {
156
+ this .$bvToast .toast (' The post was created successfully' , {
157
+ title: ` Notification` ,
158
+ variant: ' success' ,
159
+ solid: true
160
+ })
161
+ this .create_modal_show = false ;
162
+ this .loadPosts ();
163
+ } else {
164
+ this .$bvToast .toast (' Post creating operation was failed' , {
165
+ title: ` Notification` ,
166
+ variant: ' danger' ,
167
+ solid: true
168
+ })
169
+ }
170
+ },
171
+ async save_post () {
172
+ let user = this .authors .find (({name})=> name=== this .editform .user_name );
173
+ let user_id = 0
174
+ if (user) user_id = user .id
175
+ this .editform .user_id = user_id;
176
+ const { data } = await this .editform .post (' /api/posts/' + this .editform .id )
177
+ if (data .id > 0 )
178
+ {
179
+ this .$bvToast .toast (' The post was updated successfully' , {
180
+ title: ` Notification` ,
181
+ variant: ' success' ,
182
+ solid: true
183
+ })
184
+ this .edit_modal_show = false ;
185
+ this .loadPosts ();
186
+ } else {
187
+ this .$bvToast .toast (' Post updating operation was failed' , {
188
+ title: ` Notification` ,
189
+ variant: ' danger' ,
190
+ solid: true
191
+ })
192
+ }
193
+ },
194
+ async deleteItem (id ) {
195
+ const { data } = await this .editform .delete (' /api/posts/' + id)
196
+ if (data .status === " success" )
197
+ {
198
+ this .$bvToast .toast (' The post was deleted successfully' , {
199
+ title: ` Notification` ,
200
+ variant: ' success' ,
201
+ solid: true
202
+ })
203
+ } else {
204
+ this .$bvToast .toast (' Post deleting operation was failed' , {
205
+ title: ` Notification` ,
206
+ variant: ' danger' ,
207
+ solid: true
208
+ })
209
+ }
210
+ this .loadPosts ();
144
211
}
212
+ },
213
+ mounted () {
214
+ this .loadAuth ();
215
+ this .loadPosts ();
145
216
}
146
217
}
147
218
</script >
0 commit comments