如何以“插入"形式传递字段的默认值? [英] How to pass a default value for a field in 'insert' form?
问题描述
如何以插入"形式传递字段的默认值?
How to pass a default value for a field in 'insert' form?
我正在使用Meteor的软件包:Autoform,Collections2和Simple-Schema.
I'm using Meteor's packages: Autoform, Collections2, and Simple-Schema.
我的过程是:
- 用户在页面列表中选择一些值,然后
- 打开插入",我希望使用用户在上一步中选择的值来初始化一个字段.
无法弄清楚如何通过URL(或任何其他方式)传递参数. 问题是如何用值初始化表单.
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.
假设我有一个网址:
http://localhost:3000/activity/new/Sport
================ router.js:
=============== 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
=============== models/activity.js
...
Activities = new Meteor.Collection("activities", {
schema: {
title: {
type: String,
label: 'название'
},
...
tag: {
type: String,
label: 'тэг'
}
}
});
================= 模板/avtibity.js
================ templates/avtibity.js
...
Template.newActivity.helpers({
defaultTag: function() {
return Session.get('tag');
}
});
...
================= templates/activity.html
================ 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>
```
推荐答案
感谢Eric Dobbertin:
Thanks to Eric Dobbertin:
- 您可以将value设置为等于返回所需值的助手({{> afQuickField name ="tag" value = valueHelper}})
- 列表项您可以将doc设置为具有设置为所需值的对象.就像您要更新表格一样.
https://github.com/aldeed/meteor-autoform/issues/210
这篇关于如何以“插入"形式传递字段的默认值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!