How to pass a default value for a field in 'insert' form?

拜拜、爱过 提交于 2019-12-10 17:09:06

问题


How to pass a default value for a field in 'insert' form?

I'm using Meteor's packages: Autoform, Collections2, and Simple-Schema.

My process is:

  1. A User selects some value in a list on a page, then
  2. The 'insert' from opens, and I want that one field to be initialized with the value the user chose on a previous step.

Can't figure out how to pass a parameter withing URL (or any other way). The problem is how to initialize form with the value.

Suppose I have an 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;
        }
    });
    ...

=============== models/activity.js

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

================ templates/avtibity.js

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

================ templates/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>

```


回答1:


Thanks to Eric Dobbertin:

  1. You could set value equal to a helper that returns the desired value ({{> afQuickField name="tag" value=valueHelper}})
  2. List item You could set doc to an object that has the value set to what you want. Just like you would for an update form.

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



来源:https://stackoverflow.com/questions/24187784/how-to-pass-a-default-value-for-a-field-in-insert-form

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!