【问题标题】:FreeMarker do is have a option to assing value to variable from static html textFreeMarker 可以选择从静态 html 文本中为变量赋值
【发布时间】:2021-12-31 15:58:04
【问题描述】:

我在网上搜索我无法弄清楚是否有机会从列表中的静态 html 中为变量赋值。

<#list users as user>
   <#assign userId = //the user id value here// ><p>${user.id} </p>
</#list>

用户id值必须来自&lt;p&gt;标签

编辑

<tbody>
<#assign count = 1>
<#list departmentsList as department>
<tr>
  <td>${count}</td>
  <td>${department.name}</td>
  <td class="text-right">
    <div class="dropdown dropdown-action">
      <a href="#" class="action-icon dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="material-icons">more_vert</i></a>
        <div class="dropdown-menu dropdown-menu-right">
            <a class="dropdown-item" href="#" data-toggle="modal" data-target="#edit_department"><i class="fa fa-pencil m-r-5"></i>Edit</a>
            <a class="dropdown-item" href="#" data-toggle="modal" data-target="#delete_department"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
      <#assign employeeId = ${employee.id}>
      </div>
    </div>
  </td>
</tr>

<#assign count ++>
</#list>
</tbody>

${employeeId} 从列表中返回最后一个元素。我需要选择。

这是我使用的表格我做一个循环表单列表我需要单击锚点来获取当前部门 ID 我尝试使用 href,但它不起作用,因为 data-target="#edit_department 使用 JavaScript 并且不起作用。当按下 EDIT 或 DELETE 以使用 HTML 中的变量时,如何获取所选部门的 ID,该变量在按下锚点后打开并位于另一个 div 中

带有操作表单的 HTML DIV

  <div id="edit_department" class="modal custom-modal fade" role="dialog">
<div class="modal-dialog modal-dialog-centered" role="document">
  <div class="modal-content">
    <div class="modal-header">
      <h5 class="modal-title">Edit Department</h5>
      <button type="button" class="close" data-dismiss="modal" aria-label="Close">
        <span aria-hidden="true">&times;</span>
      </button>
    </div>
    <div class="modal-body">
      <form  action="departments/update/${departmentId}" method="post">
        <div class="form-group">
          <label>Department Name <span class="text-danger">*</span></label>
          <input name="name" class="form-control" type="text">
        </div>
        <div class="submit-section">
          <button class="btn btn-primary submit-btn">Save</button>
        </div>
      </form>
    </div>
  </div>
</div>

【问题讨论】:

  • 您的表格可以有许多部门,每个部门都有自己的 id,但您的 JavaScript 代码只能打开一个 &lt;div id="edit_department"&gt;。因此,您的 JavaScript 代码必须读出需要编辑的部门 id 并插入到 &lt;div id="edit_department"&gt; 内的表单中(并且为了进行编辑,您可能还希望将当前部门名称设置为默认名称)。跨度>
  • 与问题无关,但请注意,您不需要维护自己的count 变量。相反,您可以使用&lt;td&gt;${department?counter}&lt;/td&gt;
  • 是的,我在发布问题时对此并不熟悉。谢谢。

标签: java html spring templates freemarker


【解决方案1】:

不确定您对 来自静态 html 文本的含义是什么:写入 &lt;p&gt; 标记的用户 ID 值是动态创建的(因为您使用的是 ${user.id})。

当然,您可以在 assign 指令中使用相同的值:

<#assgin userId="${user.id}">

或者(甚至更好更短):

<#assgin userId=user.id>

请参阅FreeMarker documenation 了解更多信息。


在您更具体的示例中,您可能希望在链接中有两个 data- 属性(一个用于操作,另一个用于部门 ID):

<tbody>
<#assign count = 1>
<#list departmentsList as department>
<tr>
  <td>${count}</td>
  <td>${department.name}</td>
  <td class="text-right">
    <div class="dropdown dropdown-action">
      <a href="#" class="action-icon dropdown-toggle" data-toggle="dropdown" aria-expanded="false"><i class="material-icons">more_vert</i></a>
        <div class="dropdown-menu dropdown-menu-right">
            <a class="dropdown-item" href="#" data-toggle="modal" data-operation="#edit_department" data-target="${department.id}"><i class="fa fa-pencil m-r-5"></i>Edit</a>
            <a class="dropdown-item" href="#" data-toggle="modal" data-operation="#delete_department" data-target="${department.id}"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
      </div>
    </div>
  </td>
</tr>

<#assign count ++>
</#list>
</tbody>

然后您的 JavaScript 代码可以访问操作以及要处理的部门的 id。

【讨论】:

  • 是的,我知道,但是 ${user.id} 在循环中并分配列表中的最后一个元素。我需要当前表索引中的值。这就是为什么我需要静态文本。为此,我在文档中看到是不可能的
  • &lt;#assign userId=user.id&gt; - 不需要"${}"
  • @mikebrucks 我不明白。 “当前表索引”是什么意思?也许你想要user?indexuser?counter?见freemarker.apache.org/docs/…
  • 我需要将对象用户的 ID 分配给变量,但是当我这样做时 我得到了列表中元素的最后一个 ID,因为是n循环并保存最后一个值
  • @mikebrucks 如果您有例如 10 个用户,您的模板片段会生成 10 个 &lt;p&gt; 标签,其中包含 10 个不同的用户 ID。如果您只需要其中一个,那么您的问题就很不清楚,您需要包含更多代码来显示您需要哪些用户 ID。
猜你喜欢
  • 1970-01-01
  • 2020-01-11
  • 2012-12-01
  • 2011-02-24
  • 1970-01-01
  • 2016-09-29
  • 1970-01-01
  • 2014-07-03
  • 1970-01-01
相关资源
最近更新 更多