sorting a repeating Table

M

martin.pichler

Hi,

I am trying to sort a repeating table with the help of JScript. I found
an example in an demoform that came with Infopath.

I am using this code, but I could not figure out what the function
expects as fnCompare.
Can anyone shed some light please? I always get an error at this line
rgItems.sort(fnCompare);

Thanks in advance, Martin

function sort(xpathParent, xpathChildren, fnCompare)
{
// Retrieve the parent node; all the children will be sorted.
var xmlOrigParent = getNode(xpathParent)
var xmlItems = xmlOrigParent.selectNodes(xpathChildren);

if (1 < count(xmlItems))
{
// Store the (pointers to) items in an array for faster access.
rgItems = new Array();

while (xmlItem = xmlItems.nextNode())
rgItems.push(xmlItem);

// Sort the array (the XML does not change).
-------> rgItems.sort(fnCompare);

// Now that the array is sorted the DOM should be updated.
var xmlSortParent = xmlOrigParent.cloneNode(true);
var xmlClones = xmlSortParent.selectNodes(xpathChildren);
xmlClones.removeAll();

// Update the nodelist, each item is moved only once.
// (each change is causing several notifications to be fired.)
for (var i=0; i<rgItems.length; i++)
xmlSortParent.appendChild(rgItems.cloneNode(true));

xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
}
}
 
S

S.Y.M. Wong-A-Ton

Hi Martin,

When you call the sort(xpathParent, xpathChildren, fnCompare) function, you
need to pass the name of the function doing the actual comparison between two
child nodes as the third parameter (=fnCompare).

Search the demoform for a function in which two nodes are compared with each
other and a -1, 1, or 0 is returned, copy this function into your own form,
and use its name as the fnCompare parameter when calling sort(xpathParent,
xpathChildren, fnCompare).

Regards,
S.Y.M. Wong-A-Ton
 
M

Martin P.

Hi S.Y.M Wong-A-Ton,

thanks for your reply, but I cannot find a suitable function in any of
the examplen on my machine. Either I am blind or plain stupid. Could
you please point me in the right direction....

Thanks a ton
Martin
 
S

S.Y.M. Wong-A-Ton

Hi Martin,

Took a long while for you to respond. :) Almost missed your message...

Take a look at the "Asset Tracking" form that comes with InfoPath. Open its
script file and search for a function with the name "compareAssets". That
should get you started.

Regards,
S.Y.M. Wong-A-Ton
 
M

Martin P.

Hi S.Y.M Wong-A-Ton,

yeah I was quite busy with other things... chritsmas and stuff ;-)

Thanks for your hint, I will try this right away!

Regards,
Martin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top