SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Advanced Help! Object Class Programming! I'm stuck!

    Hi all, I'm new to "advance" javascript and I need some help I dont mind if it's not written for me but a point in the right direction.

    I'm building an app, that takes the contents of an Array (which will eventually be dynamic) and printing the array out via a specific formation.

    I've written pretty much the shell of program, although I'll be re-writing the output to be done via DOM. ANYWAYS...

    I've hit a brick wall due to two problems:
    1) The first is how can I sort the array, as no matter where I put cinema.sort() in my app it crashes it. I want it sorted by localeGroup.
    2) How can I dynamically put in a new <tr><td></td></tr> tag at the top and bottom of each new iteration of a localeGroup, BUT ONLY THE FIRST INSTANCE OF new localeGroup.

    I.E. - Currently it outputs the array as it comes in nothing more, but I need to put in a <tr><td></td></tr> structure at the start of a new set of data. When it loops through the array when it sees a new localeGroup it'll put in a <tr><td></td></tr> and another <tr><td></td></tr> at the last element in that localeGroup.

    current output:
    [ malaga ][ 99 ][ Europe ]
    [ orlando ][ 99 ][ USA ]
    [ london ][ 99 ][ Europe ]

    what i need:
    [ NEW <tr><td></td></tr> ] <-- Element new localeGroup
    [ malaga ][ 99 ][ Europe ]
    [ london ][ 99 ][ Europe ]
    [ NEW <tr><td></td></tr> ] <-- Element last localeGroup

    [ NEW <tr><td></td></tr> ] <-- Element new localeGroup
    [ orlando ][ 99 ][ USA ]
    [ NEW <tr><td></td></tr> ] <-- Element last localeGroup

    Cheers for any help you give me, hopefully I've explained myself correctly. I'm having trouble taking my Javascript skills to the next level. Hopefully someone will make my brain click

    heres WORKING source for so far:
    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <body>
    	<h2>Prices Array Printing/Sorting</h2>
    		<script type="text/javascript">
    			function dynamicPricing (localeName,localePrice, localeGroup) {
    				this.localePrice = localePrice;
    				this.localeName = localeName;
    				this.localeGroup = localeGroup;
    			}
    			
    			dynamicPricing.prototype.getLocalePrice = function() {
    				return this.localePrice;
    			}
    			
    			dynamicPricing.prototype.setLocalePrice = function(localePrice) {
    				this.localePrice = localePrice;
    			}
    			
    			dynamicPricing.prototype.getLocaleGroup = function() {
    				return this.localeGroup;
    			}
    			
    			dynamicPricing.prototype.setLocaleGroup = function(localeGroup) {
    				this.film = localeGroup;
    			}
    			
    			dynamicPricing.prototype.getLocaleName = function() {
    				return this.localeName;
    			}
    			
    			dynamicPricing.prototype.setLocaleName = function(localeName) {
    				this.localeName = localeName;
    			}
    			
    			function cinema()
    			{
    				this.bookings = new Array();
    			}
    			
    			cinema.prototype.addLocale = function(localeName, localePrice, localeGroup)
    			{
    				this.bookings[localeName] = new dynamicPricing(localeName, localePrice, localeGroup);
    			}
    			
    			cinema.prototype.getBookingsTable = function()
    			{
    				var booking;
    					var bookingsTableHTML = "<table border=1>";
    					
    						for (booking in this.bookings)
    							{
    								bookingsTableHTML += "<tr><td>";
    								bookingsTableHTML += this.bookings[booking].getLocaleName();
    								bookingsTableHTML += "</td>";
    								
    								bookingsTableHTML += "<td>";
    								bookingsTableHTML += this.bookings[booking].getLocalePrice();
    								bookingsTableHTML += "</td>";
    								
    								bookingsTableHTML += "<td>";
    								bookingsTableHTML += this.bookings[booking].getLocaleGroup();
    								bookingsTableHTML += "</td>";							
    								
    								bookingsTableHTML += "</tr>"							
    																														
    							}
    					bookingsTableHTML += "</table>";
    					return bookingsTableHTML;
    			}
    			
    			var pricingTable = new cinema();
    			pricingTable.addLocale("malaga", "99", "Europe");
    			pricingTable.addLocale("orlando", "99", "USA");			
    			pricingTable.addLocale("london", "99", "Europe");
    			
    			document.write(pricingTable.getBookingsTable());										
    		</script>
    	</body>
    </html>

  2. #2
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Your "array" of bookings isn't technically an array - it's an object; so the sort method won't work when you've got those name/value pairs in there. My suggestion is to change the way you create the bookings array:
    Code:
    this.bookings[this.bookings.length] = {name:value, name:value}; //you get the idea
    What you end up with is a sortable array full of objects. When you "sort", you use a function with the .sort array method to use the objects within the array:
    Code:
    this.bookings.sort(function(a,b) {
      return return ((a.locale + "") < (b.locale + "")) ? -1 : ((a.locale + "") > (b.locale + "")) ? 1 : 0;
    });
    I've generalized here, but I hope this points you in the right direction.

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    24
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    cheers for your help!


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •