Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Search Features - search in multiple sources at the same time #1115

Closed
andreaordonselli opened this issue Dec 4, 2024 · 8 comments
Closed

Comments

@andreaordonselli
Copy link

Good morning,
search features can be set to search in multiple sources at the same time showing a unique list of results?
I would like to search in multiple simultaneous layers by defining one or more fields to look for.
Thanks

@Viglino
Copy link
Owner

Viglino commented Dec 4, 2024

No, but you can create a source collection to concat sources on search (it just need to get features in the source):

class sourceCollection extends ol.source.Vector {
    constructor(opt) {
        opt = opt || {};
        super(opt);
        this._sources = opt.sources
    }
    getFeatures() {
        var a = [];
        this._sources.forEach(s => a = a.concat(s.getFeatures()))
        return a;
    }
}

then you can use it as a source:

search.setSource(new sourceCollection ({ sources: [source1,source2] })

@andreaordonselli
Copy link
Author

Thank you, this requires both sources to have the same property to search in right? and if the fields to search in were different between the two sources how could I set them?

@Viglino
Copy link
Owner

Viglino commented Dec 4, 2024

No, you can define a getSearchString function that will take a feature and returns a string to search in, instead of a property name.

// Search in prop1 or prop2
var search = new ol.control.SearchFeature({
   source: vectorSource,
   getSearchString: function(f) {
     return (f.get('prop1') || '') + ' ' + (f.get('prop2') || '')
   }
});

@andreaordonselli
Copy link
Author

andreaordonselli commented Dec 10, 2024

Sorry but without defining the property value it seems that the getSearchString: has no effect

@Viglino
Copy link
Owner

Viglino commented Dec 10, 2024

You have to define your own getSearchString function.
The default one uses a property value but you can overwrite it.

@andreaordonselli
Copy link
Author

I understood, thank you.
One last thing, if I enter the getTitle setting at a later time this has no effect, while if I write it at the creation of the var it works fine, how can I do that?

search.set('getTitle', function(f) {
	var title = 'F' + f.get('FOGLIO') + ' ' + 'P' + f.get('PARTICELLA');
	return title;
});

@Viglino
Copy link
Owner

Viglino commented Dec 11, 2024

getTitle is a function, not a property of the objet.
You have to set it like that:

search.getTitle = function(f) {
	var title = 'F' + f.get('FOGLIO') + ' ' + 'P' + f.get('PARTICELLA');
	return title;
});

@andreaordonselli
Copy link
Author

Many thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants