【问题标题】:How to pass a default value for a field in 'insert' form?如何以“插入”形式传递字段的默认值?
【发布时间】:2014-08-02 23:28:26
【问题描述】:

如何为“插入”表单中的字段传递默认值?

我正在使用 Meteor 的包:Autoform、Collections2 和 Simple-Schema。

我的流程是:

  1. 用户选择页面列表中的某个值,然后
  2. “插入”来自打开,我希望使用用户在上一步中选择的值来初始化该字段。

无法弄清楚如何通过 URL(或任何其他方式)传递参数。 问题是如何用值初始化表单。

假设我有一个网址:

http://localhost:3000/activity/new/Sport

=============== router.js:

...
Router.map(function () {
    ...
    this.route('newActivity', {
        path: '/activity/new/:tag',
        data: function() {
            Session.set('tag', this.params.tag);
            return null;
        }
    });
    ...

=============== 模型/activity.js

...
Activities = new Meteor.Collection("activities", {
    schema: {
        title: {
            type: String,
            label: 'название'
        },
        ...
        tag: {
            type: String,
            label: 'тэг'
        }
    }
});

================ 模板/avtibity.js

...
Template.newActivity.helpers({
    defaultTag: function() {
        return Session.get('tag');
    }
});
...

================ 模板/activity.html

...
<template name="newActivity">
    <h1>Create new Activity!</h1>
    {{#autoForm collection="Activities" id="insertActivityForm" type="insert"}}
        {{> afQuickField name="title"}}
        ...
        {{> afQuickField name="tag" value="   ??????    "}} // ? {{defaultTag}}
        ho ho ho {{defaultTag}}
    {{/autoForm}}
</template>

```

【问题讨论】:

    标签: meteor meteor-autoform simple-schema meteor-collection2


    【解决方案1】:

    感谢 Eric Dobbertin:

    1. 您可以将 value 设置为等于返回所需值的帮助程序 ({{> afQuickField name="tag" value=valueHelper}})
    2. 列表项 您可以将 doc 设置为一个对象,该对象的值设置为您想要的值。就像您需要更新表单一样。

    https://github.com/aldeed/meteor-autoform/issues/210

    【讨论】:

    • 您可能应该在原始帖子和主题标题中指定您正在使用 Autoform、Collections2 和 Simple-Schema 包。
    • 如何访问我的架构中 valueHelper 的值?
    猜你喜欢
    • 1970-01-01
    • 2012-09-09
    • 1970-01-01
    • 2017-04-20
    • 1970-01-01
    • 1970-01-01
    • 2013-11-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多