2

So I want to <br/> if value is array otherwise just display the value. I guess I can write a function to do this but wondering if there's a better way to do this with jquery template?

<script id="template" type="x-jquery-tmpl"> <table>
  <tr>
    <td>${name}</td>
    <td>{{each value}}${$value}<br/>{{/each}}</td>
  </tr> </table>
</script>

<script>
      var data = [
                    {
                        name: "blah",
                        value: ["1", "2", "3"]
                    },
                    {
                        name: "blah blah",
                        value : "abc"
                    }
                ];

$('#template').tmpl(data).appendTo('#target);
    </script>

<div id="target">

</div>

1 Answer 1

5

Something like this should work:

<script id="template" type="x-jquery-tmpl"> <table>
  <tr>
    <td>${name}</td>
    {{if typeof value == 'array'}}
        <td>{{each value}}${$value}<br/>{{/each}}</td>
    {{/if}}
    {{else}}
        <td>${value}<br/></td>
    {{/else}}
  </tr> </table>
</script>
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.