【问题标题】:Meteor fill a form with data from database if there is one, if not show the form流星用数据库中的数据填充表格,如果有,则显示表格
【发布时间】:2016-03-06 09:04:01
【问题描述】:

我正在学习如何使用 MeteorJS。我正在尝试使用数据库的返回预填充表单,所有文本输入都没有问题,但我怎么能检查收音机?我从数据库中得到的是一个很长的表格,至少有 150 个字段。所以我将不得不处理这个很多,但我不知道如何。显然我已经在检索正确的文档,所有其他字段都很好,收音机的值很好,但是我如何使用车把或任何其他方式将选中的属性添加到它们。

Template.myTemp.helpers({
   full : function () {
      var id = Session.get('currentId');
      if (id) {
         return People.findOne({_id: id});
      } else {
         return true;
      }
   }
});

好的,所以这里返回 true 只是为了在没有定义 Session 的情况下显示没有预填充值的表单。下一个在我的模板的简短版本中

<template name="myTemp">
  {{#with full}}
  <div class="input-field">
        <label for="name" class="active">Name</label>
        <input id="name" name="name" type="text" value= "{{name}}">
  </div>
  <p>
        <input id="fem" name="sex" type="radio" value="FEM" class="with-gap">
        <label for="fem">Female</label>
        <input id="male" name="sex" type="radio" value="MALE" class="with-gap">
        <label for="male">Male</label>
  </p>
  {{/with}}
</template>

所以这就是诀窍,当我使用此表单插入它时效果很好,但是当尝试预填充它时,它是一场噩梦。如果我得到的返回值是存储在数据库中的表单中的值,我该如何检查这个单选按钮。

【问题讨论】:

    标签: javascript forms meteor radio-button autofill


    【解决方案1】:

    我假设您的 People 集合的文档类似于以下对象:

    { "_id" : "adCsEGoHFbpJbFjtP", "name" : "John Doe", "sex" : "male" }
    { "_id" : "TYLDYicCzpHSD5Sk5", "name" : "Jane Doe", "sex" : "female" }
    

    如果是这种情况,您可以在 myTemp 模板中定义一个帮助器,如果文档的属性 type 与函数的参数匹配,则返回字符串 checked

    if (Meteor.isClient) {
        Template.myTemp.helpers({
            full: function () {
                var id = Session.get('currentId');
                if (id) return People.findOne({_id: id});
                else return true;
            },
            isChecked: function (type) {
                return (this && this.sex === type) ? 'checked' : null;
            }
        });
    }
    
    People = new Mongo.Collection("people");
    
    if (Meteor.isServer) {
        Meteor.startup(function () {
            People.insert({
                "name": "John Doe",
                "sex": "male"
            });
            People.insert({
                "name": "Jane Doe",
                "sex": "female"
            });
        });
    }
    

    <template name="myTemp">
        {{#with full}}
            <div class="input-field">
                <label for="name" class="active">Name</label>
                <input id="name" name="name" type="text" value="{{name}}">
            </div>
            <p>
                <input id="fem" name="sex" type="radio" value="FEM" class="with-gap" {{isChecked 'female'}}>
                <label for="fem">Female</label>
                <input id="male" name="sex" type="radio" value="MALE" class="with-gap" {{isChecked 'male'}}>
                <label for="male">Male</label>
            </p>
        {{/with}}
    </template>
    

    这是MeteorPad

    【讨论】:

    • 我已经尝试过您提供的代码,并且效果很好。但是你能帮我一下吗,我不明白这个功能(类型)是如何工作的。参数类型来自哪里。即使复制粘贴您的代码,我也无法使其与我的代码一起使用。所以我想如果我能理解你的代码,我可能会让事情顺利进行
    • 参数type 可以通过{{isChecked 'female'}}{{isChecked 'male'}} 等Meteor 模板助手传递。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-25
    相关资源
    最近更新 更多