WEB need to sort json data via jquery

Discussion in 'OT Technology' started by biawokauns, Oct 4, 2008.

  1. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    hollar back.
     
  2. biawokauns

    biawokauns New Member

    Joined:
    Sep 18, 2001
    Messages:
    19,893
    Likes Received:
    0
    Location:
    Republic of Kalifornia
    if anybody cares

    Code:
    // {'frequent':[
    // 				{'departingCity':'Boston', 
    // 				'arrivingCity':'Los Angeles',
    // 				'departingCityCode':'BOS',
    // 				'arrivingCityCode':'LAX',
    // 				'departingDate':'08/02/2008',
    // 				'arriveDate':'08/15/2008'},
    // 
    // 				{'departingCity':'X--Seattle', 
    // 				'arrivingCity':'X--Los Angeles',
    // 				'departingCityCode':'SEA',
    // 				'arrivingCityCode':'LAX',
    // 				'departingDate':'06/02/2008',
    // 				'arriveDate':'06/15/2008'},
    // 
    // 
    // 				{'departingCity':'Seattle', 
    // 				'arrivingCity':'Los Angeles',
    // 				'departingCityCode':'SEA',
    // 				'arrivingCityCode':'LAX',
    // 				'departingDate':'01/19/2008',
    // 				'arriveDate':'01/25/2008'}
    // 				]
    // }
    // 
    
    
    // good to go?
    $(document).ready(function(){
    
    	//
    	// // original code
    	//
    	// departureTmp = Date.fromString(item.departingDate);
    	// departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+item.departingDate.substring(3,5);
    	// 
    	// arriveTmp = Date.fromString(item.arriveDate);
    	// arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+item.departingDate.substring(3,5)+' '+item.departingDate.substring(6,10);
    	// 
    	// 
    	// $("ul#search-history").append('<li><a href="#">'+item.departingCity+' - '+item.arrivingCity+'</a><br/>'
    	// +item.departingCityCode+' - '+item.arrivingCityCode+'<br />'
    	// +departingDate+' - '+arriveDate+'<br/>'
    	// +'</li>');
    	
    
    	// get JSON object
    	$.getJSON("./ajax/trips.js", function(json){		
    		
    		// history
    		var searchHistory = new Array();	
    		$.each(json.history,function(i,item) {
    
    			// build the array so it can be sorted
    			// convert date to sortable #
    			parsedDate = Date.parse(item.dateSearched);
    			
    			searchHistory[i] =	new Array(parsedDate,
    				new Array(item.departingCity),
    				new Array(item.arrivingCity), 
    				new Array(item.arrivingCityCode),
    				new Array(item.departingCityCode),								
    				new Array(item.departingDate),							
    				new Array(item.arriveDate)								
    				);
    		});	
    
    		searchHistory = searchHistory.sort();
    		
    		// generate LIs	
    		for (count=0; count<3; count++)
    		{
    			var departingCity		= searchHistory[count][1];
    			var arrivingCity		= searchHistory[count][2];
    			var arrivingCityCode	= searchHistory[count][3];		
    			var departingCityCode	= searchHistory[count][4];		
    			var departingDate		= searchHistory[count][5][0];		
    			var arriveDate			= searchHistory[count][6][0];		
    			
    
    			departureTmp = Date.fromString(departingDate);			
    			departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+departingDate.substring(3,5);
    
    			arriveTmp = Date.fromString(arriveDate);
    			arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+arriveDate.substring(3,5)+' '+searchHistory[count][5][0].substring(6,10);	
    		
    			$("ul#search-history").append('<li><a href="#">'+departingCity+' - '+arrivingCity+'</a><br/>'
    			+departingCityCode+' - '+arrivingCityCode+'<br />'
    			+departingDate+' - '+arriveDate+'<br/>'
    			+'</li>');
    			
    		}
    
     
    
    
    		// upcoming trips
    		var upcomingTrips = new Array();	
    		$.each(json.upcoming,function(i,item) {
    
    			// build the array so it can be sorted
    			
    			// convert date to sortable #
    			parsedDate = Date.parse(item.departingDate);
    			
    			upcomingTrips[i] =	new Array(parsedDate,
    				new Array(item.departingCity),
    				new Array(item.arrivingCity), 
    				new Array(item.arrivingCityCode),
    				new Array(item.departingCityCode),								
    				new Array(item.departingDate),							
    				new Array(item.arriveDate)								
    				);
    		});	
    		
    		// sort, then invert
    		upcomingTrips = upcomingTrips.sort();
    		upcomingTrips = upcomingTrips.reverse();
    		
    		// generate LIs	
    		for (count=0; count<3; count++)
    		{
    			var departingCity		= upcomingTrips[count][1];
    			var arrivingCity		= upcomingTrips[count][2];
    			var arrivingCityCode	= upcomingTrips[count][3];		
    			var departingCityCode	= upcomingTrips[count][4];		
    			var departingDate		= upcomingTrips[count][5][0];		
    			var arriveDate			= upcomingTrips[count][6][0];		
    			
    
    			departureTmp = Date.fromString(departingDate);			
    			departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+departingDate.substring(3,5);
    
    			arriveTmp = Date.fromString(arriveDate);
    			arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+arriveDate.substring(3,5)+' '+upcomingTrips[count][5][0].substring(6,10);	
    			
    			$("ul#upcoming-trips").append('<li><a href="#">'+departingCity+' - '+arrivingCity+'</a><br/>'
    			+departingCityCode+' - '+arrivingCityCode+'<br />'
    			+departingDate+' - '+arriveDate+'<br/>'
    			+'</li>');
    		}
    
    
    
    		
    		// frequent trips
    		var frequentCities = new Array();	
    		$.each(json.frequent,function(i,item) {
    
    			// build the array so it can be sorted
    			frequentCities[i] =	new Array(item.departingCity+i,
    				new Array(item.departingCity),
    				new Array(item.arrivingCity), 
    				new Array(item.arrivingCityCode),
    				new Array(item.departingCityCode),								
    				new Array(item.departingDate),							
    				new Array(item.arriveDate)								
    				);
    		});	
    
    		frequentCities.sort();
    		
    		// generate LIs	
    		for (count=0; count<3; count++)
    		{
    			var departingCity		= frequentCities[count][1];
    			var arrivingCity		= frequentCities[count][2];
    			var arrivingCityCode	= frequentCities[count][3];		
    			var departingCityCode	= frequentCities[count][4];		
    			var departingDate		= frequentCities[count][5][0];		
    			var arriveDate			= frequentCities[count][6][0];	
    			
    			departureTmp = Date.fromString(departingDate);			
    			departingDate = departureTmp.getDayName(true)+', '+departureTmp.getMonthName(true)+' '+departingDate.substring(3,5);
    
    			arriveTmp = Date.fromString(arriveDate);
    			arriveDate = arriveTmp.getDayName(true)+', '+arriveTmp.getMonthName(true)+' '+arriveDate.substring(3,5)+' '+frequentCities[count][5][0].substring(6,10);
    			
    			
    			$("ul#frequent-trips").append('<li><a href="#">'+departingCity+' - '+arrivingCity+'</a><br/>'
    			+departingCityCode+' - '+arrivingCityCode+'<br />'
    			+departingDate+' - '+arriveDate+'<br/>'
    			+'</li>');
    		}
    
    		// when jquery inserts the above LIs, the rounded corners don't appear on the footer. let's insert them
    		// after we've create the lists
    		$("#saved-searches-dropdown span.tr").after('<span class="br corner"></span><span class="bl corner"></span>');
    	
    	});
    	
    
    	// saved search link functionality 
        $("div#saved-searches a").click(function () {
          if ($("div#saved-searches-dropdown").is(":hidden")) {
            $("div#saved-searches-dropdown").fadeIn("fast");
          } else {
            $("div#saved-searches-dropdown").fadeOut("fast");
          }
        });
    
    });
    
    
    
     

Share This Page