Search Widget Order by

813
4
03-01-2019 06:29 AM
UlrichOpalka
New Contributor

Hi,

I want to sort the search result(SuggestResult) with the Api 4 and the  "esri/widgets/Search "!

The featureservice allows you to sort:

The Advanced Query Capabilities:

      Supports Statistics: true

The standard sort is the objectid!

Where do I specify orderByFields: ["key","hsn","hsn_zusatz"]?

    var searchWidget = new Search({
        view: view,
        allPlaceholder: "Search for",
        searchAllEnabled: false,
        includeDefaultSources: false,
        sources:[{
                  featureLayer:{
                      url:"XXX/Search_HSN/FeatureServer/1",
                      popupTemplate: {
                          title: "Adresse: {strasse_hsn}",
                          overwriteActions: false,
                      },
                      },
                 //orderByFields: ["key_alb","hsn","hsn_zusatz"], ?????? where
                  searchFields: ["strasse_hsn"],
                  displayField: "strasse_hsn",
                  outfields: ["*"],
                  exactMatch: false,
                  name: "Street & number.",
                  placeholder: "Search for",
                  minSuggestCharacters: 0,
                  suggestionsEnabled: true,
                  maxSuggestions: 20,
                  resultSymbol: {
                                    type: "simple-marker",  // autocasts as new SimpleMarkerSymbol()
                                    style: "circle",
                                    color: [161,14,210,0.5],
                                    size: "20px",  // pixels
                                    outline: {  // autocasts as new SimpleLineSymbol()
                                    color: [161,14,210,0.5],
                                    width: 1  // points
                                    }
                                  }
                },

0 Kudos
4 Replies
BirajaNayak
Esri Contributor

Hi Ulrich,

orderByFields is available in Query not in Search. Here is the webhelp:

Query | API Reference | ArcGIS API for JavaScript 4.10 

I have a sample code for your reference:

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<title>orderByFields Sample - 4.10</title>

<link rel="stylesheet" href="https://js.arcgis.com/4.10/esri/css/main.css">
<script src="https://js.arcgis.com/4.10/"></script>

<style>
html,
body,
#viewDiv {
padding: 0;
margin: 0;
height: 100%;
width: 100%;
}
</style>

<script>
require([
"esri/Map",
"esri/views/MapView",
"esri/layers/FeatureLayer"
],
function(
Map, MapView,
FeatureLayer
) {

var map = new Map({
basemap: "hybrid"
});

var view = new MapView({
container: "viewDiv",
map: map,

extent: { // autocasts as new Extent()
xmin: -9177811,
ymin: 4247000,
xmax: -9176791,
ymax: 4247784,
spatialReference: 102100
}
});

/********************
* Add feature layer
********************/

// Carbon storage of trees in Warren Wilson College.
var featureLayer = new FeatureLayer({
url: "https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0",
outFields:["*"]
});

map.add(featureLayer);
view.when(function(){
let query = featureLayer.createQuery();
query.outFields = ["Cmn_Name"];
query.returnGeometry = true;
query.orderByFields = ["Cmn_Name DESC"];
featureLayer.queryFeatures(query).then(function(response){
for(let i = 0; i < response.features.length; i++){
let fid = response.features[i].attributes.Cmn_Name;
console.log(fid);
}
});
});

});
</script>
</head>

<body>
<div id="viewDiv"></div>
</body>

</html>
0 Kudos
ClarkLangridge
Occasional Contributor II

You need to wire it into the suggest-complete even handler for the widget (because why do common sense things in your developement?):

searchWidget.on("suggest-complete", function (evt) {

searchWidget.suggestions.forEach(function (item) {

//I have a lot of features with the same name and unique identifier we generated, but different Object IDs.  To keep the list short, I'm filtering it to only have one occurrence of each name
   let temp = [];

//For each set of results for each layer searched, check to see if you've already found a feature with the same display name
   item.results.forEach(function (entry) {
      if (!temp.some(function (nextItem) {
            return nextItem.text == entry.text
      })) temp.push(entry);
   });//end of 2nd forEach

//Take the results and sort the list
   temp.sort(function (a, b) {
      return a.text.localeCompare(b.text);
   });//end of sort function

//Assign the sorted array back to the results attribute.
   item.results = temp;
   });//end of forEach

});

Shane_
by
New Contributor II

Hi, can you sort the 4.14 js api search widget to display exact matches first?

Thanks,

Shane

NaveedAhmed2
New Contributor III

I have same issue as described by Shane above

0 Kudos