naive-ui/demo/documentation/components/dataTable/enUS/ajaxUsage.md

167 lines
3.7 KiB
Markdown
Raw Normal View History

2020-02-04 22:43:40 +08:00
# Async
```html
<n-data-table
2020-02-03 02:42:51 +08:00
remote
ref="table"
:columns="columns"
:data="data"
:loading="loading"
:pagination="pagination"
2020-02-03 02:42:51 +08:00
:paging="false"
2020-01-31 17:02:33 +08:00
:row-key="rowKey"
@sorter-change="handleSorterChange"
@filters-change="handleFiltersChange"
@page-change="handlePageChange"
/>
```
```js
2020-02-03 02:42:51 +08:00
const Column1 = {
title: 'Column1',
key: 'column1',
sorter: true,
2020-02-03 02:42:51 +08:00
sortOrder: false
}
2020-02-03 02:42:51 +08:00
const Column2 = {
title: 'Column2',
key: 'column2',
filter: true,
2020-01-10 14:43:10 +08:00
filterOptionValues: [],
filterOptions: [
2020-02-03 02:42:51 +08:00
{ label: 'Value1', value: 1 },
{ label: 'Value2', value: 2 }
2020-01-10 14:43:10 +08:00
]
}
const columns = [
2020-02-03 02:42:51 +08:00
Column1,
Column2,
{
2020-02-03 02:42:51 +08:00
title: 'Column3',
key: 'column3'
}
]
2020-02-03 02:42:51 +08:00
const data = Array.apply(null, { length: 987 }).map((_, index) => {
return {
column1: index,
column2: index % 2 + 1,
column3: 'a' + index
}
})
function query (page, pageSize = 10, order = 'ascend', filterValues = []) {
return new Promise(resolve => {
const copiedData = data.map(v => v)
const orderedData = order === 'descend' ? copiedData.reverse() : copiedData
const filteredData = filterValues.length ?
orderedData.filter(row => filterValues.includes(row.column2)) :
orderedData
const pagedData = filteredData.slice((page - 1) * pageSize, page * pageSize)
const pageCount = Math.ceil(filteredData.length / pageSize)
setTimeout(() => resolve({
pageCount,
data: pagedData
}), 1500)
})
2020-01-10 14:43:10 +08:00
}
export default {
data () {
return {
data: [],
columns,
2020-02-03 02:42:51 +08:00
Column1,
Column2,
2020-01-10 14:43:10 +08:00
pagination: {
page: 1,
2020-02-03 02:42:51 +08:00
pageCount: 1,
pageSize: 10
2020-01-10 14:43:10 +08:00
},
2020-02-03 02:42:51 +08:00
loading: true,
}
},
2020-01-10 14:43:10 +08:00
mounted () {
2020-02-03 02:42:51 +08:00
query(
this.pagination.page,
this.pagination.pageSize,
this.Column1.sortOrder,
this.Column2.filterOptionValues
).then(data => {
this.data = data.data
this.pagination.pageCount = data.pageCount
this.loading = false
})
},
methods: {
2020-02-03 02:42:51 +08:00
rowKey (rowData) {
return rowData.column1
},
handleSorterChange (sorter) {
2020-02-03 02:42:51 +08:00
if (!sorter || sorter.columnKey === 'column1') {
if (!this.loading) {
this.loading = true
query(
this.pagination.page,
this.pagination.pageSize,
!sorter ? false : sorter.order,
this.Column2.filterOptionValues
).then(data => {
this.Column1.sortOrder = !sorter ? false : sorter.order,
this.data = data.data
this.pagination.pageCount = data.pageCount
this.loading = false
})
}
2020-01-10 14:43:10 +08:00
}
2020-02-03 02:42:51 +08:00
},
handleFiltersChange (filters) {
if (!this.loading) {
this.loading = true
const filterValues = filters
.filter(filter => filter.columnKey === 'column2')
.map(filter => filter.filterOptionValue)
query(
this.pagination.page,
this.pagination.pageSize,
this.Column1.sortOrder,
filterValues
).then(data => {
this.Column2.filterOptionValues = filterValues
this.data = data.data
this.pagination.pageCount = data.pageCount
this.loading = false
2020-01-10 14:43:10 +08:00
})
}
},
handlePageChange (currentPage) {
2020-02-03 02:42:51 +08:00
if (!this.loading) {
this.loading = true
console.log(currentPage)
query(
currentPage,
this.pagination.pageSize,
this.Column1.sortOrder,
this.Column2.filterOptionValues
).then(data => {
this.data = data.data
console.log(data.data)
this.pagination.page = currentPage
this.pagination.pageCount = data.pageCount
this.loading = false
})
}
}
}
}
```
```css
.n-button {
margin: 0 8px 12px 0;
}
```