gpt4 book ai didi

javascript - Meteor AutoForm 中的条件(类别/子类别)选项

转载 作者:行者123 更新时间:2023-12-03 07:26:07 25 4
gpt4 key购买 nike

我目前正在使用 autoform 和 collection2 来生成表单。我想创建一个更改子类别选项的选择选项。

例如。选择(类别)- 水果- 蔬菜

例如。选择(出现子类别)

如果选择水果:

  • 苹果
  • 香蕉

如果选择蔬菜:

  • 胡萝卜
  • 西兰花

我一直在寻找解决方案,但找不到有效的解决方案。有人可以指出我正确的方向吗,因为我不知道从哪里开始。

最佳答案

您可以使用 AutoForm.getFieldValue(fieldName, [formId]) 检索category 的当前值。然后,您可以set the subcategory options取决于是否选择了水果蔬菜

例如:

var fruitArr = ['apple', 'banana'];
var vegetablesArr = ['carrot', 'broccoli'];

Food = new Mongo.Collection("food");

Food.attachSchema(new SimpleSchema({
category: {
type: String,
label: "Category",
allowedValues: ['fruit', 'vegetables']
},
subcategory: {
type: String,
label: "Subcategory",
allowedValues: _.union(fruitArr, vegetablesArr),
autoform: {
options: function () {
let category = AutoForm.getFieldValue("category");
if (!category) return [{label: "Please select a category first", value: ""}];
if (category === "fruit") return _.map(fruitArr, (v, i) => ({
label: "Fruit " + (i + 1) + ": " + v,
value: v
}));
else return _.map(vegetablesArr, (v, i) => ({label: "Vegetables " + (i + 1) + ": " + v, value: v}));
}
}
}
}));

关于javascript - Meteor AutoForm 中的条件(类别/子类别)选项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36001725/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com