Skip to content

Instantly share code, notes, and snippets.

@odoe

odoe/filter.js

Last active Oct 19, 2015
Embed
What would you like to do?
require([
'dojo/on',
'esri/core/Collection',
'esri/tasks/QueryTask',
'esri/tasks/support/Query'
], function (on, Collection, QueryTask, Query) {
var url = 'http://services2.arcgis.com/j80Jz20at6Bi0thr/arcgis/rest/services/Parks/FeatureServer/0';
var resultsNode = document.getElementById('resultsDiv');
var input = document.getElementById('autoComplete');
var qTask = new QueryTask({
url: url
});
var query = new Query({ returnGeometry: false, outFields: ['*'], where: '1=1' });
var cache = new Collection();
var collection = new Collection(cache);
cache.on('change', function () {
collection.clear();
collection.addItems(cache);
});
collection.on('change', function (e) {
resultsNode.innerHTML = '';
collection.map(function (x) {
var node = document.createElement('li');
node.setAttribute('class', 'list-group-item');
node.innerText = x.NAME;
resultsNode.appendChild(node);
});
});
qTask.execute(query).then(function (results) {
var attrs = results.features.map(function (x) { return x.attributes; });
// You could just do new Collection(attrs), but let's
// demonstrate some change events
cache.addItems(attrs);
});
on(input, 'keyup', function (e) {
var val = input.value.toLowerCase();
// filter the cache
var coll = cache.filter(function (x) {
return x.NAME.toLowerCase().indexOf(val) > -1;
});
collection.clear();
collection.addItems(coll);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment