'FreeMarker grab variable from list when is in loop that is selected from table

I am trying to get a variable department.id for the selected department when anchor edit or delete is clicked I try with <#assgn departmentId = "${department.id}"> but that return the last id from the table.Since is in the loop I also try with onclick="<#assgn departmentId = "${department.id}">" this also return me last id is there a way to get the current id of the department when is clicked edit or delete with Apache Free Marker

HTML

<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>
            </div>
          </div>
        </td>
      </tr>
      <#assign count ++>
      </#list>
</tbody>


Solution 1:[1]

<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" onclick="myEditFunction(${department.id})"><i class="fa fa-pencil m-r-5"></i> Edit</a>
              <a class="dropdown-item" href="#" data-toggle="modal" data-target="#delete_department" onclick="myDeleteFunction(${department.id})"><i class="fa fa-trash-o m-r-5"></i> Delete</a>
        </div>
      </div>
    </td>
  </tr>
  <#assign count ++>
  </#list>
</tbody>   

<script>
function myDeleteFunction(departmentId) {
  console.log("deleting department " + departmentId);
}

function myEditFunction(departmentId) {
  console.log("editing department " + departmentId);
}
</script>

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1