简体   繁体   English

如何使用 Express/MongoDB/Nodejs 在 EJS 模板中创建动态 select 选项?

[英]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:我尝试通过以下方式对此进行故障排除:

  1. Creating a standalone js file with the arrays after they are queried and passing that into the EJS template.在查询 arrays 并将其传递到 EJS 模板后,创建一个独立的 js 文件。 From looking at other stackoverflow posts it didn't seem like this would work because you can't pass a js variable value to an ejs variable.从查看其他 stackoverflow 帖子来看,这似乎不起作用,因为您无法将 js 变量值传递给 ejs 变量。
  2. I can't save the array/variable in a section of the EJS template because once the page loads I essentially lose access to it (this is client/server related, but I am not well versed enough in this topic to explain this better - hopefully someday).我无法将数组/变量保存在 EJS 模板的一部分中,因为一旦页面加载,我基本上就无法访问它(这是与客户端/服务器相关的,但我对这个主题的了解还不够,无法更好地解释这一点 -希望有一天)。

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.提前感谢任何反馈或资源。

If you are a novice developer, look towards SPA (Single Page) applications that are created using React .如果您是新手开发人员,请查看使用React创建的 SPA(单页)应用程序。 Look at the example of this library from github .github看这个库的例子。 Data should be transferred using the http protocol.应使用 http 协议传输数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM