Select to view content in your preferred language

Changing where clause for createQuery()

1044
11
08-08-2023 09:21 PM
Charan
by
New Contributor III

Hi everyone, I'm trying to download statistics for a drawn buffer. I'm able to do that but if I change my year (the where clause) instead of downloading the data for the new year, it keeps using the same year. For instance, if I select 2020, it will download the data statistics using 2020 year but then if I switch to the 2021 year it still downloads 2020. 

 

 

let querycentroids = layer.createQuery();
querycentroids.spatialRelationship = "contains";
querycentroids.geometry = buffgra.geometry;
$(".chosen-benchmark")
    .chosen()
    .change(function () {
      var option = [$("#YearSource").val()];
      var result = "'" + option.join("','") + "'";
      querycentroids.where="year IN (" + result + ")"
      if ($(".chosen-benchmark").val() == 'Toronto') {
        console.log('Toronto')
        if ($(".chosen-year").val() == '2020') {
          console.log('works')
          document.getElementById("report").onclick = () => {
          downloadData(Toronto_2020,option)

          }
        }
        else if ($(".chosen-year").val() == '2021') {
          console.log('works2021')          
          document.getElementById("report").onclick = () => {
          downloadData(Toronto_2021,option)

          }
        }
        else if ($(".chosen-year").val() == '2022') {
          console.log('works2022')          
          document.getElementById("report").onclick = () => {
          downloadData(Toronto_2022,option)

          }
        }
        $(".chosen-year")
        .chosen()
        .change(function () {
          var yearoptions2 = [$("#YearSource").val()];
          var yearresult2 = "'" + yearoptions2.join("','") + "'";
          querycentroids.where="year IN (" + yearresult2 + ")"

          if ($(".chosen-year").val() == '2020') {
            console.log('2020')
            document.getElementById("report").onclick = () => {
              downloadData(Toronto_2020,yearoptions2)

            }
          }
          else if ($(".chosen-year").val() == '2021') {
            console.log('2021')
            document.getElementById("report").onclick = () => {
              downloadData(Toronto_2021,yearoptions2)

            }
          }
          else if ($(".chosen-year").val() == '2022') {
            console.log('2022')
            document.getElementById("report").onclick = () => {
              downloadData(Toronto_2022,yearoptions2)

            }
          }
        })
      }
      else if ($(".chosen-benchmark").val() == 'BC') {
        //// same stuff

      }
    });

  });

 

 

@UndralBatsukh @JoelBennett @KenBuja 

0 Kudos
11 Replies
KenBuja
MVP Esteemed Contributor

Are you checking your console to see if the if statements in line 12, 19, 26, 40, 47, or 54 are being evaluated properly? Is the field you're querying a text field or a numeric field? Should the query be "year in (2021, 2022)" or "year in ('2021', '2022')"?

 

0 Kudos
Charan
by
New Contributor III

Hi, @KenBuja my console.log() for the above lines is working correctly, when I select the year 2020 and then select Toronto, it prints Toronto and works (lines 11 and 13). and when I switch to 2021 it prints 2021(line 48). The year field is numeric. For queries, it should only take one value at a time, so if I do 2020 it should be year = 2020, but if I switch my select option to 2021 it should be year=2021

0 Kudos
Charan
by
New Contributor III

Is there a way to rest queries, because the if statement seems to be working but the .where= doesn't seem to reset

0 Kudos
KenBuja
MVP Esteemed Contributor

You're adding quotes to the where clause, which is causing the problem. The method val is probably also returning a string, so I'm converting it to an integer, also.

var option = ParseInt([$("#YearSource").val()]);
//var result = "'" + option.join("','") + "'";
querycentroids.where="year = " + option; 

 

0 Kudos
Charan
by
New Contributor III

I changed my code using this but my query is not resetting. Please see the code

0 Kudos
KenBuja
MVP Esteemed Contributor

I had a small mistake in the code. It should be "parseInt".

When you run the query after changing the year, have you checked the where clause to make sure it's correct?

0 Kudos
Charan
by
New Contributor III

yup I fixed the parseInt, and when I switch to the year, I checked the console and I see where the clause becomes where: "year = 2021".

 

0 Kudos
KenBuja
MVP Esteemed Contributor

From what I can tell in the code (I can't run it due to a permissions problem), it looks like you're running the querycentroids query before you reset the year. Where is the query run again after you change the where clause?

0 Kudos
Charan
by
New Contributor III

Im able to download the correct data now, the only problem is if i draw multiple buffers on the map it will only give the data for the first drawn buffer, please see the updated code: https://codepen.io/Charanb98/pen/wvYdQYV

 

0 Kudos