Hello All,
I'm using Web App Builder 2.2 Developer Edition.
Once feature is captured using Edit Widget, I want to apply attributes from the spatial query before apply edits. I have written my code as below to achieve this but query gets completed after attribute inspector is opened.
_canDeleteSelectionFeatures: function () {
// return ture if all features can be deleted,
// else return false.
var canDeleteFeatures = true;
var selectionFeatures = this._getSelectionFeatuers();
if(selectionFeatures.length === 0) {
canDeleteFeatures = false;
} else {
array.some(selectionFeatures, function (feature) {
var featureLayer = feature.getLayer && feature.getLayer();
//I have written below line
on(featureLayer, 'before-apply-edits', lang.hitch(this, this.onBeforeApplyEdits))
if (!featureLayer ||
!featureLayer.getEditCapabilities({ feature: feature }).canDelete
) {
canDeleteFeatures = false;
return true;
}
}, this);
}
return canDeleteFeatures;
},
and then onBeforeApplyEdits, I have done spatial query to get the required attribute as below:
onBeforeApplyEdits: function (evt) {
if (evt.adds != null) {
var selectionFeatures = this._getSelectionFeatuers();
array.some(selectionFeatures, lang.hitch(this,function (feature) {
var queryDivsionName = new Query();
queryDivsionName.returnGeometry = false;
queryDivsionName.outFields = ["division"];
queryDivsionName.geometry = feature.geometry;
queryDivsionName.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
var queryDivsionNameTask = new QueryTask(divsionLayer.url);
execute(queryDivsionName, lang.hitch(this, function (featureSet) {
console.log(featureSet);
divisionName = featureSet.features[0].attributes.division;
}));
}));
evt.adds[0].attributes.division = divisionName ;
}
Please suggest how to spatial query for attribute and apply to feature before apply edits.
Regards,
Krish
Solved! Go to Solution.
Krish,
This will work:
onBeforeApplyEdits: function (evt) {
if (evt.adds != null) {
var feat = evt.adds[0];
var queryDivsionName = new Query();
queryDivsionName.returnGeometry = false;
queryDivsionName.outFields = ["division"];
queryDivsionName.geometry = feat.geometry;
queryDivsionName.spatialRelationship = Query.SPATIAL_REL_INTERSECTS;
var queryDivsionNameTask = new QueryTask(divsionLayer.url);
execute(queryDivsionName, lang.hitch(this, function (featureSet) {
feat.attributes.division = featureSet.features[0].attributes.division;
setTimeout(lang.hitch(this, function(){
evt.target.applyEdits(null, [feat], null);
}), 100);
}));
}
},
Now, my widget is working as expected after adding setTimeout function.
Thank you so much!
Regards,
Krish
HI,
I want to add the code above to my edit widget - where are you adding those code?
Also where have you specified the attributes that you're saving?
Thanks