【问题标题】:Filters in VueJsVueJs 中的过滤器
【发布时间】:2017-11-30 04:31:10
【问题描述】:

我正在尝试在 Vuejs 2.0 中构建一个小型应用程序,我正在使用 v-select 组件,我的数据格式如下:

{
    "model":
        [
            {
                "id":1,
                "salutation":"Mr",
                "first_name":"Rahul",
                "last_name":"Bashisht",
                "number":"9876521102",
                "email":"rahul@icicibank.com",
                "company":
                    {
                        "id":1,
                        "name":"ICICI Bank",
                        "is_client":1,
                    }
            },
            {
                "id":2,
                "salutation":"Mr",
                "first_name":"Vikash",
                "last_name":"Pandey",
                "number":"0987654345",
                "email":"vikash@hdfc.com",
                "company":
                    {
                        "id":2,
                        "name":"HDFC Bank",
                        "is_client":0,
                    }
            }
        ]
}

现在我将其设置为变量model,然后尝试在computed property 中使用client = 1 进行过滤,如下所示:

contactClients: function() {
    if(this.model)
    {
        return this.model
            .filter(f => (f.company.is_client == 1))
            .map(d => ({label: d.first_name+' '+d.last_name+' - '+d.company.name, value: d.id}))
    }
},

然后我将它放在 v-select 选项中:

<v-select multiple :options="contactClients" v-model="clientParticipants"></v-select>

现在我有另一个 v-select,它与公司名称一致,但 is_client 是真的,所以我正在尝试这样的事情:

我有公司的数据集:

{
    "model":
        [
            {
                "id":1,
                "name":"ICICI Bank",
                "is_client":1,
            },
            {
                "id":2,
                "name":"HDFC Bank",
                "is_client": 0,
            },
            {
                "id":3,
                "name":"BNP Paribas",
                "is_client": 0,
            }
            {
                "id":4,
                "name":"Barclays Bank",
                "is_client": 1,
            }
        ]
}       

我将它放在companies 变量中并像这样过滤它:

clients: function () {
    if(this.companies)
    {
        return this.companies
            .filter(f => f.is_client == 1)
            .map(d => ({label: d.name, value: d.id}))
    }
} 

在 v-select 中我有:

<v-select :options="clients" v-model="summary.client"></v-select>

我想根据contactsClients的选择有一个额外的过滤器,即如果在第一个列表中选择了任何contactsClients,第二个列表应该只有那些company作为选项,如果没有在第一个列表(contactClients)中选择,然后第二个列表应该包含所有默认选项,以及当前情况下的简单is_client 过滤器。由于contactClients 中的选择是多重的,所以我不知道如何过滤元素。请指导我。

编辑: Codepen Link

【问题讨论】:

标签: javascript vue.js vuejs2


【解决方案1】:

也许这会对你有所帮助。

contactClients: function() {
  if (this.model) {
    return this.model.filter(f => f.company.is_client == 1).map(d => ({
      label: d.first_name + " " + d.last_name + " - " + d.company.name,
      value: d.id,
      companyId: d.company.id
    }));
  }
},
clients: function() {
  var self = this;
  var res = [];
  if (this.companies) {

    if (this.clientParticipants.length) {
      console.log(this.clientParticipants)
      this.clientParticipants.forEach(function(cc) {
        self.companies.forEach(function(c) {
          if (cc.companyId === c.id) {
            res.push(c);
          }
        });
      });
      return res.map(d => ({ label: d.name, value: d.id }));
    } else {
      return this.companies
        .filter(f => f.is_client == 1)
        .map(d => ({ label: d.name, value: d.id }));
    }
  }
}

示例here

【讨论】:

  • 你在和id(cc.value === c.id)比较,但是contact id和company id不一样,不一样。
  • 好的,它们之间的关系是什么?他们之间应该有一些共同因素可以比较吧?
  • 一旦您从model 获取联系人,它也会在关键字company 下附带公司数据,在这里您可以看到每个联系人在company key 下都有公司名称和ID。
  • 不。由于您在contactClients 计算值中对其进行修改,因此它将只有labelvalue。检查this。我已经修改过了。
  • 是的,我明白这一点。但是核心数据集有这些值。我想我们可以从联系人模型数据集中找到并映射值。
【解决方案2】:

您可以使用计算属性在那里实现您的过滤器逻辑并将其绑定到您的第二个列表。 参考:https://vuejs.org/v2/guide/computed.html#Computed-Properties

【讨论】:

  • 我已经在我的计算属性中创建了这些列表,选择列表是一个数组(多个),我不确定如何多次过滤
猜你喜欢
  • 2018-03-08
  • 2018-03-02
  • 2019-03-04
  • 1970-01-01
  • 1970-01-01
  • 2018-02-05
  • 2018-01-04
  • 2020-12-09
  • 1970-01-01
相关资源
最近更新 更多