简体   繁体   English

如何在Vuejs中过滤多个元素

[英]How to filter by more than one element in Vuejs

I'm implementing an application in Vuejs , I'm having two select in which child select is being filtered if any options is being selected in parent select. 我正在Vuejs实现一个应用程序,如果在父选择中选择了任何选项,我有两个选择正在过滤子选择。 I want to add an extra filter of checking whether it is a client or not: 我想添加一个额外的过滤器来检查它是否是客户端:

I've a model.data which holds all the data, it is basically an array of elements : 我有一个包含所有数据的model.data,它基本上是一个元素数组:

model.data: [
    {id: 1, name: XYZ 1, is_client: 0, address: "ABCD Address 1"},
    {id: 2, name: XYZ 2, is_client: 1, address: "ABCD Address 2"},
    {id: 3, name: XYZ 3, is_client: 0, address: "ABCD Address 3"},
]

I'm having a v-model="company_name" of parent select which is used as filter in child select 我有一个父v-model="company_name"v-model="company_name" ,它在子选择中用作过滤器

filteredCompanyOptions() {
    if (this.model.data)
    {
        return this.model.data
            .filter(f => f.name !== this.company_name.label)
            .map(d => ({label: d.name, value: d.id}))
    }
}

Guide me how to achieve this. 指导我如何实现这一目标。

If the value returned by the callback to filter is true the element will be added to the filtered array you can use a many boolean operation as you like: 如果回调函数返回的值为true,则元素将添加到已过滤的数组中,您可以根据需要使用许多布尔运算:

filteredCompanyOptions () {
  if (this.model.data) {
    return this.model.data
      .filter(f => f.name !== this.company_name.label && f.is_client === 1)
      .map(d => ({ label: d.name, value: d.id }))
  }
}

if your condition is more complicated you can do all you need in the body of the callback then return a boolean: 如果你的条件更复杂,你可以在回调体中做你需要的所有事情然后返回一个布尔值:

filteredCompanyOptions () {
  if (this.model.data) {
    return this.model.data
      .filter(f => {
        if (f.name !== this.company_name.label && f.is_client === 1) {
          return true
        } else {
          return false
        }
      })
      .map(d => ({ label: d.name, value: d.id }))
  }
}

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

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