Select to view content in your preferred language

query.where syntax issue

1150
12
Jump to solution
04-09-2014 06:53 AM
williamcarr
Frequent Contributor
2 part question:

The first part is getting the value of
  <select class="field" name="field" id="selectfield">        <option value="FIRSTNAME">First Name</option>   <option value="LAST_NAME">Last Name</option>   <option value="Service_ID">Service ID</option>   <option value="TOWNSHIP">Township</option> </select>


to be a variable in a query.where

var sel = dom.byId("input1").value; query.where = "  (select option variable) LIKE'" + sel +"'"; 


Essentially we need the user to select the query field in from the drop down menu. I'm hoping this is possible and I'm just running into syntax issues.

The second part is making the query accept partial or misspelled text inputs. I'm new to JS and this is really racking my brain. Any help is appreciated.
0 Kudos
1 Solution

Accepted Solutions
JeffPace
MVP Alum
ok there is two options

if you want to use the dijit, you have to use the parser and call parser.parse() to make the dijits.  This will allow you to use the registry.

the other option is to go back to what you had and just to get the value from the select

I would advise option 1 because it will be more useful in the long run, but will be more work. 

However, the easy answer (option2)
<!DOCTYPE html> <html>   <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <!--The viewport meta tag is used to improve the presentation and behavior of the samples        on iOS devices-->     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">     <title>Query State Info without Map</title>      <script src="http://js.arcgis.com/3.8/"></script>     <script>       require([         "esri/tasks/query", "esri/tasks/QueryTask","dijit/registry",         "dojo/dom", "dojo/on", "dojo/domReady!"       ], function (Query, QueryTask,registry, dom, on) {          var queryTask = new QueryTask("secure");          var query = new Query();         query.returnGeometry = false;         query.outFields = [           "*"         ];          on(dom.byId("execute"), "click", execute);            function execute () {          console.log("whooooaaaa");                      var sel = dom.byId("input1").value;      console.log(sel);          var option= dom.byId("selectfield");    var optionValue = option.options[option.selectedIndex].value;    console.log(optionValue);                      query.where = optionValue + "LIKE '%" + sel +"%'";                       queryTask.execute(query, showResults);         }          function showResults (results) {           var resultItems = [];           var resultCount = results.features.length;           for (var i = 0; i < resultCount; i++) {             var featureAttributes = results.features.attributes;             for (var attr in featureAttributes) {               resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");             }             resultItems.push("<br>");           }           dom.byId("info").innerHTML = resultItems.join("");           dom.byId("info1").innerHTML = resultCount;         }       });     </script>   </head>    <body>    <select data-dojo-type="dijit/form/Select" class="field" name="selectfield" id="selectfield">        <option value="FIRSTNAME">First Name</option>   <option value="LAST_NAME">Last Name</option>   <option value="Service_ID">Service ID</option>   <option value="TOWNSHIP">Township</option> </select> Total records:    <span id="info1">0</span> Total records:       US state name :     <input type="text" id="input1" value="California">     <input id="execute" type="button" value="Get Details">     <br />     <br />     <div id="info" style="padding:5px; margin:5px; background-color:#eee;">     </div>   </body> </html>

View solution in original post

0 Kudos
12 Replies
JeffPace
MVP Alum
var optionValue= registry.byId("selectfield").get('value');


1. your select element is id'd "selectfield"
2. You need to use registry (not dom) since you want the value of the DIJIT, not the HTML ELEMENT


see some more info here http://stackoverflow.com/questions/1850050/how-to-get-the-value-of-a-filteringselect-select-in-dojo

notice that is legacy style (dijit.byId) and since you are using AMD you want to use registry.byId



Second Part

ESRI now uses standardized queries to make it database agnostic.  Supported SQL queries are

http://resources.arcgis.com/en/help/main/10.2/index.html#//015400000686000000

so your where clause should be something like

query.where = optionValue  + " LIKE '%" + sel +"%'";

this handles partials (  'ea' would return team) but not mispellings.  For that you would need a soundex on the field you are querying in the database, it could not be handled in the application without submitting excessive amounts of individual queries for each possibility.
0 Kudos
williamcarr
Frequent Contributor
Appreciate the help. Perhaps you could help me a little more.
I keep getting "registry:byID(.........) is undefined".

     function execute () {
         
         
           var sel = dom.byId("input1").value;
         var optionValue= registry.byId("selectfield").get('value');
      
          
          query.where =optionValue + " LIKE'%" + sel +"%'"; 
          
          queryTask.execute(query, showResults);
        }


require([
        "esri/tasks/query", "esri/tasks/QueryTask","dijit/registry",
        "dojo/dom", "dojo/on", "dojo/domReady!"
      ], function (Query, QueryTask,registry, dom, on)


Any ideas as to where I screwed up this time?
0 Kudos
JeffPace
MVP Alum
from the error, it looks like you used registry.byID not registry.byId
0 Kudos
williamcarr
Frequent Contributor
Just checked, wasn't that..

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Query State Info without Map</title>

    <script src="http://js.arcgis.com/3.8/"></script>
    <script>
      require([
        "esri/tasks/query", "esri/tasks/QueryTask","dijit/registry",
        "dojo/dom", "dojo/on", "dojo/domReady!"
      ], function (Query, QueryTask,registry, dom, on) {

        var queryTask = new QueryTask("secure");

        var query = new Query();
        query.returnGeometry = false;
        query.outFields = [
          "*"
        ];

        on(dom.byId("execute"), "click", execute);
  
        function execute () {
         console.log("whooooaaaa");
         
           var sel = dom.byId("input1").value;
         var optionValue = registry.byId("selectfield").item.value;
      
          
          query.where = optionValue + "LIKE '%" + sel +"%'"; 
          
          queryTask.execute(query, showResults);
        }

        function showResults (results) {
          var resultItems = [];
          var resultCount = results.features.length;
          for (var i = 0; i < resultCount; i++) {
            var featureAttributes = results.features.attributes;
            for (var attr in featureAttributes) {
              resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");
            }
            resultItems.push("<br>");
          }
          dom.byId("info").innerHTML = resultItems.join("");
          dom.byId("info1").innerHTML = resultCount;
        }
      });
    </script>
  </head>

  <body>
   <select class="field" name="field" id="selectfield">
    
  <option value="FIRSTNAME">First Name</option>
  <option value="LAST_NAME">Last Name</option>
  <option value="Service_ID">Service ID</option>
  <option value="TOWNSHIP">Township</option>
</select>
Total records: 
  <span id="info1">0</span>
Total records: 

    US state name :
    <input type="text" id="input1" value="California">
    <input id="execute" type="button" value="Get Details">
    <br />
    <br />
    <div id="info" style="padding:5px; margin:5px; background-color:#eee;">
    </div>
  </body>
</html>
0 Kudos
JeffPace
MVP Alum
try declaring the select as a dijit

<select data-dojo-type="dijit/form/Select"  class="field" name="field" id="selectfield">
0 Kudos
williamcarr
Frequent Contributor
It is still throwing  a registry:byId error.
It is claiming that anything  on the optionValue is undefined.
0 Kudos
JeffPace
MVP Alum
ok there is two options

if you want to use the dijit, you have to use the parser and call parser.parse() to make the dijits.  This will allow you to use the registry.

the other option is to go back to what you had and just to get the value from the select

I would advise option 1 because it will be more useful in the long run, but will be more work. 

However, the easy answer (option2)
<!DOCTYPE html> <html>   <head>     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">     <!--The viewport meta tag is used to improve the presentation and behavior of the samples        on iOS devices-->     <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">     <title>Query State Info without Map</title>      <script src="http://js.arcgis.com/3.8/"></script>     <script>       require([         "esri/tasks/query", "esri/tasks/QueryTask","dijit/registry",         "dojo/dom", "dojo/on", "dojo/domReady!"       ], function (Query, QueryTask,registry, dom, on) {          var queryTask = new QueryTask("secure");          var query = new Query();         query.returnGeometry = false;         query.outFields = [           "*"         ];          on(dom.byId("execute"), "click", execute);            function execute () {          console.log("whooooaaaa");                      var sel = dom.byId("input1").value;      console.log(sel);          var option= dom.byId("selectfield");    var optionValue = option.options[option.selectedIndex].value;    console.log(optionValue);                      query.where = optionValue + "LIKE '%" + sel +"%'";                       queryTask.execute(query, showResults);         }          function showResults (results) {           var resultItems = [];           var resultCount = results.features.length;           for (var i = 0; i < resultCount; i++) {             var featureAttributes = results.features.attributes;             for (var attr in featureAttributes) {               resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");             }             resultItems.push("<br>");           }           dom.byId("info").innerHTML = resultItems.join("");           dom.byId("info1").innerHTML = resultCount;         }       });     </script>   </head>    <body>    <select data-dojo-type="dijit/form/Select" class="field" name="selectfield" id="selectfield">        <option value="FIRSTNAME">First Name</option>   <option value="LAST_NAME">Last Name</option>   <option value="Service_ID">Service ID</option>   <option value="TOWNSHIP">Township</option> </select> Total records:    <span id="info1">0</span> Total records:       US state name :     <input type="text" id="input1" value="California">     <input id="execute" type="button" value="Get Details">     <br />     <br />     <div id="info" style="padding:5px; margin:5px; background-color:#eee;">     </div>   </body> </html>
0 Kudos
JeffPace
MVP Alum
option #1 (with the parser), you will need to fix the styling

<!DOCTYPE html>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  <link rel="stylesheet" href="http://js.arcgis.com/3.9/js/esri/css/esri.css">
    <!--The viewport meta tag is used to improve the presentation and behavior of the samples 
      on iOS devices-->
    <meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no">
    <title>Query State Info without Map</title>

    <script src="http://js.arcgis.com/3.9/"></script>
    <script>
      require([
        "esri/tasks/query", "esri/tasks/QueryTask","dijit/registry",
        "dojo/dom", "dojo/on", "dojo/parser","dojo/domReady!"
      ], function (Query, QueryTask,registry, dom, on, parser) {
  parser.parse();
        var queryTask = new QueryTask("secure");

        var query = new Query();
        query.returnGeometry = false;
        query.outFields = [
          "*"
        ];

        on(dom.byId("execute"), "click", execute);
  
        function execute () {
         console.log("whooooaaaa");
         
           var sel = dom.byId("input1").value;
     console.log(sel);
         var optionValue= registry.byId("selectfield");
   console.log(optionValue);
          
          query.where = optionValue + "LIKE '%" + sel +"%'"; 
          
          queryTask.execute(query, showResults);
        }

        function showResults (results) {
          var resultItems = [];
          var resultCount = results.features.length;
          for (var i = 0; i < resultCount; i++) {
            var featureAttributes = results.features.attributes;
            for (var attr in featureAttributes) {
              resultItems.push("<b>" + attr + ":</b>  " + featureAttributes[attr] + "<br>");
            }
            resultItems.push("<br>");
          }
          dom.byId("info").innerHTML = resultItems.join("");
          dom.byId("info1").innerHTML = resultCount;
        }
      });
    </script>
  </head>

  <body>
   <select data-dojo-type="dijit/form/Select" class="field" name="selectfield" id="selectfield">
    
  <option value="FIRSTNAME">First Name</option>
  <option value="LAST_NAME">Last Name</option>
  <option value="Service_ID">Service ID</option>
  <option value="TOWNSHIP">Township</option>
</select>
Total records: 
  <span id="info1">0</span>
Total records: 

    US state name :
    <input type="text" id="input1" value="California">
    <input id="execute" type="button" value="Get Details">
    <br />
    <br />
    <div id="info" style="padding:5px; margin:5px; background-color:#eee;">
    </div>
  </body>
</html>
0 Kudos
williamcarr
Frequent Contributor
Unable to complete operation..... Wouldn't give me anymore than that.

Is that something on my end?
0 Kudos