[英]How can you create dynamic select options within an EJS template using Express/MongoDB/Nodejs?
My dependencies are EJS, Express, and Mongoose.我的依赖项是 EJS、Express 和 Mongoose。
I am trying to figure out how I can make a Form/Select/Option that is dynamically driven by another Select/Option.我试图弄清楚如何制作由另一个选择/选项动态驱动的表单/选择/选项。 In my app.js I have two different arrays, optionOneList and optionTwoList, that are created when I query the database.在我的 app.js 中,我有两个不同的 arrays,optionOneList 和 optionTwoList,它们是在我查询数据库时创建的。 Then in my first EJS section I have an Select list with both of the query parameters as options.然后在我的第一个 EJS 部分中,我有一个 Select 列表,其中两个查询参数都作为选项。 I would like the optionOneList or optionTwoList variables to be used in creating the options for the second EJS template section, and the results will differ depending on what is selected in the first EJS section.我希望在为第二个 EJS 模板部分创建选项时使用 optionOneList 或 optionTwoList 变量,结果将根据在第一个 EJS 部分中选择的内容而有所不同。
I tried troubleshooting this the following ways:我尝试通过以下方式对此进行故障排除:
I also assumed I would have some luck if I just saved optionOneList/optionTwoList as local variables, but that wasn't working for me either.我还假设如果我只是将 optionOneList/optionTwoList 保存为局部变量,我会有一些运气,但这对我也不起作用。
Express Template Section One快递模板第一节
app.get('/general', async (req, res) => {
const optionOneList = await collectionOne.find({ "Person": "queryOne" });
const optionTwoList = await collectionOne.find({ "Person": "queryTwo" });
res.render('general/index', {optionOneList: optionOneList, optionTwoList: optionTwoList} )
});
EJS Template Section One EJS 模板第一节
Note: This first select is what I want to drive what options that are available for the second select注意:第一个 select 是我想要驱动的第二个 select 可用的选项
<select id="firstSelection">
<option selected>Default</option>
<option value="queryOne">queryOne</option>
<option value="queryTwo">queryTwo</option>
</select>
EJS Template Section One EJS 模板第一节
<select id="secondSelection">
<% for (let available of optionOneList) {%>
<option>
<%= available.Name %>
</option>
<% } %>
</select>
I am happy to take links to specific parts of the documentation that could point me in the right direction, or critiques on my understanding of why certain things didn't work.我很高兴链接到文档的特定部分,这些部分可以为我指明正确的方向,或者批评我对为什么某些事情不起作用的理解。 Thanks in advance for any feedback or resources.提前感谢任何反馈或资源。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.