7
7
<b-form-group
8
8
label =" Title:"
9
9
label-for =" title" >
10
- <b-form-input type =" text" id =" title" name =" title" :class =" { 'is-invalid': form.errors.has('title') }" v-model =" form.title" ></b-form-input >
11
- <has-error :form =" form" field =" title" />
10
+ <b-form-input
11
+ type =" text"
12
+ id =" title"
13
+ name =" title"
14
+ :class =" { 'is-invalid': form.errors.has('title') }"
15
+ v-model =" form.title" >
16
+ </b-form-input >
17
+ <has-error
18
+ :form =" form"
19
+ field =" title" />
12
20
</b-form-group >
13
21
<b-form-group
14
22
label =" Author:"
15
- label-for =" user_name" >
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 =" 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
- <datalist id =" my-list-id" >
19
- <option v-for =" author in authors" >{{ author.name }}</option >
20
- </datalist >
21
- <has-error :form =" form" field =" user_id" />
23
+ label-for =" user_id" >
24
+ <v-select
25
+ id =" user_id"
26
+ name =" user_id"
27
+ v-model =" form.user_id"
28
+ :searchable =" true"
29
+ :clearable =" false"
30
+ :options =" authors"
31
+ :class =" { 'is-invalid': form.errors.has('user_id') }"
32
+ :reduce =" author => author.id"
33
+ :get-option-label =" getOptionLabel" >
34
+ </v-select >
35
+ <has-error
36
+ :form =" form"
37
+ field =" user_id" />
22
38
</b-form-group >
23
39
<b-form-group
24
40
label =" Description:"
25
41
label-for =" description" >
26
- <b-form-textarea id =" description" name =" description" :class =" { 'is-invalid': form.errors.has('description') }" v-model =" form.description" rows =" 3" max-rows =" 6" ></b-form-textarea >
27
- <has-error :form =" form" field =" description" />
42
+ <b-form-textarea
43
+ id =" description"
44
+ name =" description"
45
+ :class =" { 'is-invalid': form.errors.has('description') }"
46
+ v-model =" form.description"
47
+ rows =" 3"
48
+ max-rows =" 6" >
49
+ </b-form-textarea >
50
+ <has-error
51
+ :form =" form"
52
+ field =" description" />
28
53
</b-form-group >
29
54
</b-form >
30
55
<template #modal-footer >
31
56
<div class =" w-100" >
32
57
<b-button
33
58
variant =" danger"
34
59
class =" float-right"
35
- @click =" create_modal_show=false"
36
- >
60
+ @click =" create_modal_show=false" >
37
61
Close
38
62
</b-button >
39
63
<b-button
40
64
type =" submit"
41
65
variant =" primary"
42
66
class =" float-right mr-2"
43
- @click =" create_post()"
44
- >
67
+ @click =" create_post()" >
45
68
Save
46
69
</b-button >
47
70
</div >
48
71
</template >
49
72
</b-modal >
50
- <b-modal v-model =" edit_modal_show" id =" modal-edit" no-close-on-backdrop content-class =" shadow" title =" Edit Post" >
51
- <b-form @submit.prevent =" register" @keydown =" editform.onKeydown($event)" >
73
+ <b-modal
74
+ v-model =" edit_modal_show"
75
+ id =" modal-edit"
76
+ no-close-on-backdrop
77
+ content-class =" shadow"
78
+ title =" Edit Post" >
79
+ <b-form
80
+ @submit.prevent =" register"
81
+ @keydown =" editform.onKeydown($event)" >
52
82
<b-form-group
53
83
label =" Title:"
54
- label-for =" title" >
55
- <b-form-input type =" text" id =" title" name =" title" :class =" { 'is-invalid': editform.errors.has('title') }" v-model =" editform.title" ></b-form-input >
56
- <has-error :form =" editform" field =" title" />
84
+ label-for =" edit_title" >
85
+ <b-form-input
86
+ type =" text"
87
+ id =" edit_title"
88
+ name =" edit_title"
89
+ :class =" { 'is-invalid': editform.errors.has('title') }"
90
+ v-model =" editform.title" >
91
+ </b-form-input >
92
+ <has-error
93
+ :form =" editform"
94
+ field =" title" />
57
95
</b-form-group >
58
96
<b-form-group
59
97
label =" Author:"
60
- label-for =" user_name" >
61
- <!-- <b-form-select id="user" name="user" :class="{ 'is-invalid': editform.errors.has('user') }" v-model="editform.user" :options="options"></b-form-select>-->
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 >
63
- <datalist id =" my-list-id" >
64
- <option v-for =" author in authors" >{{ author.name }}</option >
65
- </datalist >
66
- <has-error :form =" editform" field =" user_id" />
98
+ label-for =" edit_user_id" >
99
+ <v-select
100
+ id =" edit_user_id"
101
+ name =" edit_user_id"
102
+ v-model =" editform.user_id"
103
+ :searchable =" true"
104
+ :clearable =" false"
105
+ :options =" authors"
106
+ :class =" { 'is-invalid': editform.errors.has('user_id') }"
107
+ :reduce =" author => author.id"
108
+ :get-option-label =" getOptionLabel" >
109
+ </v-select >
110
+ <has-error
111
+ :form =" editform"
112
+ field =" user_id" />
67
113
</b-form-group >
68
114
<b-form-group
69
115
label =" Description:"
70
- label-for =" description" >
71
- <b-form-textarea id =" description" name =" description" :class =" { 'is-invalid': editform.errors.has('description') }" v-model =" editform.description" rows =" 3" max-rows =" 6" ></b-form-textarea >
116
+ label-for =" edit_description" >
117
+ <b-form-textarea
118
+ id =" edit_description"
119
+ name =" edit_description"
120
+ :class =" { 'is-invalid': editform.errors.has('description') }"
121
+ v-model =" editform.description"
122
+ rows =" 3"
123
+ max-rows =" 6" >
124
+ </b-form-textarea >
72
125
<has-error :form =" editform" field =" description" />
73
126
</b-form-group >
74
127
</b-form >
77
130
<b-button
78
131
variant =" danger"
79
132
class =" float-right"
80
- @click =" edit_modal_show=false"
81
- >
133
+ @click =" edit_modal_show=false" >
82
134
Close
83
135
</b-button >
84
136
<b-button
85
137
type =" submit"
86
138
variant =" primary"
87
139
class =" float-right mr-2"
88
- @click =" save_post"
89
- >
140
+ @click =" save_post" >
90
141
Save
91
142
</b-button >
92
143
</div >
93
144
</template >
94
145
</b-modal >
95
- <b-table striped hover :items =" items" :fields =" fields" >
96
- <template #cell (id)="data">
97
- <b-button size =" sm" @click =" openModal(data.value)" variant =" info" >Edit</b-button >
98
- <b-button size =" sm" @click =" deleteItem(data.value)" variant =" danger" >Delete</b-button >
146
+ <b-table
147
+ striped
148
+ hover
149
+ :items =" items"
150
+ :fields =" fields" >
151
+ <template
152
+ #cell (action)="data" >
153
+ <b-button
154
+ size =" sm"
155
+ @click =" openModal(data.value)"
156
+ variant =" info" >
157
+ Edit
158
+ </b-button >
159
+ <b-button
160
+ size =" sm"
161
+ @click =" deleteItem(data.value)"
162
+ variant =" danger" >
163
+ Delete
164
+ </b-button >
99
165
</template >
100
166
</b-table >
101
167
</b-card >
@@ -115,20 +181,21 @@ export default {
115
181
authors: [],
116
182
form: new Form ({
117
183
title: ' ' ,
118
- user_name: ' ' ,
119
184
user_id: 0 ,
120
185
description: ' '
121
186
}),
122
187
editform: new Form ({
123
188
id: 0 ,
124
189
title: ' ' ,
125
- user_name: ' ' ,
126
190
user_id: 0 ,
127
191
description: ' '
128
192
})
129
193
}
130
194
},
131
195
methods: {
196
+ getOptionLabel (option ) {
197
+ return (option && option .name ) || ' '
198
+ },
132
199
async loadAuth () {
133
200
const { data } = await this .form .get (' /api/users' )
134
201
this .authors = data;
@@ -142,14 +209,10 @@ export default {
142
209
this .edit_modal_show = true
143
210
this .editform .id = item .id
144
211
this .editform .title = item .title
145
- this .editform .user_name = item .user_name
212
+ this .editform .user_id = item .user_id
146
213
this .editform .description = item .description
147
214
},
148
215
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
216
const { data } = await this .form .post (' /api/posts' )
154
217
if (data .id > 0 )
155
218
{
@@ -169,10 +232,6 @@ export default {
169
232
}
170
233
},
171
234
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
235
const { data } = await this .editform .post (' /api/posts/' + this .editform .id )
177
236
if (data .id > 0 )
178
237
{
0 commit comments