just learned JQuery, ran into problems...
I have a SQL database that contains a stored procedure that'll output data to a XML file every 5 minutes. The data outputted is always changing, while the format always is same.
I wanted a page that'd use AJAX to pull the data from the server, check for existing data that was already displayed on the page, if so, update the data. If the data no longer existed in the XML, the data would be removed from the page. If there was new data in the XML but wasn't displayed in the page, it'd be added to the page. Keep in mind the page is on a site that requires HTTP authentication.
A friend recommended using JQuery to simplify the task.
I wrote the code, and guess what? It works. More and less so anyway. It works in IE, but breaks in any other browser. I've been plugging away at it for the last few days trying to figure out why it won't work in any other browser, and am about at my wit's end.
Perhaps you guys can catch the problem that's been eluding me. If you can't catch it, no worries-- I can simply tell my users to just use IE to load the page.
Without further ado, here's the code:
- function updateQueue(){
- // Pull queue data from server, send to queue processor. Disable caching to avoid IE caching issue.
- var filtertype = document.getElementById('filtertype').value; // Find out what filter we're using. usually 'all', meaning no filter
- dump ('Using filter type: ' + filtertype);
- dump ('Now in updateQueue function...');
- $.ajax({
- type: "POST",
- url: "xxxx" + filtertype,
- cache: false,
- username: "xxxx",
- password: "xxxx",
- success: function(data) {
- dump('Successfully pulled data from server');
- processQueue(data);
- }
- //error: function(data) {
- // alert('Dang, it broke. Error: ' + data);
- //}
- });
- }
- function dump(dumpwhat){
- if(enabledebugger == true){
- document.getElementById('debugthis').value += dumpwhat + '\n';
- }
- }
- function processQueue(data){
- // Convert the XML into a javascript array
- var r = 0;
- var player = new Array();
- dump('Now in processQueue function...');
- dump('Converting XML into an array...');
- $(data).find('row').each(function() {
- var row = $(this);
- if(row.length > 0) {
- player[r] = {
- 'SessionID' : row.attr("SessionID"),
- 'Source' : row.attr("Source"),
- 'Region' : row.attr("Region"),
- 'QueuedTime' : row.attr("QueuedTime"),
- 'Desc' : row.attr("Desc"),
- 'Game' : row.attr("Game"),
- 'QueueType' : row.attr("QueueType"),
- 'Device' : row.attr("Device"),
- 'ANI' : row.attr("ANI")
- };
- dump(r + ': ' + player[r]['SessionID']);
- if(!player[r]['Source']) player[r]['Source'] = '';
- if(!player[r]['ANI']) player[r]['ANI'] = '';
- }
- r++;
- });
- dump('Done converting XML into an array.');
- //return;
-
- // Convert queue table into a javascript array
- dump('Converting HTML table into an array...');
- var myTable = [];
- $('#sortabletable tr').each(function(i, tr) {
- var myTr = [];
- $('td', tr).each(function(i, td) {
- myTr.push($(td).html());
- });
- myTable.push(myTr);
- });
- dump('Done converting HTML table into an array.');
-
- // Now check each row, get Session ID
- var SessionIDtoCheckFor=0;
- var i=0;
- var j=0;
- var uBoundOfMyTable = myTable.length - 1;
- var uBoundOfplayer = player.length;
- var SessionIDFound = false;
- var SessionIDIndex=0;
-
- dump('Upper limit of table array: ' + uBoundOfMyTable);
- dump('Upper limit of player (XML) array: ' + uBoundOfplayer);
- dump('Now at the section for processing already displayed sessions...');
- for (i=1;i<=uBoundOfMyTable;i++) { //start at 1, to skip table header row
- SessionIDtoCheckFor = myTable[i][0]; // Grab the session ID
- dump('Session to process: ' + SessionIDtoCheckFor);
- dump('Checking player array for session...');
- // Search XML array for session id
- for (j=0;j<uBoundOfplayer;j++) {
- if(uBoundOfplayer == 0 || j > uBoundOfplayer) break;
- if(player[j]['SessionID'] == SessionIDtoCheckFor){
- SessionIDFound = true; // Session ID has been found, exit loop.
- SessionIDIndex = j; // Store index location for later use
- dump('matching session found, it exists!');
- break;
- }
- }
- if(SessionIDFound){
- // Update QueuedTime (works!)
- $('#sortabletable tr').each(function() {
- var playerIdcellvalue = $(this).find("#SessionID").html();
- if(playerIdcellvalue == SessionIDtoCheckFor) {
- // Found the row with the cell
- var QueuedTimecell = $(this).find("#QueuedTime"); // get handle for the cell containing the corresponding cell
- $(QueuedTimecell).html(player[SessionIDIndex]['QueuedTime']); // Update cell with current queued time
- dump('player session in table updated with current Queued Time');
- }
- });
- } else {
- dump('Session was not found in player array, session expired.');
- dump('Searching for handle on expired session in HTML table...');
- // Delete row from queue table via fade out (works!)
- $('#sortabletable tr').each(function() {
- var playerIdcellvalue = $(this).find("#SessionID").text(); // get handle for the cell containing the corresponding id
- if(playerIdcellvalue == SessionIDtoCheckFor) {
- dump('Found handle!');
- //dump('found expired, deleting it...');
- // Found the row with the expired session
- $(this).fadeOut(500, function() { $(this).remove(); }); // Fade out the row, then remove it
- dump('Expired session ' + playerIdcellvalue + ' has been removed from table.');
- }
- });
- }
- // Move on to next session
- dump('Moving on to the next session...');
- }
-
-
-
- // Add new pending sessions (works in IE, but doesn't work in other browsers!)
- dump('Now at session add section...');
- if(player.length > 0) {
- dump('player.length > 0 confirmed...');
- dump('Now in for loop. ');
- for (j=0; j < player.length; j++) {
- SessionIDFound = false;
-
- dump('j value: ' + j + ' ... player length value: ' + player.length);
-
- SessionIDtoCheckFor = player[j]['SessionID'];
-
- dump('checking to see if session ' + SessionIDtoCheckFor + ' exists...');
-
- $('#sortabletable tr').each(function() {
- var playerIdcellvalue = $(this).find("#SessionID").text(); // get handle for the cell containing the corresponding id
- if(playerIdcellvalue == SessionIDtoCheckFor) {
- dump('Found session match: ' + SessionIDtoCheckFor);
- // Found the row with the cell
- SessionIDFound = true;
- }
- });
-
- if(SessionIDFound == false) {
- // New session!
- dump('No existing session found, adding session...');
-
- // Generate row...
- var a, b, c, d, e, f, g, h, i, newrow; //there's a better way to do this, but this way was done for debugging purposes
- a = "<tr class=''>";
- b = "<td id='SessionID'>" + player[j]['SessionID'] + "</td> ";
- c = "<td id='Device'>" + player[j]['Device'] + "</td> ";
- d = "<td id='Source'>" + player[j]['Source'] + player[j]['ANI'] + "</td> ";
- e = "<td id='Region'>" + player[j]['Region'] + "</td> ";
- f = "<td id='QueuedTime'>" + player[j]['QueuedTime'] + "</td> ";
- g = "<td id='Game'>" + player[j]['Game'] + "</td> ";
- h = "<td id='Desc'>" + player[j]['Desc'] + "</td></tr>";
-
- newrow = a + b + c + d + e + f + g + h;
-
- dump('row html to add: \n' + newrow);
-
- $('#sortabletable tr:last').after(newrow); // Add new row to the table
-
- dump('new session has been added to table.');
- // execute sort (on hold)
- }
- }
- }
-
- // TODO: Swap out sortable.js with this:
- // http://tablesorter.com/docs/
- }
- var enabledebugger = true;
- var productElement = document.getElementById('debugthis');
- if (productElement != null){
- enabledebugger = true;
- }
- setInterval( "updateQueue()", 4000 ); // Execute updateQueue function every four seconds.
This is the html:
- <form id="filterform"><input type="hidden" id="filtertype" name="filtertype" value="<?=$QType;?>" /></form>
-
- <? if($enabledebugger){ ?><textarea id="debugthis" rows="30" cols="200">
- Debugger Console
- ------------------
- </textarea><? } ?>
- <table border="0" cellpadding="5" cellspacing="1" width="100%" class="sortable" id="sortabletable">
- <tbody>
- <tr class="header">
- <td class="header">Session ID</td>
- <td class="header">Device</td>
- <td class="header">Source:</td>
- <td class="header">State</td>
- <td class="header">Queued Time</td>
- <td class="header">Game</td>
- <td class="header">Device Info</td>
- </tr><?
- if($theorder = 'odd'){
- $theorder = 'even';
- } else {
- $theorder = 'odd';
- }?>
- </tbody>
- </table>
My apologies for wrong usage of JQuery/javascript; I'm still trying to wrap my head around it. Feel free to criticize it; I'm always appreciative for opportunities for improvement. My thanks in advance for your time, and feedback!