Go可以捕获它正在提供的HTML文档中的点击事件吗?

huangapple go评论72阅读模式
英文:

Can Go capture a click event in an HTML document it is serving?

问题

我正在编写一个用于管理库存的程序。它根据来自PostgreSQL数据库的记录提供基于HTML的服务,或者使用HTML表单将数据写入数据库。

不同的功能(添加记录、搜索等)可以通过<a></a>标签或表单提交来访问,这些标签或提交会调用http.HandleFunc()函数,函数会生成查询,解析结果并将其呈现到HTML模板中。

搜索功能将查询结果呈现为HTML表格。为了使搜索结果页面尽可能易用和清晰,我打算只提供最相关的信息。然而,由于数据库中存储了许多更多的细节,我需要一种访问这些信息的方式。为了做到这一点,我希望每个表行都可以点击,以在页面底部或侧边的状态区域中显示所选记录的详细信息。

我可以尝试遵循运行其他功能的模式,即使用<a></a>标签和http.HandleFunc()来呈现新内容,但这并不完全符合我的要求,原因有几个。

首先:不需要离开搜索结果页面来查看其他详细信息;数据库中的详细信息并不多,一个记录的完整数据应该能够在与搜索结果相同的页面上呈现。

其次:我希望整个行都可以点击,而不仅仅是表格单元格中的文本,这是<a></a>标签所无法实现的。

使用从数据库返回的id作为属性,例如<div id="search-result-row-id-{{.ID}}"></div>,我可以处理单个记录,但我还没有找到一种在Go中捕获点击的方法。

在我开始编写JavaScript之前,有人知道是否有一种严格使用Go来实现这一点的方法吗?我并不特别反对使用经过验证的JavaScript方法,但我很好奇是否可以在不使用JavaScript的情况下实现。

英文:

I am writing a program for managing an inventory. It serves up html based on records from a postresql database, or writes to the database using html forms.

Different functions (adding records, searching, etc.) are accessible using &lt;a&gt;&lt;/a&gt; tags or form submits, which in turn call functions using http.HandleFunc(), functions then generate queries, parse results and render these to html templates.

The search function renders query results to an html table. To keep the search results page ideally usable and uncluttered I intent to provide only the most relevant information there. However, since there are many more details stored in the database, I need a way to access that information too. In order to do that I wanted to have each table row clickable, displaying the details of the selected record in a status area at the bottom or side of the page for instance.

I could try to follow the pattern that works for running the other functions, that is use &lt;a&gt;&lt;/a&gt; tags and http.HandleFunc() to render new content but this isn't exactly what I want for a couple of reasons.

First: There should be no need to navigate away from the search result page to view the additional details; there are not so many details that a single record's full data should not be able to be rendered on the same page as the search results.

Second: I want the whole row clickable, not merely the text within a table cell, which is what the &lt;a&gt;&lt;/a&gt; tags get me.

Using the id returned from the database in an attribute, as in &lt;div id=&quot;search-result-row-id-{{.ID}}&quot;&gt;&lt;/div&gt; I am able to work with individual records but I have yet to find a way to then capture a click in Go.

Before I run off and write this in javascript, does anyone know of a way to do this strictly in Go? I am not particularly adverse to using the tried-and-true js methods but I am curious to see if it could be done without it.

答案1

得分: 3

有人知道如何严格使用Go来实现这个吗?

正如其他人在评论中指出的,不,Go不能捕获浏览器中的事件。

为了实现这一点,您需要使用一些JavaScript将更多信息的Web请求发送到服务器(其中运行Go)。

您还可以在首次提供页面时将所有所需信息推送到浏览器,并根据CSS/JavaScript事件隐藏/显示它,但这只是常规的Web开发,与Go无关。

英文:

> does anyone know of a way to do this strictly in Go?

As others have indicated in the comments, no, Go cannot capture the event in the browser.

For that you will need to use some JavaScript to send to the server (where Go runs) the web request for more information.

You could also push all the required information to the browser when you first serve the page and hide/show it based on CSS/JavaScript event but again, that's just regular web development and nothing to do with Go.

huangapple
  • 本文由 发表于 2016年1月21日 03:29:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/34908835.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定