Yes, I Do Some Javascript Programming As Well

(before I get into this I wanted to let people know that I will be going to ZendCon to give my deployment talk. I might be looking for a place to crash for a second night, so let me know if you're going and can hook me up)

For a work project I had to add in some Ajax functionality for a scoreboard feature, where users will enter scores for periods / quarters / innings and then the total score has to update automatically on the screen before the user wishes to save that scoring document. So with some help from one of my online javascript ninja associates and Google searches, I got it all figured out, and thought I would share.

Here's a snapshot of the interface in question:
Sportso scoreboard interface

The first thing I did was apply the same class name to the away "innings" and home "innings" form fields, so that $().serialize would be able to pull up all those fields. That way, it wouldn't matter how many "inning" fields there might be. The tricky thing I found out was that I need to wrap my $('.homePeriods').change() call in it's own function() call so that it would actually be loaded when the page was loaded as well. I had no idea I needed to do that, and thanks to Marc for pointing it out. If there is a better method, let me know.

JAVASCRIPT:
  1. // Method used to automatically update total fields
  2. // when editing individual periods
  3. $(function(){
  4.     $('.homePeriods').change(function(){
  5.         var params = $('.homePeriods').serialize();
  6.         var pArray = params.split('&');
  7.         var homeTotal = 0;
  8.  
  9.         for (var i=0; i <pArray.length; i++) {
  10.             var temp = pArray[i].split('=');
  11.             var homePeriod = temp[1];
  12.             // Cast value to an integer with this little hack
  13.             homePeriod = homePeriod - 0;
  14.             homeTotal += homePeriod;
  15.         }
  16.  
  17.         $('#homeScoreTotal').replaceWith('<td class="period" id="homeScoreTotal"><input type="text" name="homeScoreTotal" value="' + homeTotal + '" maxlength="2" size="2" id="htotal" />');
  18.     });
  19.  
  20.     $('.awayPeriods').change(function(){
  21.         var params = $('.awayPeriods').serialize();
  22.         var pArray = params.split('&');
  23.         var awayTotal = 0;
  24.  
  25.         for (var i=0; i <pArray.length; i++) {
  26.             var temp = pArray[i].split('=');
  27.             var awayPeriod = temp[1];
  28.             awayPeriod = awayPeriod - 0;
  29.             awayTotal += awayPeriod;
  30.         }
  31.  
  32.         $('#awayScoreTotal').replaceWith('<td class="period" id="awayScoreTotal"><input type="text" name="awayScoreTotal" value="' + awayTotal + '" maxlength="2" size="2" id="vtotal" />');
  33.     });
  34. });

So, here's a small movie showing that in action.

I hope this helps out other people looking to add similar functionality to their site.

Article Tags >> || ||

What’s In Chris’ Brain: February 2008 Edition

I'm sitting in the Orlando airport at this ungodly hour waiting for a flight to Memphis as part of an Orlando -> Memphis -> Toronto trip back from my two days at the Newspaper Association of America conference. I was there with my employer to help start promoting the project I've been working on. It was an interesting show, and I had a great time in the booth hanging out and talking with Ken Zajac from The Sports Network and Digger Turnbull from Fantasy Sports Services. It was fun huddling around Digger's computer listening to the latest moves during the NHL's trading deadline day.

Anyway, enough geeking out about my sports fetish. Work took up a lot of my time this last 2 weeks getting ready for the demo, so my posting has suffered a bit. Here are some of the things I've been thinking about.:

  • more and more messages about using the CakePHP Auth component on the mailing list
  • I will be doing some Python work later this week on a utility to rip through some of our archived XML documents at work and do some search-and-replace ninja work.
  • Investing in using jQuery for work has made for some super-quick integration of some new features. It took me under half-an-hour from the first search in google to get an autocomplete plugin for jQuery working. Now *that* is rapid development
  • Despite my best efforts I can't get vim setup and working the way I want. Sorry Kevin, TextMate is just the best one for me, and Komodo when I need to break out the Xdebug guns
  • I'm going to put together a post (and contribute the same info to the CakePHP Cookbook) on my experiences with custom pagination usage with Cake, since I had to hack something together for recent changes I've been working on for the IBL website

Anyway, enough talking. It's way too early to do this sort of nonsense (can't believe I have coherent thoughts at 5:30AM) and it's close to boarding time. See you on the other side...

Article Tags >> || ||
Want to advertise on this blog? Send email to chartjes@littlehart.net
GTcars Canadian Car Audio TurboDodge Car For Sale Sign
Audi Forum Mustang Forum Dodge Intrepid Miata Turbo
GTscene Pontiac Bonneville


@TheKeyboard is Digg proof thanks to caching by WP Super Cache!