Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would handle this problem differently.</p> <p>I would assign a unique ID only to each "block" of data. Within that block I would only use classes to identify each element of the data. </p> <p>Each class within one block would be unique.</p> <p>So this is what my HTML would look like (note the class rename to make them unique within each data block)</p> <pre><code>&lt;div id="data1" class="RedColor" &gt; &lt;table class="recordTable"&gt; &lt;tr&gt; &lt;td class="studentNameData"&gt; &lt;table class="innerTab" border="0" width="100%" cellpadding="3"&gt; &lt;tr&gt; &lt;td class="studentName"&gt; ... &lt;/td&gt; ... &lt;/table&gt; &lt;/td&gt; &lt;/tr&gt; &lt;/table&gt; &lt;/div&gt; &lt;div id="data2" class="RedColor" &gt; &lt;table class="recordTable"&gt; ... </code></pre> <p>You could even get rid of the <code>div</code> wrapper and put the unique ID on each outer table.</p> <p>Now each piece of data can be uniquely identified using the form: <code>[ Data Block ID ] [ Class Name ]</code>.</p> <p>In jQuery that can be done using the <strong><a href="http://api.jquery.com/find/" rel="nofollow noreferrer"><code>.find()</code></a></strong> method.</p> <p>For example to retrieve the studentName from data block 3 I would use</p> <pre><code> $("#data3").find(".studentName").text(); </code></pre> <p>I would iterate over the data you get from the server to populate the tables. Here is an extremely simplified version just to illustrate what I described. I removed the outer DIV wrapper and added the unique ID to the table, just so you can see what that would look like: <br/></p> <h2>Simplified example:</h2> <pre><code>// Function to add an empty table with class names var $addTable = function() { // The table could be built with multiple methods too. return $('&lt;table&gt;&lt;tr&gt;&lt;td class ="name"&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;'); } $(function() { // The attribute = selector $("input[value='Get data']").click(function() { // Loading notice $("#showData").html("Loading..."); // The AJAX call to get the data using POST $.post('/ajax_json_echo/', function(data) { // Clear loading notice $("#showData").html(""); // Iterate through data // and create a table for each item. $.each(data, function(index, value) { // This is the div's unique ID var $id = "data" + index; $addTable().attr("id", $id).appendTo("#showData"); $("#" + $id).find(".name").html(value); }); }); }); });​ </code></pre> <h2><strong><a href="http://jsfiddle.net/Q9yHD/" rel="nofollow noreferrer">jsFiddle example</a></strong></h2>
 

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