Select to view content in your preferred language

Remove Null values from the Search suggestionTemplate

697
2
Jump to solution
10-03-2023 02:08 PM
ToddGurnee
Occasional Contributor

 

Thanks for any help in advance,

We have been rebuilding our CAMPUS Place Locator application. Nearly done but I have been stuck trying to remove NULL/Blank values from my Search suggestionTemplate results. The example below is when the result is an office without an employee in it:

ToddGurnee_0-1696355686411.png

Here is my widget code:
const searchWidget = new Search({
      view: view,
      allPlaceholder: "Search for Rooms, People, Last 4 of Phone ",
      includeDefaultSources: false,
      locationEnabled: false,
      sources: [{
          layer: SearchSpace,
          searchFields: ["KNOWNAS", "SHORTNAME", "Phone", "MOBILE"],
          suggestionTemplate: "Name: {KNOWNAS}, Room: {SHORTNAME}, Phone: {PHONE}, Cell: {MOBILE}, {WORKSITE} ",
          exactMatch: false,
          maxResults: 50,
          minSuggestCharacters: 0,
          maxSuggestions: 50,
          outFields: ["*"],
          placeholder: "Search for Rooms, People, Last 4 of Phone",
          zoomScale: 500
      },
  ]
  });

 

ESRI's Search Widget example here: Search widget with multiple sources | Sample Code | ArcGIS Maps SDK for JavaScript 4.27 | ArcGIS Dev... is similar to my code but their data doesn't contain any NULLs. What would the code look like if you wanted to remove "Party: " when the Senator didn't have an assigned party?

ToddGurnee_0-1696366491343.png

Thanks

 

0 Kudos
1 Solution

Accepted Solutions
JoelBennett
MVP Regular Contributor

The API doesn't provide a means for doing what you're looking for, but that doesn't mean you can't pull it off.  Although the documentation says a String is expected for suggestionTemplate, you can presently get away with an object that has simply has a "replaceAll" function.  Since such an implementation isn't according to the documentation, it might not work in future releases, but it does work now (in 4.27).

For example, with the senators sample you referred to, the critical part of the code (i.e. where suggestionTemplate is defined) could look something like this:

sources: [
	{
		//etc
		suggestionTemplate: {
			replaceAll: function(a, b) {
				var piecesIn = [
					["{Name}", ""],
					["Party: {Party}", "Party: "]
				];

				var piecesOut = [];

				piecesIn.forEach(function(pieceIn) {
					var pieceOut = pieceIn[0].replaceAll(a, b);

					if (pieceOut !== pieceIn[1])
						piecesOut.push(pieceOut);
				});

				if (piecesOut.length === 0)
					return "";
				else if (piecesOut.length === 1)
					return piecesOut[0];
				else
					return piecesOut.join(", ");
			}
		}
		//etc
	}
]

 

In your case, you would just replace the definition of "piecesIn" with something like this:

var piecesIn = [
	["Name: {KNOWNAS}", "Name: "],
	["Room: {SHORTNAME}", "Room: "],
	["Phone: {PHONE}", "Phone: "],
	["Cell: {MOBILE}", "Cell: "],
	["{WORKSITE} ", " "]
];

 

View solution in original post

0 Kudos
2 Replies
JoelBennett
MVP Regular Contributor

The API doesn't provide a means for doing what you're looking for, but that doesn't mean you can't pull it off.  Although the documentation says a String is expected for suggestionTemplate, you can presently get away with an object that has simply has a "replaceAll" function.  Since such an implementation isn't according to the documentation, it might not work in future releases, but it does work now (in 4.27).

For example, with the senators sample you referred to, the critical part of the code (i.e. where suggestionTemplate is defined) could look something like this:

sources: [
	{
		//etc
		suggestionTemplate: {
			replaceAll: function(a, b) {
				var piecesIn = [
					["{Name}", ""],
					["Party: {Party}", "Party: "]
				];

				var piecesOut = [];

				piecesIn.forEach(function(pieceIn) {
					var pieceOut = pieceIn[0].replaceAll(a, b);

					if (pieceOut !== pieceIn[1])
						piecesOut.push(pieceOut);
				});

				if (piecesOut.length === 0)
					return "";
				else if (piecesOut.length === 1)
					return piecesOut[0];
				else
					return piecesOut.join(", ");
			}
		}
		//etc
	}
]

 

In your case, you would just replace the definition of "piecesIn" with something like this:

var piecesIn = [
	["Name: {KNOWNAS}", "Name: "],
	["Room: {SHORTNAME}", "Room: "],
	["Phone: {PHONE}", "Phone: "],
	["Cell: {MOBILE}", "Cell: "],
	["{WORKSITE} ", " "]
];

 

0 Kudos
ToddGurnee
Occasional Contributor

Thank you Joel. This works perfectly!

0 Kudos