Skip to main content
Yakuza Crow
Participant
June 7, 2023
Answered

Search Matches in one listbox and print the matches in another list box

  • June 7, 2023
  • 1 reply
  • 702 views

Hello! I have the following code, to find matches in a listbox and print it in another listbox, but it doesn't work, I don't know if it's possible to do this.
The script is in the document javascript section:

 

// Function to search for matches in the ListBox and show them in another ListBox
function findMatches() {
// Get the value of the search text box
var searchText = this.getField("criteria").value.toLowerCase();

// Get the ListBox where the matches will be displayed
var listBoxData = this.getField("listdata");
var listBoxResults = this.getField("listresults");

// Clear the ListBox of results
listBoxResults.clearItems();

// Array to store the matches found
var matches = [ ];

// Loop through the options of the data ListBox
for (var i = 0; i < listBoxData.numItems; i++) {
var option = listBoxData.getItemAt(i);

// Check if the option text partially matches the search
if (option && option.value && option.value.toString().toLowerCase().includes(searchText)) {
// Add the option to the matches array
matches.push(option.value);
}
}

// Add the matches to the ListBox of results
for (var j = 0; j < matches.length; j++) {
listBoxResults.addItem(matches[j]);
}
}


In the button I have the following script to call the function:
searchMatches();

 

When trying to search for matches it does not return any results, please help me 🙂

This topic has been closed for replies.
Correct answer try67

Use this code:

 

// Function to search for matches in the ListBox and display them in another ListBox
function searchMatches() {
  // Get the value from the search text field
  var searchText = this.getField("criterio").value.toLowerCase();

  // Get the ListBox where the matches will be displayed
  var listBoxData = this.getField("listadatos");
  var listBoxResults = this.getField("listaresultados");

  // Clear the results ListBox
  listBoxResults.clearItems();

  // Array to store the found matches
  var matches = [];

  // Iterate through the options in the data ListBox
  for (var i = 0; i < listBoxData.numItems; i++) {
    var v = listBoxData.getItemAt(i);

    // Check if the option text partially matches the search text
    if (v && v.toString().toLowerCase().indexOf(searchText)!=-1) {
      // Add the option to the matches array
      matches.push(v);
    }
  }
	if (matches.length>0)
		listBoxResults.setItems(matches);
}

// Associate the searchMatches function with the button's click event
//this.getField("buscar").setAction("MouseUp", "searchMatches();");

 

I commented out the last line since it only needs to be used once, and would cause an error if the file is used in Reader.

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
June 7, 2023

Use this code:

 

// Function to search for matches in the ListBox and display them in another ListBox
function searchMatches() {
  // Get the value from the search text field
  var searchText = this.getField("criterio").value.toLowerCase();

  // Get the ListBox where the matches will be displayed
  var listBoxData = this.getField("listadatos");
  var listBoxResults = this.getField("listaresultados");

  // Clear the results ListBox
  listBoxResults.clearItems();

  // Array to store the found matches
  var matches = [];

  // Iterate through the options in the data ListBox
  for (var i = 0; i < listBoxData.numItems; i++) {
    var v = listBoxData.getItemAt(i);

    // Check if the option text partially matches the search text
    if (v && v.toString().toLowerCase().indexOf(searchText)!=-1) {
      // Add the option to the matches array
      matches.push(v);
    }
  }
	if (matches.length>0)
		listBoxResults.setItems(matches);
}

// Associate the searchMatches function with the button's click event
//this.getField("buscar").setAction("MouseUp", "searchMatches();");

 

I commented out the last line since it only needs to be used once, and would cause an error if the file is used in Reader.

Yakuza Crow
Participant
June 13, 2023

Thanks! work fine 🙂