简体   繁体   English

不同集合中的从属下拉菜单

[英]Dependent dropdown menu in different collection

I'm new in Meteor. 我是流星的新人。 I'm trying to make dropdown menu dependent in other dropdown. 我试图使下拉菜单依赖于其他下拉菜单。 The first one for client name in Customers collection & the second for the client address in Addresses collection. 第一个用于客户集合中的客户名称,第二个用于地址集合中的客户地址。 I've 2 collections Customers & Addresses. 我有2个集合“客户和地址”。 This is my code but don't know what to do next. 这是我的代码,但不知道下一步该怎么做。

EDIT: i put both templates in another template called new order 编辑:我把两个模板放在另一个称为新订单的模板中

HTML: 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 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');

Since you are using two templates in parallel (and not in a parent-child relation) you may use a ReactiveVar to cache the current selected client name: 由于您正在并行使用两个模板(而不是父子关系),因此可以使用ReactiveVar缓存当前选定的客户端名称:

const selectedCustomer = new ReactiveVar()

Note, that it needs to be accessible for both templates. 请注意,两个模板都需要访问它。 Either you declare it with both templates in one file or your use import / export to provide access over many files. 要么你有一个文件你同时使用模板或宣布其import / export在今后许多文件提供访问。

Now your customers select needs to have a value assigned to each option , so we can cache it on selection change: 现在,您的客户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>

I renamed it's class, to prevent confusion in naming, to select-customter . 为了避免命名混乱,我将其重命名为select-customter Noticed the {{#if selectedClient}}... code? 注意到{{#if selectedClient}}...代码了吗? We will use a helper to restore the last selected state on the dropdown. 我们将使用一个助手来恢复下拉菜单上的最后选择状态。 Otherwise your dropdown selection will reset on the next rendering cycle: 否则,您的下拉选择将在下一个渲染周期重置:

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

We get the selected customer from the cache and check if the current option's value is the same. 我们从缓存中获取选定的客户,并检查当前选项的值是否相同。 If true we can flag the option as selected . 如果为true,则可以将选项标记为selected

Now you still need an event that covers the selected client: 现在,您仍然需要一个覆盖所选客户端的事件:

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 })
  }
})

It saves the selected value (currently the clientName ) in a query-able format. 它将所选值(当前为clientName )以可查询的格式保存。 Now in your adresses you just need to query all Adresses documents using the cached selected client: 现在,在您的地址中,您只需要使用缓存的选定客户端查询所有Adresses文件:

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

If a client is selected it will server as the query, otherwise all adresses will be returned. 如果选择了客户端,它将作为查询服务器,否则将返回所有地址。

The good thing is, that this ReactiveVar already provides you the capability to trigger a new rendering cycle if it updates, since your helpers' code relies on it and Blaze automatically resolves this for you. 好处是,此ReactiveVar已经为您提供了在更新时触发新渲染周期的功能,因为您的助手代码依赖于此,并且Blaze会自动为您解决此问题。

Modificatopns Modificatopns

This code assumes, that Adresses have a relation to Customers by a field named clientName . 此代码假定Adresses通过名为clientName的字段与Customers有关系。 If you have stored the relation by using other fields, such as _id - clientId you need to modify your code accordingly. 如果使用其他字段(例如_id - clientId存储了关系,则需要相应地修改代码。

You could also hide the second dropdown and only display it, if there is a value in selectedCustomer . 如果selectedCustomer有一个值,您也可以隐藏第二个下拉列表并仅显示它。

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

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