【问题标题】:Populating a MeteorJS Autoform fields填充 MeteorJS Autoform 字段
【发布时间】:2015-01-18 12:52:44
【问题描述】:

我在 Meteor (https://github.com/aldeed/meteor-autoform) 中使用了非常好的 Autoform 包。我的反应式表单工作得很好-但想要填充表单数据以允许根据用户在表中选择一行进行编辑。非常简单的例子可以在这里找到:

http://autoform.meteor.com/insertaf

实际上,我想使用用户单击以进行编辑的“人员”行中的数据填充表单数据,但不知道如何执行此操作。任何有关如何执行此操作的示例将不胜感激。谢谢!

表格代码:

{{#autoForm id="afInsertDemo" type="insert" collection=Collections.People}}
  <div class="form-group {{#if afFieldIsInvalid name='firstName'}}has-error{{/if}}">
    <label class="control-label">{{afFieldLabelText name='firstName'}}</label>
    {{> afFieldInput name='firstName'}}
    {{#if afFieldIsInvalid name='firstName'}}
    <span class="help-block">{{{afFieldMessage name='firstName'}}}</span>
    {{/if}}
  </div>
  <div class="form-group {{#if afFieldIsInvalid name='lastName'}}has-error{{/if}}">
    <label class="control-label">{{afFieldLabelText name='lastName'}}</label>
    {{> afFieldInput name='lastName'}}
    {{#if afFieldIsInvalid name='lastName'}}
    <span class="help-block">{{{afFieldMessage name='lastName'}}}</span>
    {{/if}}
  </div>
  <div class="form-group {{#if afFieldIsInvalid name='age'}}has-error{{/if}}">
    <label class="control-label">{{afFieldLabelText name='age'}}</label>
    {{> afFieldInput name='age'}}
    {{#if afFieldIsInvalid name='age'}}
    <span class="help-block">{{{afFieldMessage name='age'}}}</span>
    {{/if}}
  </div>
  <div class="form-group">
    <button type="submit" class="btn btn-primary">Add Person</button>
    <button type="reset" class="btn btn-default">Reset Form</button>
  </div>
{{/autoForm}}

流星Javascript

Schemas = {};

UI.registerHelper("Schemas", Schemas);

Schemas.Person = new SimpleSchema({
  firstName: {
    type: String,
    index: 1,
    unique: true
  },
  lastName: {
    type: String,
    optional: true
  },
  age: {
    type: Number,
    optional: true
  }
});

var Collections = {};

UI.registerHelper("Collections", Collections);

People = Collections.People = new Mongo.Collection("People");
People.attachSchema(Schemas.Person);

Meteor.publish(null, function () {
  return People.find();
});

People.allow({
  insert: function () {
    return true;
  },
  remove: function () {
    return true;
  }
});

【问题讨论】:

    标签: javascript meteor meteor-autoform


    【解决方案1】:

    只是改变

    {{#autoForm id="afInsertDemo" type="insert" collection=Collections.People}}
    

    到

    {{#autoForm id="afUpdateDemo" type="update" doc=someDoc collection=Collections.People}}
    

    因此,您需要将 type 属性从 insert 更改为 update,并添加一个 doc 属性来告诉自动表单它将更新哪个文档。你可以从你的模板助手返回doc。

    https://github.com/aldeed/meteor-autoform#autoform-1 上的 autoform 组件的 autoform 文档将 type 和 doc 属性解释为:

    type:可选。 “插入”、“更新”或“方法”之一。设置 type 到其他任何东西或省略它都会调用任何 onSubmit 钩子, 您可以在其中执行自定义提交逻辑。如果onSubmit 没有返回 false 或调用this.event.preventDefault(),浏览器也会 提交表格。这意味着您可以使用 AutoForm 生成和 验证表单,但仍将其正常 POST 到某些非 Meteor HTTP 端点。

    doc:更新表单所必需的,并且必须至少有一个_id 财产。传递当前文档对象,通过调用来检索 findOne() 例如。对于插入表单,您也可以使用此 属性传递一个设置了默认表单值的对象(相同 效果就像在表单中的每个字段上设置一个 value 属性)。

    注意:我还更改了id 属性,以便您以后可以单独引用此表单。但是有一些方法可以重用单个表单进行更新/插入,如 https://github.com/aldeed/meteor-autoform#can-i-reuse-the-same-quickform-or-autoform-for-both-inserts-and-updates 中所述

    我可以为插入和更新重复使用相同的 quickForm 或 autoForm 吗?

    是的。您在状态之间翻转的代码应执行以下操作 这个顺序:

    1. 根据需要将类型属性的值更改为“插入”或“更新”,可能是通过更新反应变量。

    2. 将 doc 属性的值更改为正确的文档以进行更新或更改为 null(或包含默认值的文档)以获取 插入,可能是通过更新一个反应变量。

    3. 调用 AutoForm.resetForm(formId)。这将清除表单的任何现有验证错误。

    【讨论】:

    • 感谢您的澄清-我相信我需要做的是在上面示例中的“人员”表中添加一个“编辑”按钮,该按钮获取文档的 _id 并将其传递给自动编辑/更新。我被困在如何做到这一点上。你能提供一个简单的例子吗?
    • 您可以查看autoform.meteor.com 以获取可用作起点的示例。或者您可以使用meteorkitchen.com 创建一个示例应用程序(它是一个使用 autoform 的自动应用程序生成器)并检查生成的源代码。
    猜你喜欢
    • 1970-01-01
    • 2012-01-22
    • 2015-11-13
    • 1970-01-01
    • 1970-01-01
    • 2021-05-06
    • 2017-05-21
    • 1970-01-01
    • 2017-07-25
    相关资源
    最近更新 更多