-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #126 from Esri/feat/query-features
add queryFeatures() to send query requests to feature services
- Loading branch information
Showing
7 changed files
with
428 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
# Running this demo | ||
|
||
1. Make sure you run `npm run bootstrap` in the root folder to setup the dependencies | ||
1. `npm start` | ||
1. Visit http://localhost:8080 | ||
1. Enter a search term and click "Search" to see results |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,122 @@ | ||
<!DOCTYPE html> | ||
<html> | ||
<head> | ||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous"> | ||
</head> | ||
<body> | ||
|
||
<div class="container-fluid"> | ||
<div class="row"> | ||
<div class="col-md-12"> | ||
<div class="jumbotron" > | ||
<h1>query features!</h1> | ||
<form id="queryForm" class="form-inline"> | ||
<div class="form-group"> | ||
<label for="resultRecordCount">Show up to</label> | ||
<select id="resultRecordCount" class="form-control"> | ||
<option>5</option> | ||
<option selected>10</option> | ||
<option>25</option> | ||
<option>50</option> | ||
</select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="queryField">records where</label> | ||
<select id="queryField" class="form-control"> | ||
<option value="Cmn_Name">Type</option> | ||
<option value="Condition">Condition</option> | ||
</select> | ||
</div> | ||
<div class="form-group"> | ||
<label for="queryTerm">contains</label> | ||
<input type="text" class="form-control" id="queryTerm" style="width: 200px" tabindex="0"> | ||
</div> | ||
<button type="submit" class="btn btn-default">Go</button> | ||
</form> | ||
</div> | ||
</div> | ||
</div> | ||
<div class="row"> | ||
<div class="col-md-12"> | ||
<table id="featureTable" class="table table-striped" style="display: none"> | ||
<thead> | ||
<tr> | ||
<th>Tree ID</th> | ||
<th>Type</th> | ||
<th>Condition</th> | ||
</tr> | ||
</thead> | ||
<tbody id="tableBody"> | ||
</tbody> | ||
</table> | ||
<p id="suggestedTermsMessage">Try 'elm' or 'oak' for <strong>Type</strong>. Try 'fair' or 'good' for <strong>Condition</strong>.</p id="suggestedTermsMessage"> | ||
<p id="recordCountMessage"></p> | ||
<p id="additionalRowsMessage" style="display: none" class="alert alert-warning">There are additional rows that meet your query criteria.</p> | ||
</div> | ||
</div> | ||
</div> | ||
|
||
<!-- load arcgis rest libraries --> | ||
<script src="node_modules/@esri/arcgis-rest-request/dist/umd/arcgis-rest-request.umd.js"></script> | ||
<script src="node_modules/@esri/arcgis-rest-feature-service/dist/umd/arcgis-rest-feature-service.umd.js"></script> | ||
|
||
<script> | ||
// respond when a user fills out the query form and | ||
// either hits enter or clicks on the button | ||
var queryForm = document.getElementById('queryForm'); | ||
queryForm.addEventListener('submit', function (e) { | ||
// don't submit the form and reload the page (the default behavior) | ||
e.preventDefault(); | ||
// get query params from form fields | ||
var queryField = e.target.queryField.value; | ||
var queryTerm = e.target.queryTerm.value; | ||
var resultRecordCount = e.target.resultRecordCount.value; | ||
// execute the query | ||
queryTrees(queryField, queryTerm, resultRecordCount) | ||
.then(function(response) { | ||
// display the features in a table | ||
refreshTable(response); | ||
}); | ||
}); | ||
|
||
// perform query against the feature service and | ||
// return a promise that will resolve with the response | ||
function queryTrees(queryField, queryTerm, resultRecordCount) { | ||
return arcgisRest.queryFeatures({ | ||
url: 'https://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/Landscape_Trees/FeatureServer/0', | ||
// see: https://developers.arcgis.com/rest/services-reference/query-feature-service-layer-.htm | ||
// for all possible query parameters | ||
params: { | ||
// NOTE: returnGeometry is set to false by default | ||
where: queryField + ' LIKE \'%' + queryTerm + '%\'', | ||
outFields: ['FID','Tree_ID','Cmn_Name','Condition'], | ||
// limit to number of records that will show on the page | ||
resultRecordCount: resultRecordCount | ||
} | ||
}); | ||
} | ||
|
||
function refreshTable(response) { | ||
var features = response.features; | ||
// clear table | ||
var tableBody = document.getElementById('tableBody'); | ||
tableBody.innerHTML = ''; | ||
// show returned features (if any) | ||
var recordCount = features.length; | ||
var featureTableDisplay = recordCount > 0 ? 'table' : 'none'; | ||
document.getElementById('featureTable').style.display = featureTableDisplay; | ||
var rows = features.map(function (feature) { | ||
return '<tr><td>' + feature.attributes.Tree_ID + '<td>' + feature.attributes.Cmn_Name + '</td><td>' + feature.attributes.Condition + '</td></tr>'; | ||
}); | ||
tableBody.innerHTML = rows.join(''); | ||
// show number of returned features | ||
document.getElementById('recordCountMessage').innerHTML = recordCount + ' record(s) returned.'; | ||
// show/hide additional messages | ||
var suggestedTermsMessageDisplay = recordCount > 0 ? 'none' : 'block'; | ||
var additionalRowsMessageDisplay = response.exceededTransferLimit ? 'block' : 'none'; | ||
document.getElementById('suggestedTermsMessage').style.display = suggestedTermsMessageDisplay; | ||
document.getElementById('additionalRowsMessage').style.display = additionalRowsMessageDisplay; | ||
} | ||
</script> | ||
</body> | ||
</html> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
{ | ||
"name": "feature-service-browser", | ||
"version": "1.0.3", | ||
"private": true, | ||
"description": "Vanilla JavaScript demo of @esri/arcgis-rest-feature-service", | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"dependencies": { | ||
"@esri/arcgis-rest-request": "^1.0.3", | ||
"@esri/arcgis-rest-feature-service": "^1.0.3" | ||
}, | ||
"devDependencies": { | ||
"http-server": "*" | ||
}, | ||
"scripts": { | ||
"start": "http-server ." | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.