无法在Vue的v-data-table中去除分页并添加排序指示器。

huangapple go评论60阅读模式
英文:

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.

 &lt;v-card-title&gt;Asks&lt;/v-card-title&gt;
            &lt;div class=&quot;table-container&quot;&gt;
            &lt;v-data-table
              dense
              :sort-by=&quot;sortBy&quot;
              :headers=&quot;asksHeaders&quot;
              :items=&quot;marketdata.asks&quot;
              :rows-per-page=&quot;-1&quot;
              options=&quot;disablePagination&quot;
              class=&quot;elevation-1&quot;
            &gt;
              &lt;template v-slot:column.price=&quot;{ header }&quot;&gt;
                &lt;!-- {{ header.text.toUpperCase() }} --&gt;
                Price ({{wallets.rel.ticker }})
              &lt;/template&gt;

              &lt;template v-slot:column.maxvolume=&quot;{ header }&quot;&gt;
                &lt;!-- {{ header.text.toUpperCase() }} --&gt;
                Amount ({{wallets.base.ticker }})
              &lt;/template&gt;

              &lt;template v-slot:column.relamount=&quot;{ header }&quot;&gt;
                &lt;!-- {{ header.text.toUpperCase() }} --&gt;
                Total ({{wallets.rel.ticker }})
              &lt;/template&gt;

              &lt;!-- Rounding from https://www.jacklmoore.com/notes/rounding-in-javascript/ --&gt;
              &lt;!-- Better to move to computed function for maintainability/non-repetitive --&gt;
              &lt;template v-slot:item.price=&quot;{ item }&quot;&gt;
                {{ roundedPrice(item.columns.price) }}
              &lt;/template&gt;

              &lt;template v-slot:item.price2=&quot;{ item }&quot;&gt;
                {{ roundedPrice(item.columns.price) }}
  &lt;!-- Remaining code --&gt;

&lt;!--
better implementation handled in parent component on load of orders, then promise to set flag
                &lt;v-chip v-if=&quot;hasMyOrder(item.price)&quot; color=&quot;purple&quot; dark&gt;me&lt;/v-chip&gt;
--&gt;
                &lt;v-chip v-if=&quot;item.myOrder&quot; x-small color=&quot;purple&quot; dark&gt;*&lt;/v-chip&gt;
              &lt;/template&gt; 

              &lt;template v-slot:item.maxvolume=&quot;{ item }&quot;&gt;
                {{ roundedPrice(item.columns.maxvolume) }}
              &lt;/template&gt;

              &lt;template v-slot:item.relamount=&quot;{ item }&quot;&gt;
                {{ roundedPrice(item.columns.price * item.columns.maxvolume) }}
              &lt;/template&gt;
              &lt;template v-slot:bottom&gt;&lt;/template&gt;
            &lt;/v-data-table&gt;
            &lt;/div&gt;


&lt;script&gt;
import { VDataTable } from &#39;vuetify/labs/VDataTable&#39;

export default {
  name: &quot;MarketData&quot;,
  props: [&#39;wallets&#39;, &#39;marketdata&#39;, &#39;myOrdersThisMarket&#39;],
  components: {
    VDataTable
  },

These are my versions:

{
  &quot;name&quot;: &quot;mm2v3&quot;,
  &quot;version&quot;: &quot;0.0.0&quot;,
  &quot;private&quot;: true,
  &quot;scripts&quot;: {
    &quot;serve&quot;: &quot;vite preview&quot;,
    &quot;build&quot;: &quot;vite build&quot;,
    &quot;lint&quot;: &quot;eslint . --ext .vue,.js,.jsx,.cjs,.mjs --fix --ignore-path .gitignore&quot;,
    &quot;dev&quot;: &quot;vite&quot;,
    &quot;host&quot;: &quot;vite --host&quot;,
    &quot;format&quot;: &quot;prettier --write src/&quot;,
    &quot;preview&quot;: &quot;vite preview&quot;
  },
  &quot;dependencies&quot;: {
    &quot;@mdi/font&quot;: &quot;5.9.55&quot;,
    &quot;axios&quot;: &quot;^1.3.4&quot;,
    &quot;pinia&quot;: &quot;^2.0.32&quot;,
    &quot;qrcode.vue&quot;: &quot;^3.3.4&quot;,
    &quot;roboto-fontface&quot;: &quot;*&quot;,
    &quot;vue&quot;: &quot;^3.2.47&quot;,
    &quot;vue-router&quot;: &quot;^4.1.6&quot;,
    &quot;vuetify&quot;: &quot;npm:@vuetify/nightly@next&quot;,
    &quot;webfontloader&quot;: &quot;^1.0.0&quot;
  },
  &quot;devDependencies&quot;: {
    &quot;@rushstack/eslint-patch&quot;: &quot;^1.2.0&quot;,
    &quot;@vitejs/plugin-vue&quot;: &quot;^4.0.0&quot;,
    &quot;@vitejs/plugin-vue-jsx&quot;: &quot;^3.0.0&quot;,
    &quot;@vue/eslint-config-prettier&quot;: &quot;^7.1.0&quot;,
    &quot;eslint&quot;: &quot;^8.34.0&quot;,
    &quot;eslint-plugin-vue&quot;: &quot;^9.9.0&quot;,
    &quot;prettier&quot;: &quot;^2.8.4&quot;,
    &quot;vite&quot;: &quot;^4.1.4&quot;,
    &quot;vite-plugin-vuetify&quot;: &quot;^1.0.0-alpha.12&quot;,
    &quot;vue-cli-plugin-vuetify&quot;: &quot;~2.5.8&quot;
  }
}

Complete code here

答案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 &quot;bottom&quot; 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

. If you still want to keep the default sorting controls, you'll need to what the docs state and re-implement that sorting functionality. There's an example of how to do this in the docs as well, although it on re-implements the "click" part of the functionality, not the "hover" part.

Putting it all together

You can see an example that implements that using the &lt;v-hover&gt; 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.

huangapple
  • 本文由 发表于 2023年6月26日 07:14:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76552755.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定