Modifying the Query Widget

4873
9
Jump to solution
04-01-2015 12:44 PM
NicholasBarger
Occasional Contributor III

So I found the Query Widget to be useful, well about 75% useful.  So, while the widget functions correctly and works well the results are unable to be sorted.  This makes it difficult identifying the result that I want.  The main problem that I see with this is that it uses the objectID to sort the results (confirmed by esri tech support).

Thankfully they got me on the right track in coding the tool to be sorted.  We found that this code queryParams.orderByFields = ["FIELD_NAME"];

placed after line 828 directly following this code, queryParams.objectIds = objectIds; correctly sorted my first query in the widget.  However it failed when a second was added.

The tech supports advice was to add an if statements using the url / ID to differentiate the queries and sort each one accordingly.  So I found the following code around line 416 - 419 that parsed out the id number from the url.

------------------------

_getLayerIndexByLayerUrl: function(layerUrl){

var lastIndex = layerUrl.lastIndexOf("/");

var a = layerUrl.slice(lastIndex + 1, layerUrl.length);

return parseInt(a, 10);

I used the variable parseInt in an if statement (below) and it correctly sorted the corresponding search, but failed on the others (obviously), and also killed all of the click functions.

--------------------

if (parseInt = 18)

    {

       queryParams.orderByFields = ["NAME"];

    }

As I see it now I have 2 options...

  1. Correct the if statement to work correctly...but what am I missing?
  2. Build multiple queries into a group widget... I think this is possible right?

I don't like option 2 as much even though, if possible, it would be easiest.  I would be able to hard code the sort field per query and all would be good.  However, the current Query widget lends itself well to having multiple queries in the single widget.

Thoughts????

Robert Scheitlin, GISP

1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Nick,

  OK now that makes sence as to what you are doing wrong. the _queryByObjectIds function is looking at your  parseInt line and parseInt is a function in JS and not normally a variable unless you declare it to be. So your code makes no sense. Try this:

     _queryByObjectIds: function(objectIds, returnGeometry){
        var queryParams = new EsriQuery();
        queryParams.returnGeometry = !!returnGeometry;
        queryParams.outSpatialReference = this.map.spatialReference;
        queryParams.outFields = this._getOutputFields();
        queryParams.objectIds = objectIds;
        var layerId = this._getLayerIndexByLayerUrl(this.currentAttrs.config.url);
        if (layerId === 18){
          queryParams.orderByFields = ["NAME"];
        }
        var queryTask = new QueryTask(this.currentAttrs.config.url);
        return queryTask.execute(queryParams);
      },

View solution in original post

9 Replies
RobertScheitlin__GISP
MVP Emeritus

Nick,

   Your problem is likely this line if (parseInt = 18)

In JavaScript a single equal sign is an assignment and not a comparison operator.

So it should be if (parseInt === 18)

NicholasBarger
Occasional Contributor III

I have corrected that line, but I think I need to add more logic to the if statement.  It still won't sort correctly.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nick,

I guess I would need to see more of what you have done in the _queryByObjectIds function

0 Kudos
NicholasBarger
Occasional Contributor III

Not too much in that section. ESRI Tech support gave me the query.orderByFields property and together we were able to enter the following statement….

queryParams.orderByFields = ["STATION"]

this worked fine for my first query (Firestations), but when I added another query that did not contain the STATION field it failed. ESRI Tech support said there would need to be an if statement and possibly some additional logic in the statement to get the query widget to sort based on the id of the service.

I found the section of code (I think), and the variable that should contain the service id number (parseInt). Here is the If statement that I have with your previously mentioned changes...

if (parseInt === 15)

{

queryParams.orderByFields = ["STATION"];

}

When I had the parseInt = 15 it correctly sorted the field (I’m thinking this is because the logic wasn’t correct and it passed to the next line of code).

When I changed it to your suggestion it worked…but did not sort correctly. My end goal is to say If the service ID = ## then sort by , else if ID = ## and so on…

Here is the complete section of code that you are referring to…

_queryByObjectIds: function(objectIds, returnGeometry){

var queryParams = new EsriQuery();

queryParams.returnGeometry = !!returnGeometry;

queryParams.outSpatialReference = this.map.spatialReference;

queryParams.outFields = this._getOutputFields();

queryParams.objectIds = objectIds;

//if (parseInt === 15)

// {

// queryParams.orderByFields = ["STATION"];

// }

var queryTask = new QueryTask(this.currentAttrs.config.url);

return queryTask.execute(queryParams);

},

I have commented out the section with the if statement so that the app works correctly.

~ Nick

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nick,

  OK now that makes sence as to what you are doing wrong. the _queryByObjectIds function is looking at your  parseInt line and parseInt is a function in JS and not normally a variable unless you declare it to be. So your code makes no sense. Try this:

     _queryByObjectIds: function(objectIds, returnGeometry){
        var queryParams = new EsriQuery();
        queryParams.returnGeometry = !!returnGeometry;
        queryParams.outSpatialReference = this.map.spatialReference;
        queryParams.outFields = this._getOutputFields();
        queryParams.objectIds = objectIds;
        var layerId = this._getLayerIndexByLayerUrl(this.currentAttrs.config.url);
        if (layerId === 18){
          queryParams.orderByFields = ["NAME"];
        }
        var queryTask = new QueryTask(this.currentAttrs.config.url);
        return queryTask.execute(queryParams);
      },
NicholasBarger
Occasional Contributor III

That was the trick.  I had tried a variation of declaring the variable but I failed to include the additional code after the var layerid.

At this point I should be able to nest the if statement to include the other layers that I am looking to sort right?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Yep

0 Kudos
NicholasBarger
Occasional Contributor III

Worked!!!  Easy fix...thanks Robert!

0 Kudos
GloriaTshokama
New Contributor III

Wawooo ,,, This look like a puzzle. I hope you will figure it out... staay in touch with the support team the can help.