BC467Chapter 57Application: Transforming XML DataAssuming that the raw (Photoshop web design)
BC467Chapter 57Application: Transforming XML DataAssuming that the raw XML database stores only the sales forecast and actual dollar figures, it is up to analysis programs to perform their own calculations, such as how the actual salescompare against the forecasts. As you saw in the illustration of the rendered table, this appli- cation not only displays the percentage differences between the pairs of values, but it alsoprovides sorting facilities on those percentages. To speed the sorting, the percentages arecalculated as the JavaScript database is being accumulated, and the percentages are storedas properties of each object. Percentage calculation is called upon in two different statementsof the getOneSalesRep()function, so that the calculation is broken out to its own function, getPercentage(). In that function, the two passed values are massaged to calculate the per- centage value, and then the string result is formatted to no more than one digit to the right ofthe decimal (by way of a regular expression). The value returned for the property assignmentis converted to a number data type, because sorting on these values needs to be doneaccording to numeric sorting, rather than string sorting. You can already get a glimpse at the contribution JavaScript is making to the scripted represen- tation of the data transmitted in XML form. By virtue of planning for subsequent calculations, the JavaScript object contains considerably more information than was originally delivered, yetall the properties are derived from hard data supplied by the server database. Sorting the JavaScript databaseWith so many sorting keys for the user to choose from, it s no surprise that sorting codeoccupies a good number of script lines in this application. All sorting code consists of twomajor blocks: dispatchingand sorting. The dispatching portion is nothing more than one gigantic switchconstruction that sendsexecution to one of the 17 (!) sorting functions that match whichever sort key is chosen in theselectelement on the page. This dispatcher function, selectSort(), is also invoked fromthe init()function at load time. Thus, if the user makes a choice in the page, navigates toanother page, and then returns with the page still showing the previous selection, the onLoadevent handler will reconstruct the table precisely as it was. When sorting is completed, thetable is drawn, as you see shortly. function selectSort(chooser) { switch (chooser.value) { case byRep : db.sort(sortDBByRep); break; case byRegion : db.sort(sortDBByRegion); break; case byQ1Fcst : db.sort(sortDBByQ1Fcst); break; case byQ1Actual : db.sort(sortDBByQ1Actual); break; case byQ1Quota : db.sort(sortDBByQ1Quota); break; case byQ2Fcst : db.sort(sortDBByQ2Fcst); break; case byQ2Actual : db.sort(sortDBByQ2Actual); break;