简体   繁体   English

获取 Vuetify 数据表中过滤项目的长度

[英]Get length of filtered items in Vuetify data-table

Is there any way to get the length of filtered items in the Vuetify data-table?有什么方法可以获取 Vuetify 数据表中过滤项目的长度? When the rows are filtered the length of shown items obviously decreases and I need to know how many items there after the filter, because I need to update my external pagination component.当行被过滤时,显示项目的长度明显减少,我需要知道过滤后有多少项目,因为我需要更新我的外部分页组件。

Assuming you are using vuetify 2.2.x you can use the pagination event of v-data-table.假设您使用的是 vuetify 2.2.x,您可以使用 v-data-table 的分页事件。

<v-data-table
 @pagination="yourMethod"
...

to call your method调用你的方法

methods: {
  yourMethod(pagination) {
    console.log(pagination.itemsLength) // length of filtered/searched items in Vuetify data-table
  },

the pagination parameter passed by the pagination event to yourMethod contains the following informations:分页事件传递给 yourMethod 的分页参数包含以下信息:

{
  page: number
  itemsPerPage: number
  pageStart: number
  pageStop: number
  pageCount: number
  itemsLength: number
}

I assume you're using search property in order to filter out your data.我假设您正在使用search属性来过滤掉您的数据。 If so you need to add reference to your table ref="myTable" .如果是这样,您需要添加对表ref="myTable"的引用。 Then you can grab array of filtered items like this: this.$refs.myTable.selectableItems .然后你可以像这样抓取过滤项目的数组: this.$refs.myTable.selectableItems

If it's some other filter method approach is the same - using refs.如果是其他一些过滤方法方法是相同的 - 使用 refs.

I did it by putting a watch on my search field and the data that is being shown.我通过在我的搜索字段和正在显示的数据上放置手表来做到这一点。 I also had to add a timeout, because otherwise, it will display the current amount of records showing before the search takes place.我还必须添加一个超时,否则,它将显示在搜索发生之前显示的当前记录数量。

@Watch('search')
@Watch('table.data')
onSearchChanged() {
  setTimeout(() => {
    const table = (this.$refs.dynamoTable as any);
    this.filteredItemCount = table?.itemsLength || 0;
  }, 1200);
}

I did this, so that I could show search hint.我这样做是为了显示搜索提示。

get searchHint(): string {
  const count = this.filteredItemCount;
  const total = (this.table.data?.length)
    ? this.table.data.length
    : 0;

  return this.$t('search_records', { count, total });
}

It then shows up correctly on my search text field as a search hint.然后它作为搜索提示正确显示在我的搜索文本字段中。

<v-text-field class="ml-2"
  v-model="search"
  prepend-inner-icon="filter_list"
  :disabled="!table.data || !table.data.length"
  :label="$t('filter')"
  clearable
  :hint="searchHint"
  :persistent-hint="true"
/>

This is on version 1.5.24 of Vuetify.这是 Vuetify 的 1.5.24 版本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM