简体   繁体   English

Vuejs / javascript / 我正在尝试连接一个搜索栏来搜索我的表格并根据我的输入显示结果

[英]Vuejs / javascript / I am trying to connect a search bar that searches my table and displays the results according to my input

I'm trying to loop through each button within the search bar while displaying only the rows that match the input with filteredUsers .我试图遍历搜索栏中的每个按钮,同时仅显示与filteredUsers用户的输入匹配的行。
I am getting the list of users from an AWS S3 bucket and then bringing that data into a table.我从 AWS S3 存储桶中获取用户列表,然后将该数据放入表中。

<div>
  <div id="main">
    Search: <input type="text" @input="filteredUsers" v-model="search" />
    <base-table id="empTable" :data="info" :columns="columns">
      <template slot="columns">
        <th>Full Name</th>
        <th>Work Email</th>
        <th>Title</th>
        <th> Status </th>
        <th class="text-right">Actions</th>
      </template>
      <template
        slot-scope="{row}"
        v-if="filteredUsers &&
        typeof info != 'undefined' &&
        info != null &&
        info.length != null &&
        info.length > 0
        " id="table">
        <td>
          <button  class="buttonChange " @click="(t) =>viewInfo(t)">
            {{row.formattedName }}
          </button>
        </td>
        <td>
          <button  class="buttonChange" @click="(t) =>viewInfo(t)">
            {{row.workEmail}}
          </button>
        </td>
        <td>
          <button  class="buttonChange" @click="(t) =>viewInfo(t)">
            {{row.title}}
          </button>
        </td>
        <td>
          <button  class="buttonChange" @click="(t) =>viewInfo(t)">
            {{row.activeORinactive}}
          </button>
        </td>

My Javascript我的 Javascript

data() {
  return {
    columns: [],
    info: [],
    infoModal: "",
    modal: false,
    modalView: false,
    refreshOnExit: 0,
    error: "",
    name: '',
    search: " ",
  }
},
methods: {
  filteredUsers() {

    return this.info.filter((user) => {
      return info.formattedName.match(this.search);

    })


  },
  async getList() {
    try {
      const list = await axios.get(`MyRequest`);
      this.info = list.data;
      this.info.sort(function(a, b) {
        var nameA = a.formattedName.toUpperCase();
        var nameB = b.formattedName.toUpperCase();
        if (nameA < nameB) {
          return -1;
        }
        if (nameA > nameB) {
          return 1;
        }
        // names must be equal
        return 0;
      });
    } catch (e) {
      console.log(`getList`, e);
      if (e.response.status === 400) {
        this.error = e.response.data.message;
      }
    }
  }
}

I am using the getList function on render我在渲染上使用getList function

created() {
  this.getList();
}

I'd like some help on how I should handle this issue.我想要一些关于我应该如何处理这个问题的帮助。

1st, You have to remove the @input on the textbox, Vue observability will trigger updates automatically. 1、您必须删除文本@input上的@input,Vue 可观察性会自动触发更新。

2nd, filteredUsers() should be a computed property, not a method.第二,filteredUsers() 应该是一个computed属性,而不是一个方法。

HTML changes HTML 变化

Search: <input type="text" v-model="search" />
<base-table id="empTable" :data="filteredUsers" :columns="columns">
...
 <template
    slot-scope="{row}"
    id="table">

Javascript Javascript

data() {
    return {
        columns: [],
        info: [],
        infoModal: "",
        modal: false,
        modalView: false,
        refreshOnExit: 0,
        error: "",
        name: '',
        search: " ",
    };
},
computed: {
    filteredUsers() {
        if (!(this.info && this.info.length)) return []; //return empty if no info[]
        return this.info.filter((user) => {
            return user.match(this.search);
        });
    },
},
methods: {
    async getList() {
        try {
            const list = await axios.get(`MyRequest`);
            this.info = list.sort(function(a, b) {
                var nameA = a.formattedName.toUpperCase();
                var nameB = b.formattedName.toUpperCase();
                if (nameA < nameB) {
                    return -1;
                }
                if (nameA > nameB) {
                    return 1;
                }
                // names must be equal
                return 0;
            });
        } catch (e) {
            console.log(`getList`, e);
            if (e.response.status === 400) {
                this.error = e.response.data.message;
            }
        }
    }
},
created() {
     this.getList();
}

暂无
暂无

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

相关问题 我想 append 一个查询字符串到我的 GitHub 页面 URL 以便它搜索我的搜索栏 - I'd like to append a query string to my GitHub Page URL so that it searches my search bar 我目前正在尝试从显示菜单项的数组中返回一些数据到我的搜索栏中 - I am currently trying to return some data from an array displaying menu items into my search bar 如何使搜索栏仅搜索我的网站页面并提供建议 - How can I make search bar searches only my website pages and give suggetions 如何在HTML表格中仅搜索与搜索栏中输入内容完全匹配的内容? - How can I search an HTML table for only an exact match of my input in the search bar? Axios.all:如何使用同义词库API允许我的搜索栏在其搜索结果中包含输入词的同义词? - Axios.all: How do I use a thesaurus API to allow my search bar to include synonyms of the input word in its search results? 修复我的javascript搜索栏 - Fixing my javascript search bar 我正在尝试在 javascript 中使用 for 循环来创建一个显示 * 的表,并且对于每一行,列增加 1 * - I am trying to use a for loop in javascript to create a a table that displays * and for each row the columns increase by 1 * 用javascript填充输入搜索字段(字段动态显示结果) - Fill input search field with javascript (field dynamically displays results) 如何使用reactJS创建过滤的搜索表。 试图让我的搜索栏过滤表中的信息 - How to create a filtered search table using reactJS. Trying to get my search bar to filter the info in the table 尝试将搜索列表附加到我的搜索输入 - Trying to Append search list to my search input
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM