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[i].cloneNode(true));
xmlOrigParent.parentNode.replaceChild(xmlSortParent, xmlOrigParent);
}
}
S.Y.M. Wong-A-Ton - 17 Dec 2005 17:23 GMT
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
> Hi,
>
[quoted text clipped - 38 lines]
> }
> }
Martin P. - 11 Jan 2006 09:31 GMT
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.Y.M. Wong-A-Ton - 11 Jan 2006 12:18 GMT
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
> Hi S.Y.M Wong-A-Ton,
>
[quoted text clipped - 4 lines]
> Thanks a ton
> Martin
Martin P. - 13 Jan 2006 11:11 GMT
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