使用节点和 ejs 在单击时显示 table

Displaying table on click using node and ejs

我有一个 view.ejs 文件,其中包含一个按钮和一个 table。单击按钮时,我希望填充 table,问题是要填充 table 使用的变量 viewtabObj 在按钮被调用之前未定义点击。这是当前代码:

    <form action="/populatetable" method="get">
        <input class="button" type="submit" value="Populate Table">
    </form>

    <table class="table table-hover">
        <thead class="thead-light">
          <tr>
            <th scope="col">#</th>
            <th scope="col">Complete</th>
            <th scope="col">Completion Date</th>
            <th scope="col">Identifier</th>
          </tr>
        </thead>
        <tbody>
          <% var x = 1 %>
          <% millViewObj.forEach(function(obj) { %>
            <tr>
              <th scope="row"><%= x %></th>
              <td><%= obj.complete %></td>
              <td><%= obj.completion_date %></td>
              <td><%= obj.id %></td>
          <% x++;}); %>
        </tbody>
      </table>

有没有办法在单击按钮后在同一页面上呈现 table,这样我就不会收到未定义变量的错误?

我能够消除未定义变量的错误,如下所示:

<% if (locals.millViewObj) { %>
<% var x = 1 %>
   <% millViewObj.forEach(function(obj) { %>
   <tr>
      <th scope="row"><%= x %></th>
      <td><%= obj.complete %></td>
      <td><%= obj.completion_date %></td>
      <td><%= obj.id %></td>
<% x++;}); %>
<% } %>