无法将VUE3中的vdatatable值四舍五入到8位小数。

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

Unable to round of vdatatable values to 8 decimal places in VUE3

问题

computed: {
    roundedPrice() {
        return (price) => {
            return Number((Math.round(price * 1e8) / 1e8).toFixed(8));
        };
    }
},
英文:

I am porting an old vue2 project to vue3 the project used Math.round function to round off current market data, i.e bids and asks to 8 decimal places. The function works fine with the old version however in the newer version it does not work and I get very long decimal values. I have tried something like this but it does not work.

<template v-slot:item.price="{ item }">
                {{ roundedPrice(item.price) }}
              </template>

              <template v-slot:item.price2="{ item }">
                {{ roundedPrice(item.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.maxvolume) }}
              </template>

              <template v-slot:item.relamount="{ item }">
                {{ roundedPrice(item.price * item.maxvolume) }}
              </template>

The following is my roundedPrice method defined in my script:

computed: {
    roundedPrice() {
      return (price) => {
        return Number((Math.round(price * 1e8) / 1e8).toFixed(8));
      };
    }
  },

You can checkout the live project here and see the AtomicDexOrderbook component which is the VDATABLE with market orders against the pair. The headings are a bit messed up as well.

http://v3dev.komodefi.com:17077/traderview/RICK/MORTY

This is my complete code:

答案1

得分: 2

v-data-table的插槽语法已经发生了变化。实际上,Vuetify 2和Vuetify 3之间发生了很多变化。每当您在升级过程中遇到问题时,都应仔细查看Vuetify 3文档。假设没有任何内容相同,并始终确认您的旧Vuetify 2代码的语法是否仍然有文档,或者在Vuetify 3中是否发生了更改。

您的价格列的新插槽语法应该如下所示:

<template v-slot:item.price="{ item }">
  {{ roundedPrice(item.columns.price) }}
</template>

所有使用item.columnName的插槽都应替换为item.columns.columnName

计算属性本身没有问题。Math对象是纯JavaScript,因此与Vue或Vuetify无关的计算。

[工作中的Vue Playground示例](https://play.vuetifyjs.com/#eNr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H1FqWQSZpJVAutqHYMxPPDhVx8tSCXqcSdAgz4jush6AJEbMVlgXvCpt4nBu/5YdNgubVvcGDOoO7VdLWFD5d5mWXdP97JM4UfWfgcm6wXVuF+HDzVoVgIUtKAIpYFN6OMGpqkSqH3PIBMJE/LoJVzROma2TwM5NWsEZXB6K9WazNUNklmFolbRGTS1CHzMI6ao361iuPNnFBhkT6kTf5F9ywNoVuUcDQsS/PYJ+0rb/yb36A8mc3UzyB3Abxr4hwd+T9DJvwbwG4zAaiCRKnkUSMxGhV+zHyV+A8iPIrDDg6aDQ7/PAmC/Q78R0I8TzaG4l+vFFEjkhZvOJSYNL7j9p3BM7ONT9q+IL21Sd3elbpulXbp+DkHnweU8xH8p1fnidoOu3rx69kpYEujFBcgCD+frdYAfcezeClMCHREB2xbrFW03vc3qUOiGjZ03MbWjq4Bco6ehqhqCTnfSVSdk7cKhHPz5DWPK/qFrOMt7mfrGYwmTDMvl49km1hrgXtvfBRPfU4B7z9evFJRS4MAHpQX+wo+a+TzIPDKrM8G4YkLFALc3QZRkLv6YfT+ul38S/kBB5xsqsHCXV8R2cRrMbiehO00mZTma0sbkhrX0ycxdh6CQybF0F+RcOM52QmmT47OsAH1h7YjgIrw9zmPX8ZlwXIIk9iubcMmbeZBkJ

英文:

The syntax for v-data-table slots has changed. A lot has changed between Vuetify 2 and Vuetify 3 actually. You will want to thoroughly review the Vuetify 3 documentation any time you have an issue with the upgrade process. Assume nothing is the same and always confirm whether the syntax of your old Vuetify 2 code is still documented, or if it has changed in Vuetify 3.

The new slot syntax for your price column should be like this:

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

All of your slots that use item.columnName should be replaced with item.columns.columnName

The computed property itself is fine. The Math object is vanilla JavaScript, so nothing Vue or Vuetify related could affect it's calculation.

working Vue Playground example

huangapple
  • 本文由 发表于 2023年6月22日 06:44:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76527587.html
匿名

发表评论

匿名网友

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

确定