0

I want to search by two columns of the database.

Right now i have this code:

  var search = new ol.control.SearchFeature({
    placeholder: 'Search',
    source: vectorSource,
    maxItems: 20,
    property:  'name',
    
  });
  map.addControl (search);


2
  • So one of your "columns" is name, and the other is ...?
    – TomazicM
    Commented Mar 10, 2023 at 12:37
  • the other column would be adress
    – need help
    Commented Mar 10, 2023 at 18:12

1 Answer 1

1

Instead of property option you can use getTitle option to specify/construct search property.

With getTitle option you specify a function that takes a feature and return the name to display in the search index (see http://viglino.github.io/ol-ext/doc/doc-pages/ol.control.SearchFeature.html).

In your case code could then loke something like this:

function getTitle(feature) {
  var searchedTxt = feature.get('name') + ', ' + feature.get('adress');
  return(searchedTxt);
}

var search = new ol.control.SearchFeature({
  placeholder: 'Search',
  source: vectorSource,
  maxItems: 20,
  getTitle: getTitle
});
map.addControl (search);
2
  • Thanks a lot! it works perfectly
    – need help
    Commented Mar 13, 2023 at 18:10
  • If answer helped you solve you problem, it's customary on GIS SE site to mark it as accepted, to let others with similar problems know that problem was resolved.
    – TomazicM
    Commented Mar 13, 2023 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.