Select to view content in your preferred language

Obtaining unique Values based on user Input using Query()

704
4
Jump to solution
02-03-2023 07:12 AM
TheGamer
Occasional Contributor

 

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*)

The problem is it works but sometimes it duplicates the last stored id and adds it again. So, for instance, if the current max id is 72, and I add keep adding data, at some random point, the id repeats itself when i add new data. There are also times when I will randomly get 1 even if I have the animal is in the data.
 
 
0 Kudos
1 Solution

Accepted Solutions
UndralBatsukh
Esri Regular Contributor

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. 

View solution in original post

0 Kudos
4 Replies
UndralBatsukh
Esri Regular Contributor

Is the animal element a drop-down with predefined values? Or is a text input? 

 

 

 

0 Kudos
TheGamer
Occasional Contributor

Hi, it's a text input

0 Kudos
UndralBatsukh
Esri Regular Contributor

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. 

0 Kudos
TheGamer
Occasional Contributor

Thank you, I fixed the issue by using predefined values

0 Kudos