R
R
Ruslan Absalyamov2018-11-09 11:04:58
Vue.js
Ruslan Absalyamov, 2018-11-09 11:04:58

How can the code be improved when searching for multiple fields?

I know that I made a crutch, but I made it approximately how the code would work and showed how I want to do it.
That is, what I want to get from this, if it is not clear, that he searches in the last name, first name, patronymic, phone and email columns for the value that is indicated in the search.
My poor implementation looks like this

/src/views/AppTest.vue:278
this.items
        .filter(item => {
          let search = this.search.toLowerCase();
          let name = item.name.toLowerCase().includes(search);
          let last_name = item.last_name.toLowerCase().includes(search);
          let patronymic = item.patronymic.toLowerCase().includes(search);
          let phone = item.phone.toLowerCase().includes(search);
          let email = item.email.toLowerCase().includes(search);

//Если что-то нашел у поле, то это он возвращает
          if (name) return name; 
          if (last_name) return last_name;
          if (patronymic) return patronymic;
          if (email) return email;
          if (phone) return phone;
        })

There are a lot of repeating moments, but this is due to the fact that I do not know how to implement it,
this is how it looks in the sandbox https://codesandbox.io/s/pyvjxxnnvj

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vladimir Malkov, 2018-11-09
@MalkovVladimir73

An example from the vue documentation: grid with search.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question