How can I specify multiple display fields in the search widget

2065
3
Jump to solution
06-27-2017 01:33 PM
JackFairfield
Occasional Contributor II

In esri's 3.x or 4.x version, how can I search a feature layer by a field that has duplicate records?  For example, I want to search my feature layer with employers.  If I type "Mcdonalds", I get a list full of suggestions that all say "Mcdonalds" like this:

What I want to show in the suggestions is something like this:

Mcdonalds - address1

Mcdonalds - address2

Mcdonalds - address3

Mcdonalds - address4

Here is a section of my code for initializing the search widget.  I am specifying the "displayName" option: 

var sources = [{
   featureLayer: new FeatureLayer({ url: url, popupTemplate: popupTemplate }),
   searchFields: layer.search.searchFields,
   displayField: layer.search.displayField,
   name: layer.search.title || layer.title,
   outFields: layer.search.outFields,
   placeholder: layer.search.placeHolder
}]
var search = new Search({
viewModel: {
view: app.mapView,
maxSuggestions: 4
},
sources: sources
},'divID');

I am pretty sure there is no way in the api to do this.  Has anyone done this in their project or have any recommendations?

0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Jack,

  You need to look at the suggestionTemplate property of the sources object. Here is an example:

{
     featureLayer: new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/US_Senators/FeatureServer/0"),
    searchFields: ["Name", "Party"],
    suggestionTemplate: "${Name}, Party: ${Party}",
    exactMatch: false,
    outFields: ["*"],
    name: "Senators",
    labelSymbol: textSymbol,
    placeholder: "Senator name",
    maxResults: 6,
    maxSuggestions: 6,
    enableSuggestions: true,
    minCharacters: 0,
    localSearchOptions: {distance: 5000},
   }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

View solution in original post

3 Replies
RobertScheitlin__GISP
MVP Emeritus

Jack,

  You need to look at the suggestionTemplate property of the sources object. Here is an example:

{
     featureLayer: new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/US_Senators/FeatureServer/0"),
    searchFields: ["Name", "Party"],
    suggestionTemplate: "${Name}, Party: ${Party}",
    exactMatch: false,
    outFields: ["*"],
    name: "Senators",
    labelSymbol: textSymbol,
    placeholder: "Senator name",
    maxResults: 6,
    maxSuggestions: 6,
    enableSuggestions: true,
    minCharacters: 0,
    localSearchOptions: {distance: 5000},
   }‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
JackFairfield
Occasional Contributor II

Robert,

Thank you so much!  This worked perfectly!  Not sure how I missed this in the documentation.  You always seem to be the one answering questions here on GeoNet.  I greatly appreciate it!

Jack

0 Kudos
Arne_Gelfert
Occasional Contributor III

Yes, same here... I kept trying to figure out the displayField parameter. The above worked great except that I found no use for the $'s. If you have features with a Name and Number attribute, this worked:

suggestionTemplate: "Name: {NAME}, Number: {NUMBER}",
0 Kudos