Hi all, I’ve been developing a Javascript app, and I had it working properly but using old skool methods. Like hardcoding HTML attributes in my JS. So I’ve attempted to re-write it but it’s breaking I dont suppose someone could tell me where I’m going wrong. It’s got something to do with the sorting I’m sure of it.
[b]Original Working Source
[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, localeHref) {
this.localePrice = localePrice;
this.localeName = localeName;
this.localeGroup = localeGroup;
this.localeHref = localeHref;
}
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;
}
dynamicPricing.prototype.getLocaleHref = function() {
return this.localeHref;
}
dynamicPricing.prototype.setLocaleHref = function(localeHref) {
this.localeHref = localeHref;
}
function localeStore()
{
this.localeArr = new Array();
}
localeStore.prototype.addLocale = function(localeName, localePrice, localeGroup, localeHref)
{
this.localeArr[this.localeArr.length] = new dynamicPricing(localeName, localePrice, localeGroup, localeHref);
}
localeStore.prototype.getBookingsTable = function()
{
this.localeArr.sort(this.sortLocale)
var bookingsTableHTML = “<table border=1>”;
var currentLocale;
for (var i = 0; i < this.localeArr.length; i++)
{
if (this.localeArr[i].getLocaleName() != currentLocale)
{
if (currentLocale != “”) {
bookingsTableHTML += “</tr>”
bookingsTableHTML += “<tr>”
}
}
currentLocale = this.localeArr[i].getLocaleName();
bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocaleName();
bookingsTableHTML += “</td>”;
bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocalePrice();
bookingsTableHTML += “</td>”;
bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocaleGroup();
bookingsTableHTML += “</td>”;
bookingsTableHTML += “<td>”;
bookingsTableHTML += this.localeArr[i].getLocaleHref();
bookingsTableHTML += “</td>”;
}
bookingsTableHTML += “</table>”;
return bookingsTableHTML;
}
localeStore.prototype.sortLocale = function(a,b){
if (a.getLocaleGroup() < b.getLocaleGroup() /*&& a.getLocaleName() < b.getLocaleName()*/)
return -1
if (a.getLocaleGroup() > b.getLocaleGroup() /*&& a.getLocaleName() > b.getLocaleName()*/)
return 1
return 0
}
var pricingTable = new localeStore();
pricingTable.addLocale(“malaga”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“orlando”, “£199”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“london”, “£29”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“paris”, “£49”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“sydney”, “£299”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“las vegas”, “£599”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“tokyo”, “£199”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“rome”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“boston”, “£399”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“buenos aires”, “£999”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“manchester”, “£10”, “Europe”, “http://www.google.co.uk/”)
document.write(pricingTable.getBookingsTable());
</script>
</body>
[b]New Non-Working Code
[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>
<style type=”text/css”>
#dpTable {width: 150px; border-collapse: collapse;}
#destStyle {color: #666666; font-weight: bold; font-size: 9pt; font-family: Arial, Helvetica, sans-serif;}
#priceStyle {color: #ff0000; font-weight: bold; font-size: 9pt; font-family: Arial, Helvetica, sans-serif;}
</style>
<script type=”text/javascript”>
function dynamicPricing (localeName,localePrice, localeGroup, localeHref) {
this.localePrice = localePrice;
this.localeName = localeName;
this.localeGroup = localeGroup;
this.localeHref = localeHref;
}
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;
}
dynamicPricing.prototype.getLocaleHref = function() {
return this.localeHref;
}
dynamicPricing.prototype.setLocaleHref = function(localeHref) {
this.localeHref = localeHref;
}
function localeStore()
{
this.localeArr = new Array();
}
localeStore.prototype.addLocale = function(localeName, localePrice, localeGroup, localeHref)
{
this.localeArr[this.localeArr.length] = new dynamicPricing(localeName, localePrice, localeGroup, localeHref);
}
localeStore.prototype.getBookingsTable = function()
{
var getBody = document.getElementsByTagName(‘body’)[0];
var dpTbl = document.createElement(‘table’);
dpTbl.id = ‘dpTable’;
var dpTblBdy = document.createElement(‘tbody’);
this.localeArr.sort(this.sortLocale)
var currentLocale;
for (var i = 0; i < this.localeArr.length; i++)
{
if (this.localeArr[i].getLocaleName() != currentLocale)
{
if (currentLocale != “”) {
dpTblBdy.appendChild(dpRow);
var dpRow = document.createElement(‘tr’);
}
}
currentLocale = this.localeArr[i].getLocaleName();
var dpRow = document.createElement(‘tr’);
var dpCol1 = document.createElement(‘td’);
var dpCol1L = document.createElement(‘a’);
dpCol1.appendChild(dpCol1L);
dpCol1L.href = this.localeArr[i].getLocaleHref();
dpCol1L.innerHTML = this.localeArr[i].getLocaleName();
dpCol1L.id = ‘destStyle’;
dpRow.appendChild(dpCol1);
var dpCol2 = document.createElement(‘td’);
var dpCol2L = document.createElement(‘a’);
dpCol2.appendChild(dpCol2L);
dpCol2L.href = this.localeArr[i].getLocaleHref();
dpCol2L.innerHTML = this.localeArr[i].getLocalePrice();
dpCol2L.id = ‘priceStyle’;
dpRow.appendChild(dpCol2);
dpTblBdy.appendChild(dpRow);
}
dpTbl.appendChild(dpTblBdy);
getBody.appendChild(dpTbl);
}
localeStore.prototype.sortLocale = function(a,b){
if (a.getLocaleGroup() < b.getLocaleGroup() /*&& a.getLocaleName() < b.getLocaleName()*/)
return -1
if (a.getLocaleGroup() > b.getLocaleGroup() /*&& a.getLocaleName() > b.getLocaleName()*/)
return 1
return 0
}
var pricingTable = new localeStore();
pricingTable.addLocale(“malaga”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“orlando”, “£199”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“london”, “£29”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“paris”, “£49”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“sydney”, “£299”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“las vegas”, “£599”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“tokyo”, “£199”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“rome”, “£99”, “Europe”, “http://www.google.co.uk/”);
pricingTable.addLocale(“boston”, “£399”, “USA”, “http://www.google.co.uk/”);
pricingTable.addLocale(“buenos aires”, “£999”, “Worldwide”, “http://www.google.co.uk/”);
pricingTable.addLocale(“manchester”, “£10”, “Europe”, “http://www.google.co.uk/”)
window.onload = pricingTable.getBookingsTable();
</script>
</body>
Many Thanks!