英文:
How to concatenate a href in a go html template
问题
我很乐意帮助你解决这个问题。我一直在查看类似的问题,但似乎没有一个能解决我的问题。
我在go模板中有一个用户表格。在最右边的列中,我有两个按钮,用于更新和删除用户。我希望这些按钮指向一个URL,以便更新和删除特定的用户。因此,我需要在URL中包含用户ID。
以下是代码:
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Date Of Birth</th>
<th>County</th>
<th>Books</th>
<th>Perform Action</th>
</tr>
</thead>
<tbody>
{{ range .Users}}
<tr>
<td>{{ .Id }}</td>
<td>{{ .Name }}</td>
<td>{{ .DateOfBirth }}</td>
<td>{{ .County }}</td>
<td>
{{if .Books}}
{{ range $id, $book := .Books }}
<li>{{ $book.Title }}</li>
{{ end }}
{{else}}
No Books
{{end}}
</td>
{{ $updateUrl := "http://localhost:8080/library/updateuser/" + .Id }}
<td><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Update User</a><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Delete User</a></td>
</tr>
{{ end}}
</tbody>
</table>
在倒数第5行,你会看到我错误地尝试将href与id连接起来。
英文:
I'd really appreciate if somebody could help me out with this. I've been going through similar questions asked but none seem to help with my problem.
I have a User table in a go template. In the right most column I have two buttons to update and delete a user. I want these buttons to point to a url that will update and delete that specific user. So I need to have the user id in the url.
This is the code:
<table class="table table-striped">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Date Of Birth</th>
<th>County</th>
<th>Books</th>
<th>Perform Action</th>
</tr>
</thead>
<tbody>
{{ range .Users}}
<tr>
<td>{{ .Id }}</td>
<td>{{ .Name }}</td>
<td>{{ .DateOfBirth }}</td>
<td>{{ .County }}</td>
<td>
{{if .Books}}
{{ range $id, $book := .Books }}
<li>{{ $book.Title }}</li>
{{ end }}
{{else}}
No Books
{{end}}
</td>
{{ $updateUrl := "http://localhost:8080/library/updateuser/" + .Id }}
<td><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Update User</a><a href="http://localhost:8080/library/deleteuser/"+{{ .Id }} class="btn btn-outline-dark ml-3">Delete User</a></td>
</tr>
{{ end}}
</tbody>
</table>
In the 5th line up from the bottom you'll see my incorrect attempt at concatenating the href with the id.
答案1
得分: 3
你不能使用+
来连接字符串。应该使用以下方式:
<a href="http://localhost:8080/library/deleteuser/{{ .Id }}">
英文:
You cannot concatenate strings using +
. This should work:
<a href="http://localhost:8080/library/deleteuser/{{ .Id }}">
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论