Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

Add/Remove the same exact item in a dropdown

Avatar

Level 3

Hi!

I need to be able to delete/hide and /add/show THE SAME ITEM, or that it look like the same item, please see the example of what I´m getting:

When you open the document:

01.png

If you click "yes" in the first radio, value 4 is added

02.png

If you click "yes" in the second radio, value 5 is added

03.png

If you click "no" on the first radio, you remove value 4 BECAUSE you are removing the item #3

04.png

If you click "no" on the second radio, you remove nothing 4 BECAUSE you are trying to remove item #4


05.png

And then you get this weird combinations

06.png

This is the code on the first Radio

 form1.#subform[0].value_4::click - (JavaScript, client)

if (this.rawValue == "1") {
     DropDownList1.addItem("value 4");
}else {
    DropDownList1.deleteItem(3);
}

This is the code on the second radio

 form1.#subform[0].value_5::click - (JavaScript, client)

if
(this.rawValue == "1") {
       DropDownList1.addItem("value 5");
}else {
       DropDownList1.deleteItem(4);
}

Does anyone know how to delete a specific item from a dropdrown? or maybe not delete it, just hide if this is possible... I tried editing specific values to add/delete always the same item but it didnt work.

Thanks!

1 Accepted Solution

Avatar

Correct answer by
Level 8

Ya, I find it weird that there are no native xfa methods to get the index of an item in the drop down. Not documented at least (as far as I know).

What you can do though is create your own method!

Here, in the initialize event of your top most subform you create a method that exends the object class (which includes all XFA objects too), just like native JavaScript:

//@para String value of the item in the drop down

//@return Boolean - true if item was deleted, false if not and null object if not found in the list


Object.prototype.deleteItemValue=function(sVal){

for (var a=0;a<this.items.nodes.length;a++){

  if (this.items.nodes.item(a).value==sVal)

   return this.deleteItem(a);

}

return null;

}

Then you can delete any item from your drop down by modying your code to:

if (this.rawValue == "1") {   

DropDownList1.addItem("value 4");

}

  else {   

DropDownList1.deleteItemValue("value 4");

}

Kyle

View solution in original post

3 Replies

Avatar

Correct answer by
Level 8

Ya, I find it weird that there are no native xfa methods to get the index of an item in the drop down. Not documented at least (as far as I know).

What you can do though is create your own method!

Here, in the initialize event of your top most subform you create a method that exends the object class (which includes all XFA objects too), just like native JavaScript:

//@para String value of the item in the drop down

//@return Boolean - true if item was deleted, false if not and null object if not found in the list


Object.prototype.deleteItemValue=function(sVal){

for (var a=0;a<this.items.nodes.length;a++){

  if (this.items.nodes.item(a).value==sVal)

   return this.deleteItem(a);

}

return null;

}

Then you can delete any item from your drop down by modying your code to:

if (this.rawValue == "1") {   

DropDownList1.addItem("value 4");

}

  else {   

DropDownList1.deleteItemValue("value 4");

}

Kyle

Avatar

Level 5

This Script would help you ...

Keep this script change event of the radio button list and uncheck the check box "Specify Item Values" under binding tab of the check box.

var nDD = DropDownList1.length;

var flag = 0;            

if (this.rawValue == "1") {

          for (var i=0; i< nDD; i++) {

                      if(DropDownList1.getDisplayItem(i) == "value 4"){

                                flag = 1;

                      }

            }

          if(flag != 1){

               DropDownList1.addItem("value 4")

     }

}

else {

                      for (var i=0; i<nDD; i++) {

                                if(DropDownList1.getDisplayItem(i) == "value 4"){

                                DropDownList1.deleteItem(i)

                      }

          }

}

You can use same script for the option 5 also.

I tried to share the sample file but i couldnt.

Let me knw how it goes...

Thanks

Vjay

Avatar

Level 3

Kyle, Vjay thanks!

I kept Kyle's code but both pieces of code worked perfectly!