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);
});
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.
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.
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 + "'");
});
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
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);
});
Robert,
That worked great. What do you suggest out zooming to the extent of the selection?
Jacob,
Query the layer for extent using the SQL statement previously.