0%

Vuetify4

表格

1
2
3
4
5
6
7
8
<v-data-table
:headers=""
:items=""
:sort-by.sync=""
:sort-desc.sync=""
:loading=""
class="elevation-1"
></v-data-table>

说明:header、items 对象数组,header 对象中有 text、align、value 属性,items 对象中有 header 中 value 的值 ,sort-by.sync header 中用来排序的 value,可以通过修改它来修改用来排序的列,sort-desc.sync 判断是否倒序,可以通过控制它来控制升降序。loading 为 true 有进度条效果。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<template>
<div>
<v-data-table
:headers="headers"
:items="desserts"
:options.sync="options"
:server-items-length="totalDesserts"
:loading="loading"
class="elevation-1"
></v-data-table>
</div>
</template>
<script>
export default {
name: "HelloWorld",
data() {
return {
totalDesserts: 0,
desserts: [],
loading: true,
options: {},
headers: [
{
text: "Dessert (100g serving)",
align: "start",
sortable: false,
value: "name",
},
{ text: "Calories", value: "calories" },
{ text: "Fat (g)", value: "fat" },
],
};
},
watch: {
options: {
async handler() {
const { items, total } = await this.getDataFromApi();
this.desserts = items;
this.totalDesserts = total;
},
deep: true,
},
},
async mounted() {
const { items, total } = await this.getDataFromApi();
this.desserts = items;
this.totalDesserts = total;
},
methods: {
getDataFromApi() {
this.loading = true;
return new Promise((resolve) => {
const { sortBy, sortDesc, page, itemsPerPage } = this.options;

let items = this.getDesserts();
const total = items.length;

if (sortBy.length === 1 && sortDesc.length === 1) {
items = items.sort((a, b) => {
const sortA = a[sortBy[0]];
const sortB = b[sortBy[0]];

if (sortDesc[0]) {
if (sortA < sortB) return 1;
if (sortA > sortB) return -1;
return 0;
} else {
if (sortA < sortB) return -1;
if (sortA > sortB) return 1;
return 0;
}
});
}

if (itemsPerPage > 0) {
items = items.slice((page - 1) * itemsPerPage, page * itemsPerPage);
}

setTimeout(() => {
this.loading = false;
resolve({
items,
total,
});
}, 1000);
});
},
getDesserts() {
return [
{
name: "Frozen Yogurt",
calories: 159,
fat: 6.0,
},
{
name: "Ice cream sandwich",
calories: 237,
fat: 9.0,
},
{
name: "Eclair",
calories: 262,
fat: 16.0,
},
];
},
},
};
</script>
您的打赏将会成为我前进的动力!!