3
votes

The issue

The problem is already described in the title: When i use a server side paginated datatable (this component: https://vuetifyjs.com/en/components/data-tables) and want it to sort client side (e.g. i use a public API i am not developing) the default sorting does not work on the component.

Is it even possible? The docs give only an example on paginating AND sorting server side.

My component:

<v-data-table
          :headers="datapointHeaders"
          :items="datapoints"
          :items-per-page="datapointsPerPage"
          :server-items-length="datapointsTotalItems"
          :loading="loading"
          :loading-text="'Loading datapoints ...'"
          :options.sync="options"
          :footer-props="footerProps"
        >
          <template v-slot:item.health="{ item }">
            <StatusLamp
              :color="item.health"
              :classes="'ml-2'"
            />
          </template>
          <template v-slot:item.attributes="{ item }">
            <v-btn
              outlined
              small
              color="blue"
              @click="showAttributes(item)"
              class="mb-1 mr-1 mt-1"
            >details</v-btn>
          </template>
        </v-data-table>

The JS part:

private options: any = {
  itemsPerPage: this.datapointsPerPage,
  page: this.datapointsCurrentPage,
};

@Watch('options.page')
onPageChange(value: number, oldValue: number): void {
  this.fetchDatapoints(value, this.options.itemsPerPage);
}

@Watch('options.itemsPerPage')
onPerPageChange(value: number, oldValue: number): void {
  this.fetchDatapoints(this.page, value);
}

get loading(): boolean {
  return get(this.$store.getters.getDatapoints, ['loading'], true);
}

get attributes(): any {
  const res: any = [];
  if (this.selectedDatapoint == null) return [];

  Object.keys(get(this.selectedDatapoint, ['attributes'], {})).forEach((key: string) => {
    if (Object.prototype.hasOwnProperty.call(get(this.selectedDatapoint, ['attributes'], {}), key)) {
      res.push({ name: key, value: this.selectedDatapoint.attributes[key] });
    }
  });

  return res;
}

get datapoints(): any {
  return this.$store.getters.getDatapoints.datapoints
    ? get(this.$store.getters.getDatapoints.datapoints, 'items', []).map((item: any) => ({
      ...item,
      last_value: Math.floor(get(item, ['data', '0', '1'], 0) * 100) / 100,
    }))
    : [];
}

get datapointsCurrentPage(): number {
  return get(this.$store.getters.getDatapoints, ['datapoints', 'meta', 'current_page'], 1);
}

get datapointsPerPage(): number {
  return get(this.$store.getters.getDatapoints, ['datapoints', 'meta', 'per_page'], 10);
}

get datapointsTotalPages(): number {
  return get(this.$store.getters.getDatapoints, ['datapoints', 'meta', 'total_pages'], 0);
}

get datapointsTotalItems(): number {
  return get(this.$store.getters.getDatapoints, ['datapoints', 'meta', 'total_items'], 0);
}

EDIT

I just noticed: As soon as i remove the server-items-length property from the v-data-table component, the sorting works client-side. But then , obviously, the total lenght of the items gets lost and it only shows the current per_page as the maximum for the items length.

1

1 Answers

2
votes

After a little bit more of investigation

Regarding the #vuetify-help channel on the official discord of vuetify, which i asked for exactly this problem, cilent-side pagination and sorting are disabled as soon as one uses server-items-length. Same rule applies to search.

Also, after a little bit of investigation, i found a related issue with search + server side pagination: https://github.com/vuetifyjs/vuetify/issues/3475#issuecomment-370566804

Conclusion

I have to admit, that a client-side sorting & filtering does not make any sense on server-side paginated data, since it would only filter & search through the current page.