Skip to content
This repository was archived by the owner on Jun 10, 2021. It is now read-only.

Commit 47e3327

Browse files
authored
Merge pull request #50 from coderdiaz/improve/ISSUE-38
Improve/issue 38
2 parents d78220b + e0a5ee1 commit 47e3327

File tree

3 files changed

+30
-41
lines changed

3 files changed

+30
-41
lines changed

src/App.vue

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,19 @@
11
<script>
2+
import axios from 'axios'
23
import Datasource from './components/ServerDatasource.vue'
34
export default {
45
name: 'app',
56
components: {
67
Datasource
78
},
9+
mounted () {
10+
this.getSource()
11+
},
812
render (h) {
913
return (
1014
<div id="app">
11-
<datasource source="http://young-falls-97690.herokuapp.com/getusers" limits={this.limits} actions={this.actions} columns={this.columns}></datasource>
15+
<h2 class="text-center mb3">Vue Datasource</h2>
16+
<datasource source={this.information} limits={this.limits} actions={this.actions} columns={this.columns} on-change={this.change}></datasource>
1217
</div>
1318
)
1419
},
@@ -21,19 +26,15 @@ export default {
2126
key: 'id'
2227
},
2328
{
24-
name: 'Name',
25-
key: 'name'
29+
name: 'First Name',
30+
key: 'first_name'
2631
},
2732
{
28-
name: 'City',
29-
key: 'city',
33+
name: 'Last Name',
34+
key: 'last_name',
3035
render: function (value) {
3136
return `<strong>${value}</strong>`
3237
}
33-
},
34-
{
35-
name: 'Company',
36-
key: 'company'
3738
}
3839
],
3940
actions: [
@@ -98,14 +99,19 @@ export default {
9899
}
99100
},
100101
methods: {
102+
getSource () {
103+
axios.get(`https://reqres.in/api/users?per_page=${this.perpage}&page=${this.page}`).then((response) => {
104+
this.information = response.data.data
105+
})
106+
},
101107
change (value) {
102108
this.page = value.page
103109
this.perpage = value.perpage
104-
this.getData()
110+
this.getSource()
105111
},
106112
searching (value) {
107113
this.search = value
108-
this.getData()
114+
this.getSource()
109115
}
110116
}
111117
}
@@ -115,6 +121,10 @@ export default {
115121
@import url('https://fonts.googleapis.com/css?family=Roboto:300,400,500,700');
116122
@import "./assets/app.css";
117123
#app {
118-
font-family: 'Roboto' !important
124+
font-family: 'Roboto' !important;
125+
margin: 150px 0;
126+
}
127+
.mb3 {
128+
margin-bottom: 30px;
119129
}
120130
</style>

src/components/ServerDatasource.vue

Lines changed: 8 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
<script>
2-
import axios from 'axios'
3-
import _ from 'lodash'
42
import DatasourceUtils from '../utils/DatasourceUtils'
53
import Pagination from './Pagination.vue'
64
import { EventBus } from '../utils/EventBus'
@@ -20,7 +18,7 @@ export default {
2018
</select>
2119
</div>
2220
<div class="form-group pull-right">
23-
<input class="form-control mr1" type="text" on-input={ (e) => this.sync('search', e.target.value) } placeholder={this.translation.placeholder_search} />
21+
<input class="form-control mr1" type="text" on-input={ (e) => this.searching(e) } placeholder={this.translation.placeholder_search} />
2422
</div>
2523
<div class="clearfix"></div>
2624
</div>
@@ -55,11 +53,11 @@ export default {
5553
},
5654
props: {
5755
/**
58-
* Table source url
56+
* Table source data
5957
* @type {Array}
6058
*/
6159
source: {
62-
type: String,
60+
type: Array,
6361
required: true
6462
},
6563
/**
@@ -114,12 +112,10 @@ export default {
114112
},
115113
created () {
116114
EventBus.$on('pagination-change', this.changePage)
117-
this.setData()
118115
},
119116
data () {
120117
return {
121118
perpage: 10,
122-
tableData: [],
123119
selected: null, // row and Object selected on click event
124120
indexSelected: -1, // index row selected on click event
125121
search: '', // word to search in the table,
@@ -144,12 +140,12 @@ export default {
144140
})
145141
},
146142
columnObjects () {
147-
if (this.tableData.length === 0) {
143+
if (this.source.length === 0) {
148144
return <tr class="text-center">
149145
<td colspan={ this.columns.length }>{ this.translation.records_not_found }</td>
150146
</tr>
151147
} else {
152-
return this.tableData.map((row, index) => {
148+
return this.source.map((row, index) => {
153149
let columns = this.columns.map((column, index) => {
154150
return <td domPropsInnerHTML={ this.fetchFromObject(row, column.key, column.render) }></td>
155151
})
@@ -187,19 +183,7 @@ export default {
187183
this.selected = null
188184
this.indexSelected = -1
189185
this.pagination.current_page = 1
190-
this.setData()
191-
this.$emit('searching', this.search)
192-
},
193-
setData () {
194-
axios.get(`${this.source}?per_page=${this.perpage}&page=${this.pagination.current_page}&search=${this.search}`)
195-
.then((response) => {
196-
this.tableData = response.data.data
197-
this.pagination = response.data.pagination
198-
this.perpage = this.pagination.per_page
199-
})
200-
.catch((error) => {
201-
console.warn(`[VueDatasource] ${error}`)
202-
})
186+
this.$emit('searching', e.target.value)
203187
}
204188
},
205189
watch: {
@@ -211,16 +195,12 @@ export default {
211195
this.selected = null
212196
this.indexSelected = -1
213197
this.pagination.current_page = 1
214-
this.setData()
215198
this.$emit('change', {perpage: this.perpage, page: 1})
216199
},
217-
tableData () {
200+
source () {
218201
this.selected = null
219202
this.indexSelected = -1
220-
},
221-
search: _.debounce(function () {
222-
this.setData()
223-
}, 500)
203+
}
224204
}
225205
}
226206
</script>

src/utils/DatasourceUtils.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ export default {
2727
this.selected = null
2828
this.indexSelected = -1
2929
this.pagination.current_page = page
30-
this.setData()
3130
this.$emit('change', { perpage: this.perpage, page: page })
3231
},
3332

0 commit comments

Comments
 (0)