Select to view content in your preferred language

Search Widget with Layer View filter

183
3
2 weeks ago
jauhari_mani
Occasional Contributor

Hi Community,

I have created a search widget with layer as a source, which works completely fine, but I have a feature to filter features of the layer and when we filter features on layer view, search widget still suggests all the options based on layer without filter. How can I achieve that.

Regards,
Mani Jauhari

0 Kudos
3 Replies
JoelBennett
MVP Regular Contributor

Unfortunately, the Search widget doesn't take into account the LayerView.filter when executing queries when using a LayerSearchSource.  However, the LayerSearchSource does have a filter property which you can use to duplicate at least some of the LayerView.filter's properties.

 

layerSearchSource.filter = {
	where: layerView.filter.where,
	geometry: layerView.filter.geometry
};

 

Fortunately, "where" and "geometry" are the most common properties, so that might be all you need.

0 Kudos
jauhari_mani
Occasional Contributor

Hi @JoelBennett,
I have tried this way but the layerSearchSource filter captures the static value from layerView filter at the time of creation, it does not take any reference from layerView, so if I update filter of layerView in the lifetime it does not affects layerSearchSource filter of search widget.

Also, I tried to update search widget property whenever I change filter for layerView, in that I am getting error with sources is a read only property can not be modified.
And I wish not to destroy search widget and create new one whenever filter changes as that will not be good on UI and it also kills the DOM container when destroy is called.

Thanks,
Mani

0 Kudos
JoelBennett
MVP Regular Contributor

Ok, you can do something like the following so that if you update the LayerView.filter, it will automatically update the LayerSearchSource.filter as well:

function setupFilter(layerSearchSource, layerView) {
	if (layerView.filter) {
		layerSearchSource.filter = {
			where: layerView.filter.where,
			geometry: layerView.filter.geometry
		};

		layerView.filter.watch(["where", "geometry"], function(newValue, oldValue, propertyName, target) {
			if (propertyName == "where")
				layerSearchSource.filter.where = newValue;
			else if (propertyName == "geometry")
				layerSearchSource.filter.geometry = newValue;
		});
	} else
		layerSearchSource.filter = null;
}

layerView.watch("filter", function(newValue, oldValue, propertyName, target) {
	setupFilter(layerSearchSource, layerView);
});

setupFilter(layerSearchSource, layerView);
0 Kudos