Sei*_*eif 9 sorting datatable vue.js vuetify.js
我想在数据表中使用自定义排序。我的目标是对表DESC进行排序,而不是对默认ASC进行排序。但是我不知道如何。
这是我的数据表组件的开始:
<v-data-table
:headers="headers"
:items="acts"
hide-actions
class="elevation-1"
>
<template slot="items" slot-scope="props">
<td>{{ props.item.id }}</td>
<td>{{ props.item.name }}</td>
<td class="text-xs-center">{{ props.item.provider.id }}</td>
<td class="text-xs-center">{{ props.item.category.name }}</td>
<td class="text-xs-center">{{ props.item.budget }}</td>
<td class="text-xs-center">{{ props.item.location.name }}</td>
<td class="text-xs-center">{{ props.item.deets }}</td>
<td class="text-xs-center">{{ props.item.keeping_it_100 }}</td>
<td class="text-xs-center"><img width="50" height="50" :src="props.item.inspiration.inspiration"></td>
<td class="justify-center layout px-0">....
Run Code Online (Sandbox Code Playgroud)
这是我正在使用的脚本:
<script>
export default {
data () {
return {
dialog: false,
customerSort: {
isDescending: true,// I tried this? as the kabab format throws an error
},
headers: [
{ text: 'ID', value: 'id'},
{ text: 'Name', value: 'name' },
{ text: 'Provider', value: 'provider' },
{ text: 'Category', value: 'category' },
{ text: 'Budget', value: 'budget' },
{ text: 'Country', value: 'location', sortable: true },
{ text: 'Keeping it 100%', value: 'keeping_it_100', sortable: false },
{ text: 'deets', value: 'deets', sortable: false },
{ text: 'inspiration', value: 'inspiration', sortable: false },
{ text: 'Cover', value: 'cover', sortable: false },
{ text: 'Actions', value: 'actions', sortable: false }
],
Run Code Online (Sandbox Code Playgroud)
根据文档,它是一个function prop。但是我还没有找到如何传递它的例子。
bha*_*kar 11
您可以使用如下功能:
customSort(items, index, isDesc) {
items.sort((a, b) => {
if (index === "date") {
if (!isDesc) {
return compare(a.date, b.date);
} else {
return compare(b.date, a.date);
}
}
});
return items;
}
Run Code Online (Sandbox Code Playgroud)
其中compare是一个比较a.date和b.date并返回或的函数1-1
isDesc是表传递的变量,该变量指示用户要对其进行排序的顺序。如果要按desc排序,只需在if-else条件中使用!isDesc
要在模板中使用它,只需使用
<v-data-table
:headers="headers"
:items="Data"
:custom-sort="customSort"
>
<template slot="items" slot-scope="props">
<td class="font-weight-black">{{ props.item.date }}</td>
<td class="text-xs-right">{{ props.item.time }}</td>
<td class="text-xs-right">{{ props.item.name }}</td>
</template>
</v-data-table>
Run Code Online (Sandbox Code Playgroud)
为了确保您的其他字段仍然可以正常排序使用
customSort(items, index, isDesc) {
items.sort((a, b) => {
if (index === "date") {
if (!isDesc) {
return dateHelp.compare(a.date, b.date);
} else {
return dateHelp.compare(b.date, a.date);
}
} else {
if (!isDesc) {
return a[index] < b[index] ? -1 : 1;
} else {
return b[index] < a[index] ? -1 : 1;
}
}
});
return items;
}
Run Code Online (Sandbox Code Playgroud)
小智 9
虽然这是个老问题了...
对于仅一列的特殊排序,您可以使用 headers 数组中的属性 sort。另请参阅https://vuetifyjs.com/en/api/v-data-table/#headers
就像这样:
// in data ...
headers: [
...
{
text: "Date",
sortable: true,
value: "date",
sort: (a, b) => a.time_stamp - b.time_stamp
},
...
]
Run Code Online (Sandbox Code Playgroud)
像使用它一样
<v-data-table
:headers="headers"
...
>
Run Code Online (Sandbox Code Playgroud)
根据这个答案代码custom-filter,我尝试使用custom-sort.
如果您将其应用于您的代码,请参考此答案。
通过以下代码,当我单击“卡路里”标题时,我已确认排序。
我的代码笔
new Vue({
el: '#app',
data() {
return {
food: [
{ name: 'Bakchoi', type: 'vegetable', calories: 100 },
{ name: 'Pork', type: 'meat', calories: 200 },
{ name: 'Chicken Thigh', type: 'meat', calories: 300 },
{ name: 'Watermelon', type: 'fruit', calories: 10 },
],
headers: [
{ text: 'Name', align: 'left', value: 'name' },
{ text: 'Food Type', align: 'left', value: 'type' },
{ text: 'Calories', align: 'left', value: 'calories' },
],
search: '',
};
},
methods: {
customSort(items, index, isDescending) {
// The following is informations as far as I researched.
// items: 'food' items
// index: Enabled sort headers value. (black arrow status).
// isDescending: Whether enabled sort headers is desc
items.sort((a, b) => {
if (index === 'calories') {
if (isDescending) {
return b.calories - a.calories;
} else {
return a.calories - b.calories;
}
}
});
return items;
}
}
})
Run Code Online (Sandbox Code Playgroud)
<script src="https://unpkg.com/vue@2.4.2/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify@0.15.2/dist/vuetify.js"></script>
<link rel="stylesheet" href="https://unpkg.com/vuetify@0.15.2/dist/vuetify.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">
<div id="app">
<v-app>
<v-select
label="Food Type"
:items="['vegetable', 'meat', 'fruit']"
v-model="search"
></v-select>
<v-data-table
:headers="headers"
:items="food"
:search="search"
:custom-sort="customSort"
hide-actions
>
<template slot="items" scope="{ item }">
<td>{{ item.name }}</td>
<td>{{ item.type }}</td>
<td>{{ item.calories }}</td>
</template>
</v-data-table>
</v-app>
</div>
Run Code Online (Sandbox Code Playgroud)
注意:以下答案适用于 Vuetify 1.5.x
有点晚了,如果您只想按单个字段降序排序,那么自定义排序不是您想要使用的,您最好使用:pagination.syncprop
当您想要更改比较函数的行为时,可以使用自定义排序(例如,根据字符串的反向或小写版本进行排序,或者以“DD-MM-YYYY”格式对日期字符串进行正确排序)。
如果您想使用默认的降序功能,请使用:pagination.syncprop,如下所示:
<v-data-table
:headers="headers"
:items="acts"
:pagination.sync="pagination"
>
<template v-slot:items="props">...</template>
</v-data-table>
Run Code Online (Sandbox Code Playgroud)
在您的脚本中,设置pagination:
data () {
return {
pagination: {
sortBy: 'id', // The field that you're sorting by
descending: true
}
}
}
Run Code Online (Sandbox Code Playgroud)
这指定您希望表最初按 id 降序排序 - id 可以更改为数据集中的任何字段名称。
值得注意的是,这仅指定默认行为,如果您为其他标题启用了排序,用户仍然可以按任何字段对表进行排序。
| 归档时间: |
|
| 查看次数: |
10546 次 |
| 最近记录: |