Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Whenever, you encounter this kind of situation where you have to render something based on data changes, please consider decoupling between data and view layer. Because your view have to change as per data changes. You can implement this kind of logic on your own or you can use framework like Angular.js, Backbone.js, Ember.js etc. which tries to solve this kind of problem. Well in your case there are several ways to solve your problem but like I said earlier decoupling would be better (I think). You can do something like</p> <p>Modified HTML (for easy data extraction). You can keep your original HTML but logic to pull data have to be changed.</p> <pre><code>Sort data by: &lt;a href="#"&gt;Date&lt;/a&gt;&amp;nbsp;&lt;a href="#"&gt;Name&lt;/a&gt; &lt;br /&gt; &lt;br /&gt; &lt;form id="live-search" action="" class="styled" method="post"&gt; &lt;fieldset&gt; &lt;input type="text" class="text-input" id="filter" value="Date" /&gt; &lt;br /&gt; &lt;input type="text" class="text-input" id="filter2" value="Name" /&gt; &lt;br /&gt; &lt;span id="filter-count"&gt;&lt;/span&gt; &lt;/fieldset&gt; &lt;/form&gt; &lt;ul class="records2" id="records"&gt; &lt;div data-record='{"year": "2012","name":"Joe"}'&gt; &lt;li&gt;2012&lt;/li&gt; &lt;li&gt;Joe&lt;/li&gt; &lt;/div&gt; &lt;br /&gt; &lt;div data-record='{"year": "2013","name":"Dave"}'&gt; &lt;li&gt;2013&lt;/li&gt; &lt;li&gt;Dave&lt;/li&gt; &lt;/div&gt; &lt;br /&gt; &lt;div data-record='{"year":"2013","name":"Adam Brown"}'&gt; &lt;li&gt;2013&lt;/li&gt; &lt;li&gt;Adam Brown&lt;/li&gt; &lt;/div&gt; </code></pre> <p></p> <pre><code>function sortArray(arr, prop, asc) { arr = arr.sort(function(a, b) { if (asc) { return (a[prop] &gt; b[prop]); } else { return (b[prop] &gt; a[prop]); } }); return arr; } $(document).ready(function () { var recordDiv = $('#records'), dataDivs = $('#records div'), data = [], sortLink = $('a'); for (var i = 0, len = dataDivs.length; i &lt; len; i++) { var t = $(dataDivs[i]).attr('data-record'); t = $.parseJSON(t); data.push(t); } sortLink.click(function() { var sortBy = this.innerHTML.toLowerCase(); data = sortArray(data, sortBy, true); //Now render the element inside #record DIV as per sorted data return false; }); }); </code></pre> <p>Let us know how it goes. :)</p>
    singulars
    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. VO
      singulars
      1. This table or related slice is empty.
    2. 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