Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Table sorted date not correct

Expected: I'll get a datetime string from an API returned. This value "2019-08-15T15:58:14.597Z" should be displayed in a table as `DD-MM-YYYY HH:MM. Vuetify comes with a data table component, that can sort the data ascending and descending. I also want to use this functionality for the date, to make it sort ascending and descending.

Short question: The date from the API should be saved and displayed in another "style" in the table, but the sort-functionality uses the real date object.

My current code:

<template>
    <v-content class="mt-12 ml-12">
        <h1 class="font-weight-black display-3">Servers</h1>

        <v-data-table
            class="elevation-1"
            :headers="headers"
            :items="columns"
            :items-per-page="15"
        >
        </v-data-table>

        <ul id="example-1">
            <li v-for="(data, index) in columns.data" :key="index">
                {{ data.attributes }}
            </li>
        </ul>
    </v-content>
</template>

<script>
import instances from '../services/instances';

export default {
  data() {
    return {
      columns: [],
      headers: [
        { text: 'Server Name', value: 'attributes.name' },
        { text: 'Spieler', value: 'attributes.playerCount' },
        { text: 'Avg. FPS', value: 'attributes.details.rust_fps_avg' },
        { text: 'Wiped', value: 'attributes.details.rust_last_wipe' },
      ],
    };
  },
  async created() {
    const { data } = await instances.createInstance();
    this.columns = data.data;
    this.columns.forEach((entry) => {
      const { players, maxPlayers } = entry.attributes;
      entry.attributes.playerCount = `${players} / ${maxPlayers}`;
      const { rust_last_wipe } = entry.attributes.details;
      const mmmm = new Date(entry.attributes.details.rust_last_wipe);
      entry.attributes.details.rust_last_wipe = `${mmmm.getDate()}.${mmmm.getMonth() + 1}.${mmmm.getFullYear()}`;
    });
    console.log(this.columns);
  },
};
</script>
like image 521
dewey Avatar asked Aug 22 '19 11:08

dewey


People also ask

Why is my data not sorting correctly?

The most common reason for data not sorting correctly is due to the leading space ahead of the text. Many people using encounter this problem. The text with leading space is sorted at the top in ascending and at the bottom in descending order sort. Try correcting this, and it will work.

How do I fix date sort in Excel?

Here's how to sort unsorted dates: Drag down the column to select the dates you want to sort. Click Home tab > arrow under Sort & Filter, and then click Sort Oldest to Newest, or Sort Newest to Oldest.

Why is Excel not sorting my numbers correctly?

Excel number sort order problems The reason this happens is because Excel has decided that the 'numbers' are actually text and so it is sorting the 'text'. So in much the same way that words sort based on there letters, the numbers sort on the digits instead of the value.

Why is my Excel sheet not sorting by date?

In this case, the default Excel sort feature won't work because it always considers the year, even if your cells are formatted to display only the month or month and day. The solution is to add a helper column, extract the month number and sort by that column. To get a month from date, use the MONTH function. Tip.


1 Answers

In this documentation there is a way to customize the columns content

Based on it we can do:

<template>
  <v-content class="mt-12 ml-12">
    <h1 class="font-weight-black display-3">Servers</h1>

    <v-data-table
      class="elevation-1"
      :headers="headers"
      :items="columns"
      :items-per-page="15"
    >
      <template v-slot:item.attributes.details.rust_last_wipe="{ item }">
        <span>{{ item.attributes.details.rust_last_wipe_formatted }}</span>
      </template>
    </v-data-table>

    <ul id="example-1">
      <li v-for="(data, index) in columns.data" :key="index">
        {{ data.attributes }}
      </li>
    </ul>
  </v-content>
</template>

<script>
import instances from '../services/instances'

export default {
  data() {
    return {
      columns: [],
      headers: [
        { text: 'Server Name', value: 'attributes.name' },
        { text: 'Spieler', value: 'attributes.playerCount' },
        { text: 'Avg. FPS', value: 'attributes.details.rust_fps_avg' },
        { text: 'Wiped', value: 'attributes.details.rust_last_wipe' }
      ]
    }
  },
  async created() {
    const { data } = await instances.createInstance()
    this.columns = data.data
    this.columns.forEach(entry => {
      const { players, maxPlayers } = entry.attributes
      entry.attributes.playerCount = `${players} / ${maxPlayers}`
      const { rust_last_wipe } = entry.attributes.details
      const mmmm = new Date(entry.attributes.details.rust_last_wipe)
      entry.attributes.details.rust_last_wipe_formatted = `${mmmm.getDate()}.${mmmm.getMonth() + 1}.${mmmm.getFullYear()}`
    })
    console.log(this.columns)
  }
}
</script>
like image 84
talkhabi Avatar answered Nov 04 '22 04:11

talkhabi