Hi everyone Im having trouble getting the last element of the unique values
const layer = FeatureLayer({
url: "",
});
const layer2 = FeatureLayer({
url: "",
refreshInterval:0.5
});
var id;
document.getElementById("animal").addEventListener("change", () => {
var arr=[];
const query= layer.createQuery();
query.where = "Animal= '" + document.getElementById("animal").value +"'";
query.returnGeometry = false;
query.returnDistinctValues = true;
query.outFields=["ID", "Animal"];
layer.queryFeatures(query).then(function(response){
const values = response.features;
if (values.length >0) {
for (var i =0; i < values.length; i++) {
arr[i] = values[i].attributes['ID'];
}
id= parseInt(arr.at(arr.length-1))+1
}
else {
id= 1;
}
});
});
document.getElementById("createdata").onclick = () => {
const long = document.getElementById("LONG").value;
const lat = document.getElementById("LAT").value;
const point = {
type: "point",
longitude: long,
latitude: lat,
};
const simpleMarkerSymbol = {
type: "simple-marker",
color: [226, 119, 40],
outline: {
color: [12, 137, 148],
width: 0.5,
},
};
const pointGraphic = new Graphic({
geometry: point,
symbol: simpleMarkerSymbol,
attributes: {
Name: document.getElementById("name").value,
Animal: document.getElementById("animal").value,
ID: id
},
});
layer2.applyEdits({ addFeatures: [pointGraphic] });
document.getElementById("animal").value = "";
document.getElementById("name").value = "";
};
<div class="esri-widget">
<input id="city" size="15" />
</div>
So I'm trying to generate new ids and add them to the feature layer. In order to do so I want to see if the animal inputted by the user exists in the data, if it does it should get the unique values, extract the last unique id (max id) and add 1 to it. But, if the animal doesn't exist in the data, it should assign id =1.
The id should then be assigned as an attribute to the graphic, which is then added to the layer2 by doing: layer2.applyEdits({ addFeatures: [pointGraphic] }); (* layer2 is a feature layer view of layer*)
Solved! Go to Solution.
Probably that is the issue then. You are running the query every time user enters a character. Can you please change your logic so that you are running the query when user hits enter button in the input? Or add a button and run the query from the button's click event.
Is the animal element a drop-down with predefined values? Or is a text input?
Hi, it's a text input
Probably that is the issue then. You are running the query every time user enters a character. Can you please change your logic so that you are running the query when user hits enter button in the input? Or add a button and run the query from the button's click event.
Thank you, I fixed the issue by using predefined values