Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I sort a v-data-table by default?

I can't seem to get default sorting to work. All I can see for the argument custom-sort in the documentation is that it's a "Function used to sort items", but I don't know in what way. I can imagine many. Is it called for an initial sort? It seems to return a list of items, but when I try to create one, I get an error saying this.customSort is not a function.

<template>
  <v-data-table
    :headers="headers"
    :items="reports"
    hide-default-footer>
    <template v-slot:item.requested="{ item }">
      {{ datetimeToDistance(item.requested) }}
    </template>
  </v-data-table>
</template>
<script>
export default {
name: 'Reports',
data () {
    return {
        customSort: (items,index,isDesc) => console.log("never called"),
        reports: [{name:"a",requested:"2020-01-01T00:00:00Z"}.{name:"b",requested:"2020-02-02T00:00:00"}],
    }
},
computed: {
    headers () {
        return [
            {text: "Name", value: "name"},
            {text: "Report Type", value: "report_type"},
            {text: "Requested", value: "requested", sort: (a,b) => a.localeCompare(b) },
            ];
    },
}
}
</script>

My sorting works if you click on the links. All I really want here is to say: "When the page first loads, sort by 'requested' as though the user clicked that one initially. Then let them change the ordering."

Note: The datetimeToDistance is just a function which calls a library and isn't too important. It's just that the output of that column is not directly in the objects.

like image 387
mmachenry Avatar asked Mar 06 '20 22:03

mmachenry


People also ask

Does Vuetify work with Vue 3?

The current version of Vuetify does not support Vue 3.

How do I sort data in DataTables?

Default ordering (sorting) With DataTables you can alter the ordering characteristics of the table at initialisation time. Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want. The order parameter is an array of arrays where the first value of the inner array is the column ...

How do I set the Order of data in a table?

Using the order initialisation parameter, you can set the table to display the data in exactly the order that you want. The order parameter is an array of arrays where the first value of the inner array is the column to order on, and the second is 'asc' (ascending ordering) or 'desc' (descending ordering) as required.

What is default order in SQL Server?

Default ordering (sorting) The order parameter is an array of arrays where the first value of the inner array is the column to order on, and the second is 'asc' (ascending ordering) or 'desc' (descending ordering) as required. order is a 2D array to allow multi-column ordering to be defined.

How do I sort by column in a tasklist?

SortByColumns( Filter(workstackdata, searchproject.text in TitleWorkStatus.value="Active"), "ProjectStartDateColumn", Descending) then to the next one; SortByColumns( Filter(tasklistdata, status.value ="Incomplete"), "CompletionDateColumn", Descending)


2 Answers

I usually sort v-datatable in the pagination directive as below:

<template>
    <v-data-table :headers="headers" :items="reports" :pagination.sync="pagination" hide-default-footer>
        <template v-slot:item.requested="{ item }">
            {{ datetimeToDistance(item.requested) }}
        </template>
    </v-data-table>
</template>
<script>
    export default {
        name: 'Reports',
        data() {
            return {
                customSort: (items, index, isDesc) => console.log("never called"),
                reports: [{ name: "a", requested: "2020-01-01T00:00:00Z" }.{ name: "b", requested: "2020-02-02T00:00:00" }],
                pagination: { sortBy: 'requested', descending: true, rowsPerPage: 10 }
            }
        },
        computed: {
            headers() {
                return [
                    { text: "Name", value: "name" },
                    { text: "Report Type", value: "report_type" },
                    { text: "Requested", value: "requested", sort: (a, b) => a.localeCompare(b) },
                ];
            },
        }
    }
</script>
like image 29
Samidjo Avatar answered Nov 10 '22 10:11

Samidjo


Use the sort-by and the sort-desc properties with the .sync option, and set the desired values in data.

<template>
  <div>
    <v-data-table
      :sort-by.sync="sortBy"
      :sort-desc.sync="sortDesc"
    ></v-data-table>
  </div>
</template>

<script>
  export default {
    data () {
      return {
        sortBy: 'fat',
        sortDesc: false,
    },
  }
</script>

https://vuetifyjs.com/en/components/data-tables/#external-sorting

like image 174
waspinator Avatar answered Nov 10 '22 10:11

waspinator