使用带有网格数据和jQuery模板的主键
-
09-10-2019 - |
题
当使用客户端模板(如jQuery模板)时,将主键与表行相连的最佳方法是什么?
我应该做某种数据绑定吗?或者我应该像这样嵌入并隐藏ID:
<script id="contactsTemplate" type="text/x-jquery-tmpl">
<table class="contacts">
<thead><tr><td class="ui-helper-hidden">Id</td><td>Name</td><td>City</td><td>State</td></tr></thead>
<tbody>
{{each contact}}
{{tmpl($value) '#contactTemplate'}}
{{/each}}
</tbody>
</table>
</script>
<script id="contactTemplate" type="text/x-jquery-tmpl">
<tr><td class="ui-helper-hidden">${id}</td><td>${name}</td><td>${city}</td><td>${state}</td></tr>
</script>
即使隐藏了我的ID,也可以让我的ID访问吗?我还有什么其他选择?
解决方案
使用HTML5自定义数据属性,
<tr data-id="${id}">...</tr>
有关HTML5自定义数据属性的更多信息,请参见
- HTML5自定义数据属性是否在IE 6中“工作”?
- http://dev.w3.org/html5/spec/overview.html#embedding-custom-non-visible-data-with-with-the-data-tributes
- http://ejohn.org/blog/html-5-data-attributes/
- http://html5doctor.com/html5-custom-data-attributes/
- http://www.marcofolio.net/webdesign/html5_data-_attributes_are_great_and_you_know_it.html
不隶属于 StackOverflow