Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There's a technique called <strong>supplant</strong> that does this. I didn't write it, I think Douglas Crockford did. But I'll quote it:</p> <p>Now that your JavaScript program has received the data, what can it do with it? One of the simplest things is client-side HTML generation.</p> <pre><code>var template = '&lt;table border="{border}"&gt;&lt;tr&gt;&lt;th&gt;Last&lt;/th&gt;&lt;td&gt;{last}&lt;/td&gt;&lt;/tr&gt;' + '&lt;tr&gt;&lt;th&gt;First&lt;/th&gt;&lt;td&gt;{first}&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'; </code></pre> <p>Notice that we have an HTML template with three variables in it. Then we'll obtain a JSON object containing members that match the variables.</p> <pre><code>var data = { "first": "Carl", "last": "Hollywood", "border": 2 }; </code></pre> <p>We can then use a supplant method to fill in the template with the data.</p> <pre><code>mydiv.innerHTML = template.supplant(data); </code></pre> <p>JavaScript strings do not come with a supplant method, but that is ok because JavaScript allows us to augment the built-in types, giving them the features we need.</p> <pre><code>String.prototype.supplant = function (o) { return this.replace(/{([^{}]*)}/g, function (a, b) { var r = o[b]; return typeof r === 'string' ? r : a; } ); }; </code></pre> <p>From <a href="http://www.json.org/fatfree.html" rel="noreferrer">http://www.json.org/fatfree.html</a></p> <p>One technique I've seen to avoid that messy variable assignment of a template is to do:</p> <pre><code>&lt;script id="rowTemplate" type="text/html"&gt; &lt;tr&gt; &lt;td&gt;Field A:&lt;/td&gt; &lt;td&gt;&lt;input type='text' name='fielda[{id}]'&gt;&lt;/td&gt; &lt;td&gt;Field B:&lt;/td&gt; &lt;td&gt;&lt;textarea name='fieldb[{id}]'&gt;&lt;/textarea&gt;&lt;/td&gt; &lt;/tr&gt; &lt;/script&gt; </code></pre> <p>This will let your write much prettier code as you can reference it with getElementById.</p> <p>So in your case, rather than populating it with a JSON object, you would just look over however many rows you need to add and replace the id's.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload