I'm using jquery templates to populate a table.
I'm wondering if it's possible to access the data used to populate the table after the rendering is complete? (I want to access it when the user have selected something in a select
).
Or do I have to manually store that data during rendering? (i.e. store it on the tr
using jquery.data()
)?
The tmplItem functionality of the jQuery Templates plugin will help you do this: http://api.jquery.com/tmplitem/
you can do something like:
var myTmplItem = $("#target").tmplItem();
Then access it like:
myTmplItem.data
data will be set to whatever the context was of the entire template that was being rendered.
Here is a sample: http://jsfiddle.net/rniemeyer/tLnwx/
I would also put a plug in for KnockoutJS, it allows this type of connection between elements and your underlying data to happen automatically via declarative bindings added to your elements. It supports jQuery Templates as a template engine.