1
1
<script >
2
+ import axios from ' axios'
3
+ import _ from ' lodash'
2
4
import DatasourceUtils from ' ../utils/DatasourceUtils'
3
5
import Pagination from ' ./Pagination.vue'
6
+ import MoonLoader from ' vue-spinner/src/MoonLoader.vue'
4
7
import { EventBus } from ' ../utils/EventBus'
5
8
export default {
6
- name: ' Datasource ' ,
9
+ name: ' ServerDatasource ' ,
7
10
components: {
8
- Pagination
11
+ Pagination, MoonLoader
9
12
},
10
13
render (h ) {
11
14
return (
12
- < div class = " vue-datasource" >
15
+ < div class = " vue-server- datasource" >
13
16
< div class = " panel panel-default" >
14
17
< div class = " panel-heading" >
15
18
< div class = " form-inline" >
@@ -21,7 +24,6 @@ export default {
21
24
< / div>
22
25
< div class = " form-group pull-right" >
23
26
< input class = " form-control mr1" type= " text" on- input= { (e ) => this .sync (' search' , e .target .value ) } placeholder= {this .translation .placeholder_search } / >
24
- < button type= " button" class = " btn btn-primary" on- click= { (e ) => this .searching (e) }> { this .translation .search }< / button>
25
27
< / div>
26
28
< div class = " clearfix" >< / div>
27
29
< / div>
@@ -40,6 +42,7 @@ export default {
40
42
< / tr>
41
43
< / tbody>
42
44
< / table>
45
+ { this .spinnerItem }
43
46
< / div>
44
47
< div class = " panel-footer" >
45
48
< div class = " pull-left btn-group btn-group-actions" >
@@ -56,11 +59,11 @@ export default {
56
59
},
57
60
props: {
58
61
/**
59
- * Table information
62
+ * Table source url
60
63
* @type {Array}
61
64
*/
62
- tableData : {
63
- type: Array ,
65
+ source : {
66
+ type: String ,
64
67
required: true
65
68
},
66
69
/**
@@ -102,21 +105,6 @@ export default {
102
105
type: Array ,
103
106
required: true
104
107
},
105
- /**
106
- * Pagination information about the table data
107
- * @type {Object}
108
- */
109
- pagination: {
110
- type: Object ,
111
- default () {
112
- return {
113
- total: 0 ,
114
- to: 0 ,
115
- from: 0 ,
116
- per_page: 15
117
- }
118
- }
119
- },
120
108
/**
121
109
* Action buttons
122
110
* @type {Array}
@@ -130,19 +118,36 @@ export default {
130
118
},
131
119
created () {
132
120
EventBus .$on (' pagination-change' , this .changePage )
121
+ this .setData ()
133
122
},
134
123
data () {
135
124
return {
136
125
perpage: 10 ,
126
+ tableData: [],
127
+ loading: false ,
137
128
selected: null , // row and Object selected on click event
138
129
indexSelected: - 1 , // index row selected on click event
139
- search: ' ' // word to search in the table
130
+ search: ' ' , // word to search in the table,
131
+ pagination: {
132
+ total: 0 ,
133
+ to: 0 ,
134
+ from: 0 ,
135
+ per_page: 10 ,
136
+ current_page: 1
137
+ }
140
138
}
141
139
},
142
140
computed: {
141
+ spinnerItem () {
142
+ if (this .loading ) {
143
+ return < div class = " vue-spinner-wrapper" >
144
+ < moon- loader>< / moon- loader>
145
+ < / div>
146
+ }
147
+ },
143
148
limitOptions () {
144
149
return this .limits .map ((limit , index ) => {
145
- return < option value= { limit } selected= { this .perpage === limit }> { limit }< / option>
150
+ return < option value= { limit } selected= { parseInt ( this .perpage ) === parseInt ( limit) }> { limit }< / option>
146
151
})
147
152
},
148
153
columnItems () {
@@ -193,7 +198,23 @@ export default {
193
198
searching (e ) {
194
199
this .selected = null
195
200
this .indexSelected = - 1
201
+ this .pagination .current_page = 1
202
+ this .setData ()
196
203
this .$emit (' searching' , this .search )
204
+ },
205
+ setData () {
206
+ this .loading = true
207
+ axios .get (` ${ this .source } ?per_page=${ this .perpage } &page=${ this .pagination .current_page } &search=${ this .search } ` )
208
+ .then ((response ) => {
209
+ this .loading = false
210
+ this .tableData = response .data .data
211
+ this .pagination = response .data .pagination
212
+ this .perpage = this .pagination .per_page
213
+ })
214
+ .catch ((error ) => {
215
+ this .loading = false
216
+ console .warn (` [VueDatasource] ${ error} ` )
217
+ })
197
218
}
198
219
},
199
220
watch: {
@@ -204,27 +225,53 @@ export default {
204
225
perpage () {
205
226
this .selected = null
206
227
this .indexSelected = - 1
228
+ this .pagination .current_page = 1
229
+ this .setData ()
207
230
this .$emit (' change' , {perpage: this .perpage , page: 1 })
208
231
},
209
232
tableData () {
210
233
this .selected = null
211
234
this .indexSelected = - 1
212
- }
235
+ },
236
+ search: _ .debounce (function () {
237
+ this .setData ()
238
+ }, 500 )
213
239
}
214
240
}
215
241
</script >
216
- <style scoped>
217
- .vue-datasource .panel-body {
218
- padding : 0
219
- }
220
- .vue-datasource table {
221
- margin-bottom : 0
222
- }
223
- .vue-datasource .panel-footer .btn-group-actions {
224
- margin : 10px 0
242
+ <style lang="scss" scoped>
243
+ .vue-server-datasource {
244
+ .panel-body {
245
+ position : relative ;
246
+ padding : 0 ;
247
+ }
248
+ table {
249
+ margin-bottom : 0 ;
250
+ }
251
+ .panel-footer {
252
+ .btn-group-actions {
253
+ margin : 10px 0 ;
254
+ }
255
+ }
256
+ .vue-spinner-wrapper {
257
+ position : absolute ;
258
+ top : 0 ;
259
+ width : 100% ;
260
+ height : 100% ;
261
+ background : rgba (229 , 229 , 229 , 0.5 );
262
+
263
+ .v-spinner {
264
+ position : absolute ;
265
+ top : 50% ;
266
+ left : 50% ;
267
+ margin-left : -25px ;
268
+ margin-top : -50px ;
269
+ }
270
+ }
225
271
}
272
+
226
273
.pr1 {
227
- padding-right : 5px
274
+ padding-right : 5px ;
228
275
}
229
276
.pr2 {
230
277
padding-right : 10px ;
0 commit comments