11<script >
2+ import axios from ' axios'
3+ import _ from ' lodash'
24import DatasourceUtils from ' ../utils/DatasourceUtils'
35import Pagination from ' ./Pagination.vue'
6+ import MoonLoader from ' vue-spinner/src/MoonLoader.vue'
47import { EventBus } from ' ../utils/EventBus'
58export default {
6- name: ' Datasource ' ,
9+ name: ' ServerDatasource ' ,
710 components: {
8- Pagination
11+ Pagination, MoonLoader
912 },
1013 render (h ) {
1114 return (
12- < div class = " vue-datasource" >
15+ < div class = " vue-server- datasource" >
1316 < div class = " panel panel-default" >
1417 < div class = " panel-heading" >
1518 < div class = " form-inline" >
@@ -21,7 +24,6 @@ export default {
2124 < / div>
2225 < div class = " form-group pull-right" >
2326 < 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>
2527 < / div>
2628 < div class = " clearfix" >< / div>
2729 < / div>
@@ -40,6 +42,7 @@ export default {
4042 < / tr>
4143 < / tbody>
4244 < / table>
45+ { this .spinnerItem }
4346 < / div>
4447 < div class = " panel-footer" >
4548 < div class = " pull-left btn-group btn-group-actions" >
@@ -56,11 +59,11 @@ export default {
5659 },
5760 props: {
5861 /**
59- * Table information
62+ * Table source url
6063 * @type {Array}
6164 */
62- tableData : {
63- type: Array ,
65+ source : {
66+ type: String ,
6467 required: true
6568 },
6669 /**
@@ -102,21 +105,6 @@ export default {
102105 type: Array ,
103106 required: true
104107 },
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- },
120108 /**
121109 * Action buttons
122110 * @type {Array}
@@ -130,19 +118,36 @@ export default {
130118 },
131119 created () {
132120 EventBus .$on (' pagination-change' , this .changePage )
121+ this .setData ()
133122 },
134123 data () {
135124 return {
136125 perpage: 10 ,
126+ tableData: [],
127+ loading: false ,
137128 selected: null , // row and Object selected on click event
138129 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+ }
140138 }
141139 },
142140 computed: {
141+ spinnerItem () {
142+ if (this .loading ) {
143+ return < div class = " vue-spinner-wrapper" >
144+ < moon- loader>< / moon- loader>
145+ < / div>
146+ }
147+ },
143148 limitOptions () {
144149 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>
146151 })
147152 },
148153 columnItems () {
@@ -193,7 +198,23 @@ export default {
193198 searching (e ) {
194199 this .selected = null
195200 this .indexSelected = - 1
201+ this .pagination .current_page = 1
202+ this .setData ()
196203 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+ })
197218 }
198219 },
199220 watch: {
@@ -204,27 +225,53 @@ export default {
204225 perpage () {
205226 this .selected = null
206227 this .indexSelected = - 1
228+ this .pagination .current_page = 1
229+ this .setData ()
207230 this .$emit (' change' , {perpage: this .perpage , page: 1 })
208231 },
209232 tableData () {
210233 this .selected = null
211234 this .indexSelected = - 1
212- }
235+ },
236+ search: _ .debounce (function () {
237+ this .setData ()
238+ }, 500 )
213239 }
214240}
215241 </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+ }
225271}
272+
226273.pr1 {
227- padding-right : 5px
274+ padding-right : 5px ;
228275}
229276.pr2 {
230277 padding-right : 10px ;
0 commit comments