自定义排序功能在Vuetify3中不起作用。

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

Custom sort function not working in vuetify3

问题

我有一个具有设置的标题的 `v-data-table`,但是我无法使自定义排序函数起作用。

```javascript
const headers = ref([
  { key: "PersonID", title: "EDB Person ID", sortable:true }, 
  { key: "FirstName", title: "Name", sortable:true },
  { key: "PersonPersonTimekeeperMaps", title: "Timekeeper ID", sortable:false },
  { key: "Created", title: "Created", sortable:true, sort:(d1: string | null | undefined, d2: string | null | undefined) => {
        const date1 = new Date(d1 as string);
        const date2 = new Date(d2 as string);
        if (date1 > date2) return -1;
        if (date1 < date2) return 1;
        return 0;
    } }
]);

在开发工具中,它从未触发函数中的断点。并且它没有遵循我设置的逻辑。似乎它只是自行执行默认排序。


<details>
<summary>英文:</summary>

I have a `v-data-table` with headers set, but I can&#39;t get the custom sort function to work.

    &lt;v-data-table v-else :items-per-page=&quot;itemsPerPage&quot; density=&quot;compact&quot; :headers=&quot;headers&quot; :items=&quot;rows&quot; :no-data-text=&quot;t(&#39;label_no_data&#39;)&quot; sticky class=&quot;elevation-1 text-caption&quot; :class=&quot;{&#39;mobile-table&#39;:IsMobile}&quot; /&gt;


const headers = ref([
{ key: "PersonID", title: "EDB Person ID", sortable:true },
{ key: "FirstName", title: "Name", sortable:true },
{ key: "PersonPersonTimekeeperMaps", title: "Timekeeper ID", sortable:false },
{ key: "Created", title: "Created", sortable:true, sort:(d1: string | null | undefined, d2: string | null | undefined) => {
const date1 = new Date(d1 as string);
const date2 = new Date(d2 as string);
if (date1 > date2) return -1;
if (date1 < date2) return 1;
return 0;
} }
]);


It never hits the breakpoint in the function in dev tools. And it is not respecting the logic I set. It seems to just do the default sort on its own.

</details>


# 答案1
**得分**: 1

在 Vuetify 3 中,排序函数已从标题声明移至 [`:custom-key-sort`](https://vuetifyjs.com/en/api/v-data-table/#props-custom-key-sort) 属性,该属性接受将列键映射到排序函数的对象。

因此,在你的情况下,代码如下:

```javascript
const customKeySort = {
  Created: (d1: string | null | undefined, d2: string | null | undefined) =&gt; {
    ...
  }
}

以及

<v-data-table
  :custom-key-sort=&quot;customKeySort&quot;
  ...
/>

这里是一个片段示例:

const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
  setup(){
    const headers = ref([{key: &#39;name&#39;, title: &#39;Name&#39;}, {key: &#39;date&#39;, title: &#39;Date&#39;}])
  
    const items = ref(Array.from({length:10}, (_,i) =&gt; ({
      name: `Item ${1 + i}`,
      date: new Date(new Date() - Math.random()*(1e+12)).toString()
    })))

    const customKeySort = {
      date: (d1,d2) =&gt; new Date(d1).getTime() - new Date(d2).getTime()
    }

    return {headers, items, customKeySort,}
  }

}
createApp(app).use(vuetify).mount(&#39;#app&#39;)
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css&quot; />
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify-labs.min.css&quot; />
<link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;>
<div id=&quot;app&quot;>
  <v-app>
    <v-data-table
    :headers=&quot;headers&quot;
    :items=&quot;items&quot;
    item-title=&quot;name&quot;
    item-value=&quot;name&quot;
    :custom-key-sort=&quot;customKeySort&quot;
  ></v-data-table>
  </v-app>
</div>
<script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;></script>
<script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js&quot;></script>
<script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify-labs.js&quot;></script>
英文:

In Vuetify 3, the sort functions have moved from the header declaration to the :custom-key-sort property, which takes an object mapping column key to sort function.

So in your case, that would be:

const customKeySort = {
  Created: (d1: string | null | undefined, d2: string | null | undefined) =&gt; {
    ...
  }
}

and

&lt;v-data-table
  :custom-key-sort=&quot;customKeySort&quot;
  ...
/&gt;

Here it is in a snipet:

<!-- begin snippet: js hide: true console: false babel: false -->

<!-- language: lang-js -->

const { createApp, ref } = Vue;
const { createVuetify } = Vuetify
const vuetify = createVuetify()
const app = {
  setup(){
    const headers = ref([{key: &#39;name&#39;, title: &#39;Name&#39;}, {key: &#39;date&#39;, title: &#39;Date&#39;}])
  
    const items = ref(Array.from({length:10}, (_,i) =&gt; ({
      name: `Item ${1 + i}`,
      date: new Date(new Date() - Math.random()*(1e+12)).toString()
    })))

    const customKeySort = {
      date: (d1,d2) =&gt; new Date(d1).getTime() - new Date(d2).getTime()
    }

    return {headers, items, customKeySort,}
  }

}
createApp(app).use(vuetify).mount(&#39;#app&#39;)

<!-- language: lang-html -->

&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.css&quot; /&gt;
&lt;link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify-labs.min.css&quot; /&gt;
&lt;link href=&quot;https://cdn.jsdelivr.net/npm/@mdi/font@5.x/css/materialdesignicons.min.css&quot; rel=&quot;stylesheet&quot;&gt;
&lt;div id=&quot;app&quot;&gt;
  &lt;v-app&gt;
    &lt;v-data-table
    :headers=&quot;headers&quot;
    :items=&quot;items&quot;
    item-title=&quot;name&quot;
    item-value=&quot;name&quot;
    :custom-key-sort=&quot;customKeySort&quot;
  &gt;&lt;/v-data-table&gt;
  &lt;/v-app&gt;
&lt;/div&gt;
&lt;script src=&quot;https://unpkg.com/vue@3/dist/vue.global.prod.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify.min.js&quot;&gt;&lt;/script&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/vuetify@3/dist/vuetify-labs.js&quot;&gt;&lt;/script&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年7月4日 22:03:32
  • 转载请务必保留本文链接:https://go.coder-hub.com/76613442.html
匿名

发表评论

匿名网友

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

确定