sorting - 如何使用vuetify的自定义排序?

标签 sorting vue.js datatable vuetify.js

我想使用 custom-sort在我的数据表中。我的目标是对表格进行 DESC 排序,而不是默认的 ASC。但我不知道怎么做。

这是我的数据表组件的开始:

  <v-data-table
  :headers="headers"
  :items="acts"
  hide-actions
  class="elevation-1"
  >
  <template slot="items" slot-scope="props">

    <td>{{ props.item.id }}</td>
    <td>{{ props.item.name }}</td>
    <td class="text-xs-center">{{ props.item.provider.id }}</td>
    <td class="text-xs-center">{{ props.item.category.name }}</td>
    <td class="text-xs-center">{{ props.item.budget }}</td>
    <td class="text-xs-center">{{ props.item.location.name }}</td>
    <td class="text-xs-center">{{ props.item.deets }}</td>
    <td class="text-xs-center">{{ props.item.keeping_it_100 }}</td>
    <td class="text-xs-center"><img width="50" height="50" :src="props.item.inspiration.inspiration"></td>
    <td class="justify-center layout px-0">....

这是我正在使用的脚本:

<script>
  export default {
    data () {
      return {

        dialog: false,
        customerSort: {
          isDescending: true,// I tried this? as the kabab format throws an error
        },
        headers: [
            { text: 'ID', value: 'id'},
            { text: 'Name', value: 'name' },  
            { text: 'Provider', value: 'provider' },
            { text: 'Category', value: 'category' },
            { text: 'Budget', value: 'budget' },
            { text: 'Country', value: 'location', sortable: true },
            { text: 'Keeping it 100%', value: 'keeping_it_100', sortable: false },
            { text: 'deets', value: 'deets', sortable: false },
            { text: 'inspiration', value: 'inspiration', sortable: false },
            { text: 'Cover', value: 'cover', sortable: false },
            { text: 'Actions', value: 'actions', sortable: false }
        ],

根据文档,它是一个函数属性。但是我还没有找到关于如何通过它的示例。

This is a screenshot of the function...

最佳答案

你可以使用这样的函数-

customSort(items, index, isDesc) {
  items.sort((a, b) => {
    if (index === "date") {
      if (!isDesc) {
        return compare(a.date, b.date);
      } else {
        return compare(b.date, a.date);
      }
    }
  });
  return items;
}

compare 是比较 a.date 和 b.date 的函数,返回 1-1

isDesc 是一个由表传递的变量,它告诉用户想要以什么顺序对其进行排序。如果你想按 desc 排序,只需在 if-else 条件中使用 !isDesc

要在您的模板中使用它,只需使用

<v-data-table
  :headers="headers"
  :items="Data"
  :custom-sort="customSort"
>
  <template slot="items" slot-scope="props">
    <td class="font-weight-black">{{ props.item.date }}</td>
    <td class="text-xs-right">{{ props.item.time }}</td>
    <td class="text-xs-right">{{ props.item.name }}</td>
  </template>
</v-data-table>

要确保您的其他字段仍然可以使用正常的排序方式使用

customSort(items, index, isDesc) {
      items.sort((a, b) => {
        if (index === "date") {
          if (!isDesc) {
            return dateHelp.compare(a.date, b.date);
          } else {
            return dateHelp.compare(b.date, a.date);
          }
        } else {
          if (!isDesc) {
            return a[index] < b[index] ? -1 : 1;
          } else {
            return b[index] < a[index] ? -1 : 1;
          }
        }
      });
      return items;
    }

关于sorting - 如何使用vuetify的自定义排序?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52678905/

相关文章:

vue.js - Vuetify数据表结合了动态插槽和行/项目插槽

c# - 为什么 DataTable.Rows.ImportRow 在传递新创建的 DataRow 时不起作用?

C中的计数排序只能对前4个数字进行排序

javascript - 单击标题时,排序 ngTable 不起作用

javascript - 如何使 v-btn 在点击时不启动路由但仍根据路由显示事件状态?

javascript - 如何从 vue 对话框/模式中获取用户输入

javascript - Angular 与数据表注入(inject)器错误

javascript - 根据 AngularJS 中的两个变量进行升序排序/排序

javascript - 为什么更改模型不会同时更改 EmberJS 中的 View ?

vue.js - Vue 循环 Blade Laravel 内的多个 if 条件