【问题标题】:Dependent dropdown menu in different collection不同集合中的相关下拉菜单
【发布时间】:2019-02-19 11:46:19
【问题描述】:

我是 Meteor 的新手。我正在尝试使下拉菜单依赖于其他下拉菜单。第一个用于客户集合中的客户名称,第二个用于地址集合中的客户地址。我有 2 个集合客户和地址。这是我的代码,但不知道下一步该做什么。

编辑:我将两个模板都放在另一个名为 new order 的模板中

HTML:

<template name="selectClient">
  Client Name :
<select class="select">
  <option selected disabled>Choose client name</option>
  {{#each custom}}
  <option>{{clientName}}</option>
  {{/each}}
</select>
</template>
<template name="selectAddress">
  Address:
<select class="select" name="Choose the address">
  <option selected disabled>Choose the address</option>
  {{#each address}}
  <option>{{addressName}}</option>
  {{/each}}
</select>
</template>

main.js

Template.selectClient.helpers({
    'custom': function(){
        return Customers.find();
    }
  });
  Template.selectAddress.helpers({
    'address': function(){
        return Addresses.find();
    }
});


var clientName= $('[name="newName"]').val();
    var mobNumber = $('[name="newMob"]').val();
    var age = $('[name="age"]').val();
    var radioValue= $('[name="gender"]').val();
    Customers.insert({
    clientName: clientName,
    age: age,
    radioValue:gender,
    createdAt: new Date()
    });

var addressName = $('[name="addressName"]').val();
    var detail = $('[name= details]').val();
    Addresses.insert({
      addressName: addressName,
      detail: detail,
      createdAt: new Date()
    });

Customers = new Mongo.Collection('customers');
Addresses = new Mongo.Collection('addresses');
Mobile = new Mongo.Collection('mobile');

【问题讨论】:

  • 你在使用 Meteor 和 Blaze、Angular 还是 React?
  • 不,我没有使用任何库
  • 您阅读过这些教程吗? meteor.com/tutorials
  • 中级
  • 如果我能在这一点上给你一些建议:你不应该把代码像这样放在 cmets 中。您可能希望将其添加到您的问题中,以便人们更容易帮助您。只是我的建议。

标签: javascript html mongodb meteor dropdown


【解决方案1】:

由于您并行使用两个模板(而不是在父子关系中),您可以使用ReactiveVar 来缓存当前选定的客户端名称:

const selectedCustomer = new ReactiveVar()

请注意,这两个模板都需要可访问它。要么在一个文件中使用两个模板声明它,要么使用 import / export 来提供对多个文件的访问。

现在您的客户select 需要为每个option 分配一个value,因此我们可以在选择更改时对其进行缓存:

<template name="selectClient">
    Client Name :
    <select class="select-customer">
        <option selected disabled>Choose client name</option>
        {{#each custom}}
            <option value="clientName" selected="{{#if selectedClient clientName}}selected{{/if}}">{{clientName}}</option>
        {{/each}}
    </select>
</template>

为了防止命名混淆,我将它的类重命名为select-customter。注意到{{#if selectedClient}}... 代码了吗?我们将使用一个助手来恢复下拉列表中最后选择的状态。否则,您的下拉选择将在下一个渲染周期重置:

Template.selectClient.helpers({
  'custom': function(){
    return Customers.find();
  },
  selectedClient(name) {
    const selected = selectedCustomer.get()
    return selected && selected.clientName === name
  }
});

我们从缓存中获取选定的客户,并检查当前选项的值是否相同。如果为真,我们可以将选项标记为selected

现在您仍然需要一个涵盖所选客户的事件:

Template.selectClient.events({
  'change .select'(event, templateInstance) {
    // get the value using jQuery
    const value = templateInstance.$(event.currentTarget).val()
    // update the ReactiveVar
    selectedCustomer.set({ clientName: value })
  }
})

它将选定的值(当前为clientName)以可查询的格式保存。现在在您的地址中,您只需要使用缓存的选定客户端查询所有 Adresses 文档:

Template.selectAddress.helpers({
  'address': function(){
    const query = selectedCustomer.get() || {}
    return Addresses.find(query);
  }
});

如果选择了客户端,它将作为查询服务器,否则将返回所有地址。

好消息是,这个 ReactiveVar 已经为您提供了在更新时触发新渲染周期的能力,因为您的助手代码依赖于它,而 Blaze 会自动为您解决这个问题。

修饰符

此代码假定 Adresses 通过名为 clientName 的字段与 Customers 建立关系。如果您使用其他字段存储了关系,例如_id - clientId,则需要相应地修改您的代码。

如果selectedCustomer 中有值,您也可以隐藏第二个下拉菜单并仅显示它。

【讨论】:

  • 非常感谢您的努力,但我如何通过名为 clientName 的字段在客户和地址之间建立关联。
  • 我得到了这个错误..'“响应式 HTML 属性必须要么有一个常量名称,要么由一个提供名称和值字典的 {{helper}} 组成。这里不允许使用 BLOCKOPEN 类型的模板标签。”.. 抱歉,但正如我所说,我是 Meteor 的新手,这是我的第一个项目。
  • 哦,我错过了一些代码,我更新了 html 模板。关于关系 - 请张贴客户和地址的架构,以便更容易解释。
  • 好的,非常感谢,它对我有用..我编辑了我的问题并写了值应该如何显示在我的数据库中,但我没有使用 simpleschema,但如果有必要,你能帮我解决吗..提前致谢
猜你喜欢
  • 2021-12-21
  • 2021-11-17
  • 1970-01-01
  • 1970-01-01
  • 2019-12-05
  • 1970-01-01
  • 1970-01-01
  • 2014-04-19
  • 1970-01-01
相关资源
最近更新 更多