Expand my Community achievements bar.

Loop or count for unique in dropdown?

Avatar

Level 2

I'm trying to build my first dynamic dropdown populated from an XML file.

The XSD and XML files are output from Access. The nodes of the XML are pretty simple, example as follows:

<AllStudentsAllSchools>
<LastName>Doe<LastName>
<FirstName>Jane<FirstName>
<SchoolName>Main street Middle<SchoolName>
</AllStudentsAllSchools>

The dyanamics on the PDF are running - I'm getting fields filled from the XML.

(As in the above sample XML) For a dropdown to just list the unique school names and not repeat every row that contains a school name... Do I loop through the XML with a javascript loop or count, etc? Or can this be set up automatically in Livecycle, maybe with the Dynamic Properties dialog under the binding tab?

Currently, what I have is the dropdown only reads the first row, pulling only one school name.

Each of the subforms within the main flowed form are set up as Fragments (Including this school name dropdown). Some fragments are dynamic and some not. The dropdown within the fragment is the dynamic one, reading the XML file. The same XML file is the data connection for the PDF (that contains the Fragments).

Example PDF, XSD and XML attached, although the dropdown isn't set up as a Fragment here.

Thanks for any responses...

- e

3 Replies

Avatar

Level 2

If you generate a unique list of school names from Access, then you can use simple dynamic property binding to populate the dropdown.

If you are constrained to the current XML schema, then you are going to have to populate the dropdown with code.

The high-level steps are:

1. Retrieve all school names

2. Create a list of unique school names

3. Populate the dropdown

1. Retrieve all school names

I recommend retrieving the school names directly from the data DOM, rather than use unecessary binding e.g.

var schoolNames = new Array();
var schools = xfa.data.resolveNodes("dataroot.AllStudentsAllSchools[*]");

for (var i = 0; i < schools.length; i++){
    schoolNames.push(schools.item(i).SchoolName.value);
}

2. Create a list of unique school names

There are various ways to remove the non-unique values e.g.

var uniqueSchoolNames = new Array();
schoolNames.sort();

for (var j = 0, k = 0; j < schoolNames.length; j++){
    if (j > 0 && schoolNames[j] != uniqueSchoolNames[k-1]){
        console.println("Adding to unique: " + schoolNames[j]);
        uniqueSchoolNames.push(schoolNames[j]);
        k++;
    }
}


3. Populate the dropdown

Simply clear the dropdown and add the values from the array e.g.

SchoolName.clearItems();
for (var l = 0; l < uniqueSchoolNames.length; l++){
    SchoolName.addItem(uniqueSchoolNames[l]);
}

Ben Walsh

http://www.avoka.com

@ben_walsh

Avatar

Level 2


Thanks so much for the advice, Ben.

I'm not sure where to put the code you sent.

I have an on "exit" switch case javascript on this dropdown that calls the next dropdowns that appear when a user selects a school name.

I tried putting your code above this switch case as well as below it but neither pulled dynamically from the dom.

The PDF default connection is the XSD and XML (on the PDF that contains this school names dropdown fragment). There are many other nodes in the XML - with the school names dropdown and a couple of other dropdowns being dynamic - a 'swith-case' on both static and dynamic dropdowns.

Is the three step code you sent to be set up on the fragment containing this dropdown or on the PDF itself that contains this subform-fragment? Do I need to set up the default data connection on the fragment file as well or does the '3 step' javascript not need this?

I'm not sure how to mix the switch-case with what you sent since I wouldn't know before hand how many schools the dropdown may display at any given time. I know I could'nt use the following since there would be more than just a "case 0". Maybe just an "on select anything" type javascript?

switch (this.selectedIndex){
case 0 : ewcDataManageSubform.presence = "visible";
             ewcPermissionsSubform.presence = "hidden";
             ewcOrderBySubform.presence = "hidden";
             SubmitButton1Subform.presence = "hidden";
             break;
default : ewcDataManageSubform.presence = "hidden";
             ewcPermissionsSubform.presence = "hidden";
             ewcAnthropoSubform.presence = "hidden";
             ewcFitTestSubform.presence = "hidden";
             ewcOrderBySubform.presence = "hidden";
             SubmitButton1Subform.presence = "hidden";
             break;
}


Thanks again for your input and sending the javascript solution... Just not sure where to put it and if I need to bind the fragment form to a default connection pointing to the XML (as well as the PDF that displays the fragment subform). A learning curve for me here but, what an awesome program this is!

- e

Avatar

Level 10

There's a bunch of info to do with xml and dropdowns in this thread. Hope it helps!