...

View Full Version : first page of records not displaying in JS/AJAX app



chump2877
12-07-2006, 10:51 PM
Ok, let me paint a picture for you here:

I have the following app that i 'm working on: http://www.mediamogulsweb.com/client/parseXML/testingHTML.html.

For the most part, it's working as expected so far (with a few exceptions). Right now I would like to address one of those exceptions.

To illustrate the current problem, go to the above URL and double-click on "1999 Grammy Nominees", the data in the title field of the first record of the catalog. An edit box will appear. Modify this data text however you want and then click outside of the box to commit to the changes.

Now go to the search box at the top of the catalog and enter a search string (such as "Grammy") that will for sure bring up "1999 Grammy Nominees" in the search results. You should probably only get 1 search result -- the record in which you just modified.

NOW, click on "Return to Catalog" in the upper left corner of the search results. Normally, this javascript link will reload the last page of the catalog that you were viewing before you initiated the search request. In this particular case, however, the last-viewed page (of the catalog) DOES NOT load at all.

OK, now I hope that you see what the problem is here. I've been wracking my brain triying to pinpoint the problem, and the best that i can do is isolate the problem to this JS code/function:


function paginate()
{
var pageNumTotal = Math.ceil(records.length / recsPerPage);
var pagesArray = new Array;
var z = 0;

for (i=0; i<pageNumTotal; i++)
{
pagesArray[i] = new Array;
var uu = 0;

for (y=z; y<=z+(recsPerPage-1); y++)
{
if (!records[y])
{
break;
}

pagesArray[i][uu] = records[y];
uu++;
}

z += recsPerPage;
}

return pagesArray;
}

More specificially, it seems that this line of code is where the heart of the problem lies:


if (!records[y])
{
break;
}

For some reason, after you follow the above steps for modifying the above catalog record, perform the search, and then attempt to return to the last viewed page, this conditional statement returns true somewhere between iterating between records[0] and records[9] (the first page of records)....Thus, the first page of records (pagesArray[0]) is empty, and this explains the lack of any records when you return to the last viewed page of the catalog (after the search is performed)...

But this doesn;t make any sense, because these sub-arrays absolutely do exist, as is indicated if you do a records.length prior to the conditional. records.length reveals that all 26 records in the XML file and catalog are present and should be accounted for.

So why is this not working if records[0] thru records[9] still exist? I'm at a loss here, and any help is greatly appreciated.

I should also mention that the above code appears to work in every other experiment/test that I've tried. Just in this particular instance, does the code fail.

Thanks for taking the time to read this.

david_kw
12-08-2006, 12:16 AM
The problem looks like it is when you are doing your sorting. You have an array sortBy that has album names in it. But that array isn't updated when you edit a name. So when you are sorting and get to line 419:

else if (sortByArray[i] == sortAlbum[y][string_cat])

it never happens. sortByArray has the old name but sortAlbum has the new name. Thus the record is recorded as undefined. In fact, you use the slice to get rid of records. So you can put a test in at the end of sort records to see how many are left.

if (sortAlbum.length > 0) alert("Didn't include all items");

You'll see what I mean.

So since there is an undefined in records, paginate() dumps out of the page creation on the line 325

if (!records[y])

As for the fix, I don't totally understand your data structures but you need to find a way to update sortBy when changes are made to a field.

david_kw

chump2877
12-08-2006, 01:33 AM
Thanks again david_kw :thumbsup:

I think I fixed it with this code, the new code is in red:

// Define a "callback" function to process the data returned by the Ajax request:


function processGetPost()
{
var myajax = ajaxpack.ajaxobj;

if (myajax.readyState == 4)
{
// if request of file completed

if (myajax.status == 200 || window.location.href.indexOf("http") == -1)
{
// if request was successful or running script locally
// Update catalog code here

var dataNew = myajax.responseText;
var dataNew_arr = dataNew.split("|^|^");
document.getElementById(dataNew_arr[0]).innerHTML = dataNew_arr[1];
document.getElementById(dataNew_arr[0]).style.padding = "";
document.getElementById(dataNew_arr[0]).style.color = "#000";

for (y=0; y<myPages[currentPage].length; y++)
{
if (myPages[currentPage][y]['prodID'] == dataNew_arr[3])
{
var myRegxp3 = /^(\${1}.+)$/;
if (myRegxp3.test(dataNew_arr[1]) == true)
var data_new = dataNew_arr[1].substring(1);
else
var data_new = dataNew_arr[1];

myPages[currentPage][y][dataNew_arr[2]] = data_new;
}
}

for (k=0; k<sortBy.length; k++)
{
if (k+1 == dataNew_arr[3] && string_cat == dataNew_arr[2])
{
sortBy[k].childNodes[0].nodeValue = data_new;
}
}

stopLoad = 1;
loadXML(xmlURL);
}
else
{
//alert('myajax.status='+myajax.status);
}
}
else
{
//alert('myajax.readyState='+myajax.readyState);
}
}

I just updated the nodeValue of the element that was changed -- like you suggested...look right to you?

david_kw
12-08-2006, 01:56 AM
I'm not sure about the details, but the idea looks right and makes sense. I hope it works for ya. :)

david_kw



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum