Hello,
I have been trying to query a layer and show the results in the map but I can't make it work, this is my try:
define([ 'jimu/BaseWidget', 'dojo/_base/connect', 'dojo/_base/declare', 'dojo/_base/lang', 'dojo/_base/array', 'dojo/_base/html', 'dojo/dom', 'dojo/on', 'dojo/query', 'esri/tasks/QueryTask', 'dijit/registry', 'esri/layers/FeatureLayer', 'esri/layers/Field','esri/request', 'dojo/store/Memory','jimu/LayerInfos/LayerInfos' ],
function (BaseWidget, connect, declare, lang, array, html, dom, on, Query, QueryTask, registry, FeatureLayer, field, esriRequest, Memory, LayerInfos) { return declare([BaseWidget], {
baseClass: 'jimu-widget-mywidget',
startup: function () {
this.inherited(arguments);
console.log('startup');
var map = this.map;
queryTask = new QueryTask("http://10.81.6.16:6080/arcgis/rest/services/MyLayer/FeatureServer/0");
query = new Query();
query.returnGeometry = true;
query.outFields = ["OBJECTID"];
if (map.loaded) {
query.where = "OBJECTID = 1";
queryTask.execute(query, showResults);
} else {
map.on("load", function () {
query.where = "OBJECTID = 1";
queryTask.execute(query, showResults);
});
}
function showResults(featureSet) {
map.graphics.clear();
//Performance enhancer - assign featureSet array to a single variable.
var resultFeatures = featureSet.features;
//Loop through each feature returned
for (var i=0, il=resultFeatures.length; i<il; i++) {
//Get the current feature from the featureSet.
//Feature is a graphic
var graphic = resultFeatures;
graphic.setSymbol(symbol);
//Set the infoTemplate.
graphic.setInfoTemplate(infoTemplate);
//Add graphic to the map graphics layer.
map.graphics.add(graphic);
}
}
},
(......)
I get this mistake:
TypeError: a.toJson is not a function
...nction(){},onExecuteRelationshipQueryComplete:function(){},onExecuteForIdsComple...
Can any one tell me if I am using query and querytask in the correct way? I have been following other threats of this forum to write this piece of code.
Thanks!
Marga
Solved! Go to Solution.
Marga,
The first thing I see wrong in your code is that you are using a 'dojo/query' instead of 'esri/tasks/Query'. If you swap that require you should try your code again.
Marga,
The first thing I see wrong in your code is that you are using a 'dojo/query' instead of 'esri/tasks/Query'. If you swap that require you should try your code again.
Hi Robert,
Thanks!
I have modified my code and now I don't get that error and works fine!
Is it neccesary to write code for showing the results in the map, I mean, in this case is it neccesary the showResults function? Because is the only way that I have found to do that, but.. maybe are there other ways?
Marga
Hello,
I am also wondering if is it possible to show de results (the featureset get from applying the querytask) in the attribute table widget.
I have read in the documentation of esri this: "Send a layer to the Attribute Table widget: The following steps demonstrate how to send a layer from your widget to the Attribute Table widget. Since the attribute table only displays layers in the map, make sure to add the layer to the map." (Send a layer to the Attribute Table widget—Web AppBuilder for ArcGIS (Developer Edition) | ArcGIS fo... ). But the only thing that I get from runing the querytask is a featureset.
And I have done some research in the wab community, I have found this thread related to this:
https://community.esri.com/message/616240?commentID=616240#comment-621609
In which is mentioned how to apply a filter to the attribute table, so I definitely want to have my query results in the attribute table, so maybe I have better to follow that idea instead of trying with the query and querytask right?
Marga
Marga,
If you want to add the query results to the map then you have to take the results and create a FeatureLayer from the results and add that to the map.
FeatureLayer | FeatureCollection | API Reference | ArcGIS API for JavaScript 3.17
Or as you said it may be easier to just filter the data in the attribute table.
Hello!
I finally decided to create a featureclass because I will need to use 'esri/tasks/Query' to make some spatial queries and as far as I know I can't filter from the AT widget with spatial criteria. So I have create my featureclass from the featureset with no problem and add to the map to see in a graphic way the layer (this is working fine):
//Create the layer when the querytask is launched
var layerDefinition = {
"geometryType": featureSet.geometryType,
"fields": featureSet.fields
}
var featureCollection = {
layerDefinition: layerDefinition,
featureSet: featureSet
};
layerResult = new FeatureLayer(featureCollection, {
showLabels: true
});
layerResult .name = 'L_Result';
map.addLayer(layerResult );
The query criterias are selected from a window, and can be launched as much as the user wants, so I realize that I need to remove the layerResult from the map before adding the new one with the new query criteria (otherwise I finished with a lot of layers added to the map). I do that with this piece of code:
// Remove layerResult before creating a new one
if (typeof (layerResult ) !== 'undefined') {
map.removeLayer(layerResult);
}
Maybe I am nor removing this layer in a proper way because this layer its removed from the map BUT when I openned the attribute table after launching for example 3 queries, there are 3 LayerResul (one from each result) so its clear that it hasn't been removed from the attribute table.. Any ideas about why is this layer still in the attribute tables?
I have been doing some research with layerTabPageClose method (AttributeTable/Widget.js) but with no succeed. I have also thought about destroying this widget and reload it but I think that this is not a suitable solution.
Any ideas?
Marga
Marga,
This is how I do it:
if (this.tempResultLayer) {
this.map.removeLayer(this.tempResultLayer);
}
this.tempResultLayer = null;