如何使用“自定义过滤器"?vuetify中数据表中的道具?或如何创建自定义过滤器以按标题过滤?

2023-09-05前端开发问题
8

本文介绍了如何使用“自定义过滤器"?vuetify中数据表中的道具?或如何创建自定义过滤器以按标题过滤?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

截至发稿之日,我找不到在数据表中使用自定义过滤器"道具的任何文档.

我只想创建一个自定义过滤器来按标题过滤我的数据表.我有一个下拉列表,当用户单击下拉列表的选项之一时,它会过滤列表中的一个特定标题.

示例:下拉选项:食物类型:水果、肉类、蔬菜

  1. Bakchoi(蔬菜)
  2. 猪肉(肉)
  3. 鸡大腿(肉)
  4. 西瓜(水果)

如果我选择下拉菜单作为肉,它应该只显示猪肉和鸡腿.

解决方案

看Github上的代码1,貌似是用customFilter prop覆盖用于确定 filter 属性如何应用于表中的项目的默认方法.

默认的 customFilter 方法将 filter 函数应用于每个项目对象的每个属性名称,并过滤掉任何不包含通过过滤器的属性名称的项目:

<块引用>

customFilter: {类型:函数,默认值:(项目、搜索、过滤器)=>{搜索 = search.toString().toLowerCase()return items.filter(i => (Object.keys(i).some(j => filter(i[j], search))))}},

如果您想阻止任何列包含在过滤器中,或者如果您一直希望阻止过滤掉特定行,则可能需要覆盖此函数.

您会注意到该方法还依赖于 search 属性,它必须是一个字符串.

<小时>

说了这么多,你真的不需要使用那个道具来做你想做的事.您应该只创建一个计算属性来根据您的下拉值过滤项目,并将该计算属性作为 items 属性传递.

这是一个例子:

new Vue({埃尔:'#app',数据() {返回 {食物: [{名称:'Bakchoi',类型:'蔬菜',卡路里:100},{名称:'猪肉',类型:'肉',卡路里:200},{名称:'鸡大腿',类型:'肉',卡路里:300},{名称:'西瓜',类型:'水果',卡路里:10},],标题:[{ 文本:名称",对齐:左",值:名称"},{ text: '食物类型', align: 'left', value: 'type' },{ 文本:卡路里",对齐:左",值:卡路里"},],食物类型:空,};},计算:{过滤项目(){返回 this.food.filter((i) => {返回 !this.foodType ||(i.type === this.foodType);})}}})

<script src="https://unpkg.com/vue@2.4.2/dist/vue.js"></脚本><script src="https://unpkg.com/vuetify@0.15.2/dist/vuetify.js"></script><link rel="stylesheet" href="https://unpkg.com/vuetify@0.15.2/dist/vuetify.min.css"><link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|材料+图标"><div id="app"><v-app>

<小时>

  1. 这个答案是在 Vuetify 在 v0.15.2 时编写的.VDataTable的源代码可以在此处找到该版本的组件.

As of date of posting, I cannot find any documentation to use the "custom filter" prop in data tables.

I just want to create a custom filter to filter my data table by headers. I have a dropdown, and when user click on one of the options for the dropdown, it will filter the list for one specific header.

Example: Dropdown options: Food type: fruit, meat, vegetable

  1. Bakchoi (vegetable)
  2. Pork (meat)
  3. Chicken Thigh (meat)
  4. watermelon (fruit)

If I select dropdown as meat, it should only show me pork and chicken thigh.

解决方案

Looking at the code on Github1, it looks like the customFilter prop is used to overwrite the default method used to determine how the filter prop is applied to the items in the table.

The default customFilter method applies the filter function to each property name of each item object and filters out any items that don't include one property name that passes the filter:

customFilter: {
  type: Function,
  default: (items, search, filter) => {
    search = search.toString().toLowerCase()
    return items.filter(i => (
      Object.keys(i).some(j => filter(i[j], search))
    ))
  }
},

You might want to overwrite this function if you wanted to prevent any columns from being included in the filter or if there were specific rows that you always wanted to prevent from being filtered out.

You'll notice that the method also depends on the search prop, which must be a string.


All that said, you really don't need to use that prop for what you want to do. You should just make a computed property to filter the items based on your dropdown value and pass that computed property as the items prop.

Here's an example:

new Vue({
  el: '#app',
  data() {
    return {
      food: [
        { name: 'Bakchoi', type: 'vegetable', calories: 100 },
        { name: 'Pork', type: 'meat', calories: 200 },
        { name: 'Chicken Thigh', type: 'meat', calories: 300 },
        { name: 'Watermelon', type: 'fruit', calories: 10 },
      ],
      headers: [
        { text: 'Name', align: 'left', value: 'name' },
        { text: 'Food Type', align: 'left', value: 'type' }, 
        { text: 'Calories', align: 'left', value: 'calories' },
      ],
      foodType: null,
    };
  },
  computed: {
    filteredItems() {
      return this.food.filter((i) => {
        return !this.foodType || (i.type === this.foodType);
      })
    }
  }
})

<script src="https://unpkg.com/vue@2.4.2/dist/vue.js"></script>
<script src="https://unpkg.com/vuetify@0.15.2/dist/vuetify.js"></script>
<link rel="stylesheet" href="https://unpkg.com/vuetify@0.15.2/dist/vuetify.min.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700|Material+Icons">

<div id="app">
  <v-app>  
    <v-select 
      label="Food Type" 
      :items="['vegetable', 'meat', 'fruit']"
      v-model="foodType"
    ></v-select>
    
    <v-data-table 
      :headers="headers"
      :items="filteredItems"
      hide-actions
    >
      <template slot="items" scope="{ item }">
        <td>{{ item.name }}</td>
        <td>{{ item.type }}</td>
        <td>{{ item.calories }}</td>
      </template>
    </v-data-table>
  </v-app>
</div>


  1. This answer was written when Vuetify was at v0.15.2. The source code for the VDataTable component at that version can be found here.

这篇关于如何使用“自定义过滤器"?vuetify中数据表中的道具?或如何创建自定义过滤器以按标题过滤?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

如何使用百度地图API获取地理位置信息
首先,我们需要在百度地图开放平台上申请一个开发者账号,并创建一个应用。在创建应用的过程中,我们会得到一个密钥(ak),这是调用API的凭证。 接下来,我们需要准备一个PHP文件,以便可以在网页中调用。首先,我们需要引入百度地图API的JS文件,代码如下...
2024-11-22 前端开发问题
244

JavaScript小数运算出现多位的解决办法
在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会...
2024-10-18 前端开发问题
301

正则表达式([A-Za-z])为啥可以匹配字母加数字或特殊符号?
问题描述: 我需要在我的应用程序中验证一个文本字段。它既不能包含数字,也不能包含特殊字符,所以我尝试了这个正则表达式:/[a-zA-Z]/匹配,问题是,当我在字符串的中间或结尾放入一个数字或特殊字符时,这个正则表达式依然可以匹配通过。 解决办法: 你应...
2024-06-06 前端开发问题
165

CoffeeScript 总是以匿名函数返回
CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)...
2024-04-20 前端开发问题
13

getFullYear 在一年的第一天返回前一年
getFullYear returns year before on first day of year(getFullYear 在一年的第一天返回前一年)...
2024-04-20 前端开发问题
6

如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?
How do I make a TextGeometry multiline? How do I put it inside a square so it wraps like html text does inside a div?(如何制作 TextGeometry 多线?如何将它放在一个正方形内,以便它像 html 文本一样包裹在 div 内?) - IT屋-程序员软件开发技术分享社...
2024-04-20 前端开发问题
6