Get searchWidget Result as json object

857
5
08-10-2017 02:05 AM
Henryobaseki
Occasional Contributor

Hi Again,

My searchwidget is working alright, it displays the search with a popup template. The problem now is to get the result of the search as a json  object  or a table.

I have search the api references, and its the search-complete that returns search as an object. my code isn't right. Any help please see below,

// Search
 
  var search = new Search({
    view: view
  });
  search.defaultSource.withinViewEnabled = true; // Limit search to visible map area only
  view.ui.add(search, "top-right"); // Add to the map

  // Add the trailheads as a search source
  search.sources.push({
    featureLayer: trailsLayer,
     searchFields: ["Postcode", "UPRN", "ADDRESS" ],
  displayField: "Postcode",
  exactMatch: false,
  outFields: ["*"],
   resultGraphicEnabled: true,
    name: "Postcode",
  placeholder: "example:  CM2 0HU",
    popupTemplate: { // autocasts as new popupTemplate()
        title: "Post Code: {Postcode}</br>UPRN: {UPRN}</br>ADDRESS: {ADDRESS}",
        overwriteActions: true
     
      }
  });
     
  search.sources.push({
    featureLayer: trailsLayerr,
     searchFields: ["Postcode", "UPRN", "ADDRESS" ],
  displayField: "Postcode",
  exactMatch: false,
  outFields: ["*"],
   resultGraphicEnabled: true,
    name: "Postcode",
  placeholder: "example:  CM2 0HU",
    popupTemplate: { // autocasts as new popupTemplate()
        title: "Post Code: {Postcode}</br>UPRN: {UPRN}</br>ADDRESS: {ADDRESS}",
        overwriteActions: true
     
      }
  });
 
  function searchcomplete( search-complete) {
    view.popup.open({   
activeSourceIndex: Number,
errors: Error[],
numResults: Number,
searchTerm: String,
results: Object[],

}

 
    });
 
 
searchWidget.on("search-complete", function(event){
  // The results are stored in the event Object[]
  console.log("Results of the search: ", event);
});

});
 
 
 
 
 
 

});</script>
</head>
<body>
  <div id="viewDiv"></div>
Tags (1)
0 Kudos
5 Replies
RobertScheitlin__GISP
MVP Emeritus

Henry,

    Are you getting to the console.log("Results of the search: ", event); If so then you just need to change your handler for the search-complete.

searchWidget.on("search-complete", searchcomplete);
0 Kudos
Henryobaseki
Occasional Contributor

ok have made the changes does this seem right

searchWidget.on("search-complete", function(searchcomplete){
  // The results are stored in the event Object[]
  console.log("Results of the search: ", searchcomplete);
});
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Henry,

  Nope use the code I provided exactly.

0 Kudos
Henryobaseki
Occasional Contributor

ok, see below

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no">
<title>DevLabs - Search and Geocode</title>
<style>
  html, body, #viewDiv {
    padding: 0;
    margin: 0;
    height: 100%;
    width: 100%;
  }
</style>
  <link rel="stylesheet" href="https://js.arcgis.com/4.3/esri/css/main.css">
<script src="https://js.arcgis.com/4.3/"></script>
 
  <script>
require([
  "esri/Map",
  "esri/views/MapView",
  "esri/widgets/Search",
  "esri/layers/FeatureLayer",
  "dojo/domReady!"
], function(Map, MapView, Search, FeatureLayer) {
 
  var map = new Map({
    basemap: "streets-vector"
  });
 
  // Add the layer to the map
  var trailsLayer = new FeatureLayer({

});
  //map.add(trailsLayer); // Optionally add layer to map

   // Add the layer to the map
  var trailsLayer2 = new FeatureLayer({
  });

map.add(trailsLayer2); // Optionally add layer to map
 
 

// Add the layer to the map
  var trailsLayer3 = new FeatureLayer({
  });
  map.add(trailsLayer3); // Optionally add layer to map
 
 
 
 
  var view = new MapView({
    container: "viewDiv"
    map: map,
   zoom: 19,
center: [0.46564130, 51.736810] // longitude, latitude
  });
 
 
 
 
  // Search
 
  var search = new Search({
    view: view
  });
  search.defaultSource.withinViewEnabled = false; // Limit search to visible map area only
  view.ui.add(search, "top-right"); // Add to the map

  // Add the trailheads as a search source
  search.sources.push({
    featureLayer: trailsLayer,
     searchFields: ["Postcode", "UPRN", "ADDRESS" ],
  displayField: "Postcode",
  exactMatch: false,
  outFields: ["*"],
   resultGraphicEnabled: true,
    name: "Postcode",
  placeholder: "example:  CM2 0HU",
    popupTemplate: { // autocasts as new popupTemplate()
        title: "Post Code: {Postcode}</br>UPRN: {UPRN}</br>ADDRESS: {ADDRESS}",
        overwriteActions: true
     
      }
  });
     
  search.sources.push({
    featureLayer: trailsLayer2,
     searchFields: [ "UPRN", "ADDRESS" ],
  displayField: "ADDRESS",
  exactMatch: false,
  outFields: ["*"],
   resultGraphicEnabled: true,
    name: "Inborough Addresses",
  placeholder: "example:  CM2 0HU",
    popupTemplate: { // autocasts as new popupTemplate()
        title: "Post Code: {Postcode}</br>UPRN: {UPRN}</br>ADDRESS: {ADDRESS}",
        overwriteActions: true
     
      }
  });
 
 
   search.sources.push({
    featureLayer: trailsLayer3,
     searchFields: ["WARD_N" ],
  displayField: "WARD_N",
  exactMatch: false,
  outFields: ["*"],
   resultGraphicEnabled: true,
    name: "Wards",
  placeholder: "example:  CM2 0HU",
    popupTemplate: { // autocasts as new popupTemplate()
        title: "Post Code: {Postcode}</br>UPRN: {UPRN}</br>ADDRESS: {ADDRESS}",
        overwriteActions: true
     
      }
  });
 
   function searchcomplete( search-complete) {
    view.popup.open({   
activeSourceIndex: Number,
errors: Error[],
numResults: Number,
searchTerm: String,
results: Object[],

});
 
  },

  searchWidget.on("search-complete", searchcomplete);

 
 
 
 
 

});</script>
</head>
<body>
  <div id="viewDiv"></div>
</body>
</html>
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Henry,

   Nope put this line right after you create the searchWidget.

i.e.

  var search = new Search({
    view: view
  });
  searchWidget.on("search-complete", searchcomplete);