0

Here's my code

<script type="text/html" id='usageList'>
            <table cellspacing='0' cellpadding='0' border='1' >
                <thead>
                    <tr>
                        <th>Id</th>
                        <th>Name</th>
                        <th>LastName</th>
                    </tr>
                </thead>

                <tbody>
                    /* I want to include data source via ajax or some mechanism here */
                    <%_.each(items,function(item,key,list){ %>
                    <% var f = item.name; %><!-- create more variables -->
                    <% var l= item.lastName; %>
                    <tr>
                        <!-- use variables -->
                        <td><%= key %></td>
                        <td class="<%= f %>"><%= f %></td>
                        <td class="<%= l %>"><%= l %></td>
                    </tr>
                    <% }); %>
                </tbody>
            </table>
        </script>

1 Answer 1

1

First, get the template into a variable

var template = ​$(​'#usageList')​​​​​​​​​​​​​​.html();

Then, make an Ajax request. Using the _.template process the data received and add it to a DOM node

$.getJSON('ajax/test.json', function(data) {
    $('#output').html(_.template(template,{items:data}));
});

Data returned must follow this format

[
    {'name':'john', 'lastName':'M'},
    {'name':'jean', 'lastName':'N'},
    {'name':'carl', 'lastName':'K'},
    {'name':'peter', 'lastName':'B'}
]

Check this JSFiddle: http://jsfiddle.net/jaimem/2gPYZ/

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.