How to create unique IDs when ranging over data in Golang and use in Javascript

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

How to create unique IDs when ranging over data in Golang and use in Javascript

问题

我正在从服务器获取数据,并对其进行迭代以创建一个表格,然后我使用一个表单来使用JavaScript将ID存储到本地存储中。以下是代码片段:

<table>
	<tr><th>产品ID</th></tr>
	{{range .}}
	<td><form onsubmit="save_data()" action="/" method="get"><button class="btn btn-info pid" id="pid" name="{{.puid}}" value="{{.puid}}">更新</button></form></td>
	{{end}}
<table>

<script>

function save_data() {
  var input = document.getElementById("pid");
  localStorage.setItem("id", input.value); 

}	
			
</script>

然而,无论我点击哪个表格行的“更新”按钮,每次都只会存储第一个表格行元素的ID。是否有办法在遍历数据时生成唯一的ID,并在JavaScript中引用它。谢谢。

英文:

I am getting data from the server and iterating over it to create a table and then I am using a form to store an id to local storage using javascript. Here is code snippet

&lt;table&gt;
	&lt;tr&gt;&lt;th&gt;Product ID&lt;/th&gt;&lt;/tr&gt;
	{{range .}}
	&lt;td &gt;&lt;form onsubmit=&quot;save_data()&quot; action=&quot;/&quot; method=&quot;get&quot;&gt;&lt;button class=&quot;btn btn-info pid&quot; id=&quot;pid&quot; name=&quot;{{.puid}}&quot; value=&quot;{{.puid}}&quot;&gt;Update&lt;/button&gt;&lt;/form&gt;&lt;/td&gt;
	{{end}}
&lt;table&gt;

&lt;script&gt;

function save_data() {
  var input = document.getElementByID(&quot;pid&quot;);
  localStorage.setItem(&quot;id&quot;, input.value); 

}	
			
&lt;/script&gt;

However every time, no matter of which table row's "update" button I click, everytime only the ID of the first table row element is getting stored.
Is there a way I can generate unique IDs and reference it in Javascript when ranging over the data.
Thanks

答案1

得分: 1

在循环中,你有以下代码:

<button class="..." id="pid" name="{{.puid}}" value="{{.puid}}">Update</button>

这意味着所有的按钮都有相同值为pidid属性。这是一个错误,因为id在文档中必须是唯一的。当你调用

document.getElementById("pid");

时,将返回匹配id="pid"的第一个元素。这就解释了为什么"只有第一个表格行元素的ID被存储"。

为了为每一行创建唯一的id,你可以使用以下代码:

{{range $index, $value := .}}
...<button class="..." id="pid{{$index}}" name="{{$value.puid}}" value="{{$value.puid}}">Update</button>...
{{end}}

但是,当save_data()事件触发时,你如何知道哪个表单被提交了呢?为了解决这个问题,你可以将当前表单或行的id作为参数发送,例如:

{{range $index, $value := .}}
<td><form onsubmit="save_data(this, {{$index}})" action="/" method="get">...</form></td>
{{end}}
function save_data(form, rowno) {
  var input = document.getElementById("pid"+rowno);
  localStorage.setItem("id", input.value); 
}

以上是翻译好的内容,请确认是否满意。

英文:

In the loop You have

&lt;button class=&quot;...&quot; id=&quot;pid&quot; name=&quot;{{.puid}}&quot; value=&quot;{{.puid}}&quot;&gt;Update&lt;/button&gt;

which means that all buttons have id attribute whith the same value pid. This is an bug as id-s must be unique in the document. And when you call

document.getElementById(&quot;pid&quot;);

the first element matching the id=&quot;pid&quot; is returned. That explains why "only the ID of the first table row element is getting stored".

To create unique id for each row you could use something like

{{range $index, $value := .}}
...&lt;button class=&quot;...&quot; id=&quot;pid{{$index}}&quot; name=&quot;{{$value.puid}}&quot; value=&quot;{{$value.puid}}&quot;&gt;Update&lt;/button&gt;...
{{end}}

but then you have a problem how to know which form was submitted when your save_data() event fires. To solve this you could send the current form or row id as a parameter, something like

{{range $index, $value := .}}
&lt;td&gt;&lt;form onsubmit=&quot;save_data(this, {{$index}})&quot; action=&quot;/&quot; method=&quot;get&quot;&gt;...&lt;/form&gt;&lt;/td&gt;
{{end}}

function save_data(form, rowno) {
  var input = document.getElementById(&quot;pid&quot;+rowno);
  localStorage.setItem(&quot;id&quot;, input.value); 
}  

huangapple
  • 本文由 发表于 2017年3月7日 15:17:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/42642410.html
匿名

发表评论

匿名网友

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

确定