简体   繁体   English

使用 vuejs 在 js 数据上动态应用搜索过滤器

[英]Dynamically apply search filter on js data using vuejs

I have a array list which contains column names.我有一个包含列名的数组列表。 I want to write a search filter which will dynamically pick the value from the array and display all the rows and column.我想编写一个搜索过滤器,它将从数组中动态选择值并显示所有行和列。 Search Filter should be with array contains column names.搜索过滤器应该与数组包含列名。

Column name can be set dynamically列名可以动态设置

I have written code like below Inside script tag我已经编写了如下所示的代码 Inside script 标签

Its not working.. udata value is comming as array它不起作用.. udata 值以数组形式出现

export default {
    el: '#apicall',
    mixins: [Vue2Filters.mixin],
    data()
    {
    return {
    toggle: false,
    userData: [],
    search: "",
    apidata: ['Id', 'Name', 'Version'],
    currentvalue: '',
    }
    computed:
    {
    filteruserData: function(){
    var self = this;
    var list =[];

    return this.userData.filter((udata) => {

    for(var i in self.apidata)
    {
    list.push(udata[self.apidata[i]])
    }
    return list.toLowerCase().indexOf(self.search.toLowerCase()) != -1
    });
    },

I didn't really get exactly what you need.我没有真正得到你需要的东西。 From what I understood you need to search for column name in apidata array using a filter.据我了解,您需要使用过滤器在apidata数组中搜索列名。 If your search filter find an element of your apidata array, you want to diplay the row values corresponding to column name your search filter found.如果您的搜索过滤器找到apidata数组的元素,您希望显示与您的搜索过滤器找到的列名称相对应的行值。 From elements you gave in your post, I don't understand what value you need to diplay and where are they supposed to come from.从你在帖子中给出的元素来看,我不明白你需要展示什么价值以及它们应该来自哪里。

Anyway, here is my try to help you.无论如何,这是我尝试帮助您的方法。 Lets suppose that you have a data object that contains the row value to display.假设您有一个包含要显示的行值的data对象。 The code below search inside the apidata array and diplay the value of the corrsponding attribute of the data object if anything has been found.下面的代码在apidata数组中搜索,如果找到任何内容,则显示data对象的对应属性的值。

App.vue应用程序

<template>
  <div id="app"><searchCompoenent :data="data"></searchCompoenent></div>
</template>

<script>
import searchCompoenent from "./components/searchComponent.vue";

export default {
  name: "App",
  data() {
    return {
      data: {
        Id: [1, 9, 2, 3, 4, 5],
        Name: ["name1", "name2"],
        Version: ["2.0", "1.0.0", "1.1-beta"]
      }
    };
  },
  components: {
    searchCompoenent
  }
};
</script>

searchComponent.vue搜索组件.vue

<template>
  <div>
    <input v-model="search" placeholder="Search..." type="text" />
    <button type="button" @click="searchColumn">Search!</button>
  </div>
</template>

<script>
export default {
  data() {
    return {
      search: "",
      apidata: ["Id", "Name", "Version"]
    };
  },
  props: ["data"],
  methods: {
    searchColumn() {
      let result = this.apidata.find(element => {
        return element === this.search;
      });
      if (result !== undefined) {
        console.log(result);
        console.log(this.data);
      }
    }
  }
};
</script>

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

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