Verify if the object is listed or not | Core Java Forum
M
Mustapha Rhouate Posted on 12/11/2019

HI
I have this ddl that has two items (enabled and all)

by default ddl show enabled if object is enabled it will appears in the list if not it will not 

my question I need to create methode that will verify if object is listed or not once I visiste the page.


Y
Yogesh Chawla Replied on 13/11/2019

select tag in HTML is used to create a dropdown list of options which can be selected.
The option tag contains values that can be used.

The default value of the select element can be set by using ‘selected’ attribute on the required option. This is a boolean attribute.
Your HTML page has selected as ! Org 1(11275613)

You have mentioned all these values as options in your dropdown list(select tag)

To check if a particular value belongs to dropdown list or not?

Without jQuery

var exists = false;
for(var i = 0, opts = document.getElementById('participantID').options; i < opts.length; ++i)
if( opts[i].value === 'Your Value' )
{
exists = true;
break;
}

You can disable a drop down list like this

document.getElementById("participantID").disabled = true;

To enable/disable drop down list box using JavaScript

Let's take this example:

<select name="A" disabled="disabled">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
<option value="4">$</option>
</select>
<select name="B">
<option value="">Select...</option>
<option value="B1">B1</option>
<option value="B2">B2</option>
<option value="B3">B3</option>
<option value="B4">B4</option>
</select>

In JavaScript, write this:

document.getElementsByName('B')[0].addEventListener('change', function(e){
var a = document.getElementsByName('A')[0];
var b = e.target;
a.disabled = b.value != '' ? '' : 'disabled';
});

Did I answer your question or does this answer helps?


M
Mustapha Rhouate Replied on 13/11/2019

Thanks for the answer but I was looking for something in java.


Y
Yogesh Chawla Replied on 14/11/2019

What I have understood is:

You have two DDLs(Data Definition Language), Let's say two select queries that bring two kinds of data from database
enabled and all

and enabled query gets fired by default
and there is organization data which is where the enabled query data is shown

And your question is - you want to create a method that should verify if the particular organization type data is listed in this list or not

Solution:
The contains() method of List interface in Java is used for checking if the specified element exists in the given list or not.


public static boolean useList(String[] arr, String targetValue) {
// converting Array to List so that contains method can be used
return Arrays.asList(arr).contains(targetValue);
}

 

Using Set:

public static boolean useSet(String[] arr, String targetValue) {
Set<String> set = new HashSet<String>(Arrays.asList(arr));
return set.contains(targetValue);
}

 

Using a simple loop:

public static boolean useLoop(String[] arr, String targetValue) {
for(String s: arr){
if(s.equals(targetValue))
return true;
}
return false;
}

Does this helps?