1

I have a v-data-table that groups items. When using a custom header with v-slot like below - how do I enable the built in sort functionality that's on the default header?

is there a way I can trigger the built in sort function with my sortThisColumn function, or any other way? At the moment, my table headings are unclickable.

<v-app class="v-app-custom" v-if="myItems.length > 0">
      <div class="section">
        <v-data-table
          :headers="headers"
          :items="myItems"
          :items-per-page="30"
          hide-default-footer
          hide-default-header
          item-key="uuid"
          group-by="mentor_id"
          class="mentor-table"
        >
          <template v-slot:header="{ props }">
            <thead class="v-data-table-header">
              <tr>
                <th
                  class="text-start"
                  v-for="header in props.headers"
                  :key="header.value"
                  :style="{ width: `${header.width}px` }"
                  @click="sortThisColumn(header.value)"
                >
                  <span>{{ header.text }}</span>
                  <v-icon v-if="header.sortable" color="white" small>{{
                    sort[header.value] == "desc"
                      ? "fa-chevron-down"
                      : "fa-chevron-up"
                  }}</v-icon>
                </th>
              </tr>
            </thead>
          </template>
          <template
            v-slot:group.header="{ group, props, headers }"
            sort-icon="fa-chevron-down"
          >

Table headers:-

headers: [
  {
    text: "Status",
    value: "status",
    width: 82,
    sortable: false,
  },
  { text: "Type", value: "type", width: 140, sortable: true },
  { text: "Creator", value: "creator", width: 140, sortable: true },
  { text: "Date", value: "due_date", width: 141, sortable: true },
  { text: "Mentor", value: "mentor", width: 141, sortable: true },
],
4

1 回答 1

0

检查我制作的这个代码框:https ://codesandbox.io/s/stack-70975751-p9msn?file=/src/components/CustomSorting.vue

您可以像这样在 headers 数组中定义自定义排序函数。我过去这样做是为了向我的日期列之一添加自定义排序。

headers: [
   {
      text: 'Date',
      value: 'date',
      align: 'center',
      sort: (a, b) => {
         var date1 = a.replace(/(\d+)\/(\d+)\/(\d+)/, '$3/$2/$1')
         var date2 = b.replace(/(\d+)\/(\d+)\/(\d+)/, '$3/$2/$1')

         return date1 < date2 ? -1 : 1
      }
   }
]

知道了这一点,你可以在你的情况下做这样的事情。

data: (vm) => ({
   headers: [
      {
         text: 'Date',
         value: 'date',
         sort: (a,b) => {
            return vm.myCustomSort(a,b)
         }
      },
   ]
})
于 2022-02-03T20:56:12.113 回答