Using primary keys with grid data and jQuery Templates
-
09-10-2019 - |
سؤال
What is the best way to associate a primary key with a table row when using client-side templates, like jQuery Templates?
Should I do some kind of data binding or something? Or should I embed and hide the id in the table like this:
<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>
Isn't it bad to have my Ids accessible to the user, even if they are hidden? What other options do I have?
المحلول
Use an HTML5 custom data attribute, like so
<tr data-id="${id}">...</tr>
For more on HTML5 custom data attributes, see
- Do HTML5 custom data attributes “work” in IE 6?
- http://dev.w3.org/html5/spec/Overview.html#embedding-custom-non-visible-data-with-the-data-attributes
- 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