vue.js - Vue排序表(从高到低排序)
问题描述
嗨,我是 vuejs 的新手。我想按从最高到最低对表格进行排序。但是我安装了 vue-sorted-table 库。但是当我尝试运行代码时,数据总是从最低到最高返回。谁能帮我?谢谢..
这是代码:
<template>
<div id="app">
<sorted-table :values="values" @sort-table="onSort">
<thead>
<tr>
<th scope="col" style="text-align: left; width: 10rem;">
<sort-link name="id">ID</sort-link>
</th>
</tr>
</thead>
<template #body="sort">
<tbody>
<tr v-for="value in sort.values" :key="value.id">
<td>{{ value.id }}</td>
</tr>
</tbody>
</template>
</sorted-table>
</div>
</template>
<script>
import { ChevronUpIcon } from "vue-feather-icons";
export default {
name: "App",
data: function() {
return {
values: [{ id: 2 }, { id: 1 }, { id: 3 }],
sortBy: "",
sortDir: ""
};
},
components: {
ChevronUpIcon
},
methods: {
onSort(sortBy, sortDir) {
this.sortBy = sortBy;
this.sortDir = sortDir;
}
}
};
</script>
<style>
.feather {
transition: 0.3s transform ease-in-out;
}
.feather.asc {
transform: rotate(-180deg);
}
</style>
代码可以在这里访问:
https://codesandbox.io/s/pedantic-kirch-bx9zv
解决方案
将dir
属性添加到sorted-table
,并使其值等于desc
<template>
<div id="app">
<sorted-table :values="values" @sort-table="onSort" dir="desc">
<thead>
<tr>
<th scope="col" style="text-align: left; width: 10rem;">
<sort-link name="id">ID</sort-link>
</th>
</tr>
</thead>
<template #body="sort">
<tbody>
<tr v-for="value in sort.values" :key="value.id">
<td>{{ value.id }}</td>
</tr>
</tbody>
</template>
</sorted-table>
</div>
</template>
<script>
import { ChevronUpIcon } from "vue-feather-icons";
export default {
name: "App",
data: function() {
return {
values: [{ id: 2 }, { id: 1 }, { id: 3 }],
sortBy: "",
sortDir: ""
};
},
components: {
ChevronUpIcon
},
methods: {
onSort(sortBy, sortDir) {
this.sortBy = sortBy;
this.sortDir = sortDir;
}
}
};
</script>
<style>
.feather {
transition: 0.3s transform ease-in-out;
}
.feather.asc {
transform: rotate(-180deg);
}
</style>
检查https://github.com/BernhardtD/vue-sorted-table并注意dir
表的属性,你会找到答案
推荐阅读
- aem - 如何限制 aem 中的资产查找器下拉列表?
- php - 为什么 array_unique 不检测重复对象?
- sql - 仅获取具有特定条件的第一个字符
- applet - 如果我通过 VPN 连接到我的局域网,HomeKit 将无法工作
- android - AnimationDrawable 不能应用于 android.graphics.drawable.ColorDrawable[]
- reactjs - React Native:使用堆栈导航器将数据从屏幕传递到组件到另一个屏幕
- python - 从 Docker 中的 Jupyter Notebook 访问本地目录
- swagger - 嵌套复杂类型中的SpringDoc swagger文档生成异常
- c# - 将列表的值分配给另一个列表
- reactjs - 替换变量 onclick 反应