This is example of sorting users JSON data based on user's input and showing them in clean way. Click here to know how search works

JSON Data

Find below Users info in JSON format
var userData = [{
   "id": "1",
   "name": "Srinivas Dasari",
   "place": "Chennai",
   "pic": "https://lh3.googleusercontent.com/-nBqk0jHahk8/VnL9EvT6IPI/AAAAAAAADdU/4JB3A-1zjTM/s160-Ic42/srinivas_dasari.jpg"
 }, {
   "id": "2",
   "name": "Srinivas Tamada",
   "place": "Atlanta",
   "pic": "https://lh3.googleusercontent.com/-lkebnHGHGcs/VnL9E4nO_FI/AAAAAAAADdY/z2IWGONMG8E/s160-Ic42/srinivas_tamada.jpg"
 }, {
   "id": "3",
   "name": "Sri Harsha",
   "place": "Hyderabad",
   "pic": "https://lh3.googleusercontent.com/-x69ytvh8GkA/VnL9Dqye1jI/AAAAAAAADdM/pBDbJRX8Vxo/s160-Ic42/harsha.jpg"
 }, {
   "id": "4",
   "name": "Lokesh Raghuram",
   "place": "London",
   "pic": "https://lh3.googleusercontent.com/-7XM-i20j7CQ/VnL9EZnDLYI/AAAAAAAADdQ/kR99DUgRhak/s160-Ic42/lokesh.jpg"
 }, {
   "id": "5",
   "name": "Bala Ganesh",
   "place": "Chennai",
   "pic": "https://lh3.googleusercontent.com/-snzuBGu0CJE/VnL9DksYVSI/AAAAAAAADdA/7Y0EvQbWc7I/s160-Ic42/ganesh.jpg"
 }, {
   "id": "6",
   "name": "Arun kumar",
   "place": "Visakhapatnam",
   "pic": "https://lh3.googleusercontent.com/-HiQ4bqPp-fk/VnL9Dqo7u0I/AAAAAAAADdc/NqhwXqgb4Ac/s160-Ic42/arun.jpg"
 }];

Markup

By using orderBy, we can sort JSON data. Observe below highlighted code
 <!-- Search text box with clear field -->
 <div class="slinput">
 <i class="fa fa-search left-icon"></i> 
 <input id="searchInput" onkeyup="searchUsers()"    placeholder="Search here" /> 
 <i class="fa fa-close right-icon"></i>
 </div>
 
 <!-- Radio buttons sort order property -->
 <div style="padding: 10px 10px;background: white;margin-bottom:5px;border:1px dotted #aaa">
    Sort By &nbsp
    <label class="radio-inline">
      <input type="radio" value="name" name="sortOrder" checked> name
    </label>
    <label class="radio-inline">
      <input type="radio" value="place" name="sortOrder"> place
    </label>
  </div>
  
 <!-- Users information will be in this div -->
 <div id="users">
 </div>

jQuery Code for Sorting

var prop = $("input[name=sortOrder]:checked").val();
var asc = true;
results = results.sort(function(a, b) {
     if (asc) return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
     else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
});

jQuery Code

// user's input for search
var searchVal = '';
var results = [];
$(function(){
      // if text box value is not null, then darken reset icon
      $(".slinput input").keyup(function(){
        var val = $(this).val();   
        if(val.length > 0) {
           $(this).parent().find(".right-icon").css('color','#555');
        } else {
          $(this).parent().find(".right-icon").css('color','#ccc');
        }
      });
      
      // if user click on reset icon, clear text field
      $(".slinput .right-icon").click(function(){
        $(this).parent().find("input").val('');
        $(this).css('color','#ccc');
        loadData(userData);
      });
      
      // change sort order, if user clicks on radio button  
      $("input[name=sortOrder]").change(function(){
          sortData();
      });
      
      results = userData;
      sortData();
});

// sort json data in ascending order based by property
function sortData() {
    var prop = $("input[name=sortOrder]:checked").val();
    var asc = true;
    results = results.sort(function(a, b) {
         if (asc) return (a[prop] > b[prop]) ? 1 : ((a[prop] < b[prop]) ? -1 : 0);
         else return (b[prop] > a[prop]) ? 1 : ((b[prop] < a[prop]) ? -1 : 0);
    });
    loadData(results);
}

// Displaying Information to Users
function loadData(data) {
    var htmlData = '';
    $.each(data, function(index, val){
        htmlData += '<div class="media user">'+
        '  <div class="media-left">'+
        '    <a href="#">'+
        '      <img class="media-object" src="'+val.pic+'" alt="...">'+
        '    </a>'+
        '  </div>'+
        '  <div class="media-body">'+
        '    <h4 class="media-heading">'+val.name+'</h4>'+
        '    '+val.place+
        '  </div>'+
        '</div>';
    });
    $("#users").html(htmlData);
}

// Search users data based input search keyword
function searchUsers() {
    var val = $("#searchInput").val();
    if(val == searchVal) {
        return; 
    } else {
        searchVal = val;
        results = [];
        $.each(userData, function(i, v) {
            if (v.name.toLowerCase().indexOf(val) != -1) {
                results.push(v);  
            }
        });
        sortData(); 
    }
}

0 comments:

Blogroll

Follow this blog by Email

Popular Posts