英文:
Cannot remove pagination and add sorting indicators in v-data-table in vue
问题
我正在将一个旧的vue2项目迁移到vue3,我的项目是在vite上设置的,目前我正在使用vuetify labs中的v-data-table。我无法移除分页,数据表头上的默认排序指示器也不可见。就像在这里悬停时看到的那样。
这是我的vdatatable组件。
<v-card-title>Asks</v-card-title>
<div class="table-container">
<v-data-table
dense
:sort-by="sortBy"
:headers="asksHeaders"
:items="marketdata.asks"
:rows-per-page="-1"
options="disablePagination"
class="elevation-1"
>
<!-- 其他模板部分未翻译 -->
</v-data-table>
</div>
这是我的版本信息:
{
"name": "mm2v3",
"version": "0.0.0",
"private": true,
"scripts": {
"serve": "vite preview",
"build": "vite build",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
"dev": "vite",
"host": "vite --host",
"format": "prettier --write src/",
"preview": "vite preview"
},
"dependencies": {
"@mdi/font": "5.9.55",
"axios": "^1.3.4",
"pinia": "^2.0.32",
"qrcode.vue": "^3.3.4",
"roboto-fontface": "*",
"vue": "^3.2.47",
"vue-router": "^4.1.6",
"vuetify": "npm:@vuetify/nightly@next",
"webfontloader": "^1.0.0"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@vitejs/plugin-vue": "^4.0.0",
"@vitejs/plugin-vue-jsx": "^3.0.0",
"@vue/eslint-config-prettier": "^7.1.0",
"eslint": "^8.34.0",
"eslint-plugin-vue": "^9.9.0",
"prettier": "^2.8.4",
"vite": "^4.1.4",
"vite-plugin-vuetify": "^1.0.0-alpha.12",
"vue-cli-plugin-vuetify": "~2.5.8"
}
}
英文:
I am porting an old vue2 project to vue3 my project is setup on vite and I am using v-data-table from vuetify labs at the moment. I am unable to remove pagination and the default sorting indicators on the data table headers are not visible. Like the one's that appear here on hover
This is my vdatatable component.
<v-card-title>Asks</v-card-title>
<div class="table-container">
<v-data-table
dense
:sort-by="sortBy"
:headers="asksHeaders"
:items="marketdata.asks"
:rows-per-page="-1"
options="disablePagination"
class="elevation-1"
>
<template v-slot:column.price="{ header }">
<!-- {{ header.text.toUpperCase() }} -->
Price ({{wallets.rel.ticker }})
</template>
<template v-slot:column.maxvolume="{ header }">
<!-- {{ header.text.toUpperCase() }} -->
Amount ({{wallets.base.ticker }})
</template>
<template v-slot:column.relamount="{ header }">
<!-- {{ header.text.toUpperCase() }} -->
Total ({{wallets.rel.ticker }})
</template>
<!-- Rounding from https://www.jacklmoore.com/notes/rounding-in-javascript/ -->
<!-- Better to move to computed function for maintainability/non-repetitive -->
<template v-slot:item.price="{ item }">
{{ roundedPrice(item.columns.price) }}
</template>
<template v-slot:item.price2="{ item }">
{{ roundedPrice(item.columns.price) }}
<!-- Remaining code -->
<!--
better implementation handled in parent component on load of orders, then promise to set flag
<v-chip v-if="hasMyOrder(item.price)" color="purple" dark>me</v-chip>
-->
<v-chip v-if="item.myOrder" x-small color="purple" dark>*</v-chip>
</template>
<template v-slot:item.maxvolume="{ item }">
{{ roundedPrice(item.columns.maxvolume) }}
</template>
<template v-slot:item.relamount="{ item }">
{{ roundedPrice(item.columns.price * item.columns.maxvolume) }}
</template>
<template v-slot:bottom></template>
</v-data-table>
</div>
<script>
import { VDataTable } from 'vuetify/labs/VDataTable'
export default {
name: "MarketData",
props: ['wallets', 'marketdata', 'myOrdersThisMarket'],
components: {
VDataTable
},
These are my versions:
{
"name": "mm2v3",
"version": "0.0.0",
"private": true,
"scripts": {
"serve": "vite preview",
"build": "vite build",
"lint": "eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore",
"dev": "vite",
"host": "vite --host",
"format": "prettier --write src/",
"preview": "vite preview"
},
"dependencies": {
"@mdi/font": "5.9.55",
"axios": "^1.3.4",
"pinia": "^2.0.32",
"qrcode.vue": "^3.3.4",
"roboto-fontface": "*",
"vue": "^3.2.47",
"vue-router": "^4.1.6",
"vuetify": "npm:@vuetify/nightly@next",
"webfontloader": "^1.0.0"
},
"devDependencies": {
"@rushstack/eslint-patch": "^1.2.0",
"@vitejs/plugin-vue": "^4.0.0",
"@vitejs/plugin-vue-jsx": "^3.0.0",
"@vue/eslint-config-prettier": "^7.1.0",
"eslint": "^8.34.0",
"eslint-plugin-vue": "^9.9.0",
"prettier": "^2.8.4",
"vite": "^4.1.4",
"vite-plugin-vuetify": "^1.0.0-alpha.12",
"vue-cli-plugin-vuetify": "~2.5.8"
}
}
答案1
得分: 1
移除分页
如在Vuetify 3文档中的“外部分页”示例中所示,您可以通过覆盖"bottom"
插槽的内容来更改数据表的分页。通过覆盖插槽但不提供任何内容,您可以有效地移除分页。
保留默认排序机制
根据您的示例代码,您已经在每个列的标题插槽上进行了覆盖,这就是为什么默认的排序控件没有出现的原因[文档]。如果您仍然希望保留默认的排序控件,您需要按照文档中所述重新实现排序功能。文档中也有一个示例,展示了如何做到这一点,尽管它只重新实现了“点击”部分的功能,而不是“悬停”部分。
将所有内容整合在一起
您可以在这里看到一个示例,它使用了<v-hover>
组件和一些内联样式来实现这一点:[Vuetify演示链接]。在示例中,第一、第二和第三个标题已被覆盖,第四和第五个标题保留了默认值。
这种方法可能会受益于一个自定义组件,以抽象掉排序图标的逻辑,否则模板会很快变得冗长。
英文:
Removing the pagination
As shown in the "External Pagination" example in the Vuetify 3 docs, you can change the pagination of a data table by overriding the content of the "bottom"
slot. By overriding the slot but not providing any content, you can effectively remove the pagination.
Keeping the default sorting mechanism
Based on your example code, you're already overriding the headers slot for each column, which would be why the default sorting controls are not appearing
Putting it all together
You can see an example that implements that using the <v-hover>
component and some inline styling here: [Vuetify playground link]. The first, second, and third headers in the example have been overridden, and the fourth and fifth headers have been left with the default.
This approach would probably benefit from a custom component to abstract away the logic for the sort icon, otherwise that template will get verbose quickly.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论