2019-12-11 18:45:44 +08:00
|
|
|
# Ajax
|
|
|
|
|
2019-12-13 15:20:50 +08:00
|
|
|
> filter,sorter,pagination should use `custom`
|
|
|
|
|
2019-12-11 18:45:44 +08:00
|
|
|
```html
|
2020-01-05 22:46:13 +08:00
|
|
|
<n-button @click="sortName">sort name</n-button>
|
|
|
|
<n-button @click="clearFilters">clear filters</n-button>
|
2019-12-11 18:45:44 +08:00
|
|
|
<n-button @click="clearFiltersAndSorters">clear filters and sorters</n-button>
|
|
|
|
|
2019-12-31 21:01:24 +08:00
|
|
|
<n-data-table
|
2019-12-11 18:45:44 +08:00
|
|
|
ref="table"
|
|
|
|
:columns="columns"
|
|
|
|
:data="data"
|
|
|
|
:loading="loading"
|
|
|
|
:pagination="pagination"
|
2020-01-05 22:46:13 +08:00
|
|
|
@sorter-change="handleSorterChange"
|
|
|
|
@filters-change="handleFiltersChange"
|
|
|
|
@page-change="handlePageChange"
|
|
|
|
/>
|
2019-12-11 18:45:44 +08:00
|
|
|
```
|
|
|
|
|
|
|
|
```js
|
2020-01-05 22:46:13 +08:00
|
|
|
const nameColumn = {
|
|
|
|
title: 'Name',
|
|
|
|
key: 'name',
|
|
|
|
sorter: true,
|
|
|
|
render (h, row) {
|
|
|
|
return (
|
|
|
|
<span>
|
|
|
|
{ row.name.first } { row.name.last }
|
|
|
|
</span>
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const columns = [
|
|
|
|
nameColumn,
|
|
|
|
{
|
|
|
|
title: 'Gender',
|
|
|
|
key: 'gender',
|
|
|
|
filterable: true,
|
|
|
|
filterOptions: [
|
|
|
|
{ label: 'Male', value: 'male' },
|
|
|
|
{ label: 'Female', value: 'female' }
|
|
|
|
]
|
|
|
|
},
|
|
|
|
{
|
|
|
|
title: 'Email',
|
|
|
|
key: 'email'
|
|
|
|
}
|
|
|
|
]
|
2019-12-11 18:45:44 +08:00
|
|
|
|
|
|
|
export default {
|
2020-01-05 22:46:13 +08:00
|
|
|
data () {
|
2019-12-11 18:45:44 +08:00
|
|
|
return {
|
|
|
|
data: [],
|
2020-01-05 22:46:13 +08:00
|
|
|
columns,
|
|
|
|
nameColumn,
|
2019-12-11 18:45:44 +08:00
|
|
|
loading: false,
|
|
|
|
total: 0
|
2020-01-05 22:46:13 +08:00
|
|
|
}
|
2019-12-11 18:45:44 +08:00
|
|
|
},
|
|
|
|
mounted() {
|
|
|
|
this.getData().then(data => {
|
2020-01-05 22:46:13 +08:00
|
|
|
this.data = data.results
|
|
|
|
this.total = 100
|
|
|
|
})
|
2019-12-11 18:45:44 +08:00
|
|
|
},
|
|
|
|
computed: {
|
2020-01-05 22:46:13 +08:00
|
|
|
pagination () {
|
|
|
|
if (!this.total) return false
|
|
|
|
return { pageCount: this.total, limit: false }
|
2019-12-11 18:45:44 +08:00
|
|
|
}
|
|
|
|
},
|
|
|
|
methods: {
|
2020-01-05 22:46:13 +08:00
|
|
|
getData (params = {}) {
|
|
|
|
this.loading = true
|
2019-12-11 18:45:44 +08:00
|
|
|
if (!params.results) {
|
2020-01-05 22:46:13 +08:00
|
|
|
params.results = 8
|
2019-12-11 18:45:44 +08:00
|
|
|
}
|
2020-01-05 22:46:13 +08:00
|
|
|
let url = 'https://randomuser.me/api'
|
|
|
|
let paramsArr = []
|
2019-12-11 18:45:44 +08:00
|
|
|
Object.keys(params).forEach(key => {
|
|
|
|
if (Array.isArray(params[key])) {
|
|
|
|
params[key].forEach(value => {
|
2020-01-05 22:46:13 +08:00
|
|
|
paramsArr.push(`${key}[]=${value}`)
|
|
|
|
})
|
|
|
|
} else paramsArr.push(`${key}=${params[key]}`)
|
|
|
|
})
|
2019-12-11 18:45:44 +08:00
|
|
|
if (paramsArr.length) {
|
2020-01-05 22:46:13 +08:00
|
|
|
url = url + '?' + paramsArr.join('&')
|
2019-12-11 18:45:44 +08:00
|
|
|
}
|
|
|
|
return fetch(url)
|
|
|
|
.then(res => res.json())
|
|
|
|
.finally(() => {
|
2020-01-05 22:46:13 +08:00
|
|
|
this.loading = false
|
|
|
|
})
|
2019-12-11 18:45:44 +08:00
|
|
|
},
|
2020-01-05 22:46:13 +08:00
|
|
|
sortName () {
|
|
|
|
this.$refs.table.sort('name', 'ascend')
|
2019-12-11 18:45:44 +08:00
|
|
|
},
|
2020-01-05 22:46:13 +08:00
|
|
|
handleSorterChange (sorter) {
|
|
|
|
this.getData().then(data => {
|
|
|
|
this.data = data.results
|
|
|
|
this.total = 100
|
|
|
|
})
|
2019-12-11 18:45:44 +08:00
|
|
|
},
|
2020-01-05 22:46:13 +08:00
|
|
|
handleFiltersChange (filters) {
|
|
|
|
this.getData().then(data => {
|
|
|
|
this.data = data.results
|
|
|
|
this.total = 50
|
|
|
|
})
|
2019-12-11 18:45:44 +08:00
|
|
|
},
|
2020-01-05 22:46:13 +08:00
|
|
|
handlePageChange (currentPage) {
|
|
|
|
console.log('handle page change')
|
|
|
|
this.getData({
|
|
|
|
page: currentPage
|
|
|
|
}).then(data => {
|
|
|
|
this.data = data.results
|
|
|
|
this.total = 50
|
|
|
|
})
|
|
|
|
},
|
|
|
|
clearFilters () {
|
|
|
|
this.$refs.table.clearFilters()
|
|
|
|
},
|
|
|
|
clearFiltersAndSorters () {
|
|
|
|
this.$refs.table.clearFilters()
|
|
|
|
this.$refs.table.clearSorter()
|
2019-12-11 18:45:44 +08:00
|
|
|
}
|
|
|
|
}
|
2020-01-05 22:46:13 +08:00
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
```css
|
|
|
|
.n-button {
|
|
|
|
margin: 0 8px 12px 0;
|
|
|
|
}
|
2019-12-11 18:45:44 +08:00
|
|
|
```
|