Issues with creating a definitionExpression ArcGIS API for Javascript

978
7
05-22-2017 02:39 PM
JacobTodd2
New Contributor III

I am having issues setting a definitionExpression based on OnDemandGrid row selection with ArcGIS API for Javascript. I keep getting an error stating that definitionExpression is not a function and I don't understand why. Below is my code:

var selectedRows = [];
          grid.on('dgrid-select', function (event) {
              var stateName = event.rows[0].data.stateName;
              var rows = event.rows;
              for (i = 0; i < rows.length; i++) {
                  selectedRows.push(stateName);
              }

              for (i = 0; i < selectedRows.length; i++) {
                  stateLayer.definitionExpression('STATE_NAME = "' + i + '"');
              }
              console.log(selectedRows);
          });
0 Kudos
7 Replies
RobertScheitlin__GISP
MVP Emeritus

Jacob,

   It look like your code should be:

stateLayer.definitionExpression("STATE_NAME = '" + selectedRows[i] + "'");

Assuming the state name field is looking for a string and not an integer like you currently have it. I also swapped your single and double quotes around.

0 Kudos
JacobTodd2
New Contributor III

Hi Robert,

Thanks for looking at this. I think you are right about making sure I am calling a string, but unfortunately that change didn't fix the definition expression error.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jacob,

   I am kind of confused by you code on closer examination. Why are you looping twice?

          var selectedRows = [];
          grid.on('dgrid-select', function (event) {
              var stateName = event.rows[0].data.stateName;
              var rows = event.rows;
              for (i = 0; i < rows.length; i++) {
                  selectedRows.push(stateName);
              }

              for (i = 0; i < selectedRows.length; i++) {
                  stateLayer.definitionExpression('STATE_NAME = "' + i + '"');
              }
              console.log(selectedRows);
          });

//Why not

          grid.on('dgrid-select', function (event) {
              var stateName = event.rows[0].data.stateName;
              stateLayer.definitionExpression("STATE_NAME = '" + stateName  + "'");
          });
0 Kudos
JacobTodd2
New Contributor III

Hi Robert, 

The reason I was looping twice was to create a list of the selected rows in my grid. My end goal with this is to dynamically create a definition expression out of the selected rows. As rows are selected and the definition expression is created, I would like my map to zoom and center on envelope (extent) of my selections. I know that this is a lot to do at once, but any suggestions would be great. By the way I am using 4.3.

Thanks,

Jacob

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jacob,

   Then it would be something more like this:

          grid.on('dgrid-select', function (event) {
              var stateName; = event.rows[0].data.stateName;
              var rows = event.rows;
              for (i = 0; i < rows.length; i++) {
                  stateName = rows[i].data.stateName;
                  selectedRows.push("'" + stateName + "'");
              }
              stateLayer.definitionExpression("STATE_NAME IN(" + selectedRows.join(',') + ")");
              console.log(selectedRows);
          });
0 Kudos
JacobTodd2
New Contributor III

Robert,

That worked great. What do you suggest out zooming to the extent of the selection?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Jacob,

   Query the layer for extent using the SQL statement previously.

https://developers.arcgis.com/javascript/latest/api-reference/esri-tasks-QueryTask.html#executeForEx... 

0 Kudos