Filter a javascript search widget query

4054
5
Jump to solution
09-18-2015 08:49 AM
JohnWebber
New Contributor

I am attempting to implement a search widget to search for a parcel ID on the map. The feature layer I am using has multiple rows per polygon based on what appraisal year we are interested in. For other query tasks I am able to specify a where clause that limits returned rows to a specific year. I do not seem to have that same level of control with the sources for the search widget.

Here is the search widget initiating code I am using;

              var s = new Search({

                enableButtonMode: true, //this enables the search widget to display as a single button

                enableLabel: false,

                enableInfoWindow: true,

                showInfoWindowOnSelect: true,

                map: map

             }, "search");

Here is the source I am using to perform the search against the feature layer;

            var sources = [];

            sources.push({

            featureLayer: new FeatureLayer("http://xxx.xxx.xxx/arcgis/rest/services/xxx/MapServer/1"),

            searchFields: ["xxx.dbo.map_property_data_vw.prop_id"],

            displayField: "xxx.dbo.map_property_data_vw.prop_id",

            exactMatch: false,

            name: "propSearch",

            outFields: ["*"],

            placeholder: "Enter a property ID...",

            maxResults: 6,

            maxSuggestions: 6,

            infoTemplate: infoTemplateQuery,

            enableSuggestions: false,

            minCharacters: 2

            });

         s.set("sources", sources);

         s.startup();

The search works as expected and opens to appropriate infoWindow, however the data displayed is not from the correct year, thus not providing the latest information in some cases. I have no control over the REST service, or I would just alter the query to be more suitable for this application.

I am hoping to implement something similar to how I filter other feature layer queries;

  query.where = "xxx.dbo.map_property_data_vw.prop_val_yr = '" + 2015 + "'";

Is this possible, or should I be taking another approach?

Thank you in advance.

jw

0 Kudos
1 Solution

Accepted Solutions
ChrisSmith7
Frequent Contributor

This works!

Filter on "Republican":

        var testFeat = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CongressionalDistricts/FeatureServe...");
        testFeat.setDefinitionExpression("PARTY = 'Republican'"); 
        sources.push({
            featureLayer: testFeat,
            searchFields: ["DISTRICTID", "PARTY"], 
            displayField: "DISTRICTID", 
            suggestionTemplate: "${DISTRICTID}, Party: ${PARTY}", 
            name: "Congressional Districts",
            placeholder: "3708",
            maxResults: 6,
            maxSuggestions: 6,


            //Create an InfoTemplate and include three fields
            infoTemplate: new InfoTemplate("Congressional District", "District ID: ${DISTRICTID}</br>Name: ${NAME}</br>Party Affiliation: ${PARTY}"),
            enableSuggestions: true,
            minCharacters: 0
        });

Search: 37 (no dems returned):

Filter on "Democrat":

   var testFeat = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CongressionalDistricts/FeatureServe...");
        testFeat.setDefinitionExpression("PARTY = 'Democrat'");  
         sources.push({
            featureLayer: testFeat,
            searchFields: ["DISTRICTID", "PARTY"],  
            displayField: "DISTRICTID",  
            suggestionTemplate: "${DISTRICTID}, Party: ${PARTY}",  
            name: "Congressional Districts",
            placeholder: "3708",
            maxResults: 6,
            maxSuggestions: 6,


            //Create an InfoTemplate and include three fields
            infoTemplate: new InfoTemplate("Congressional District", "District ID: ${DISTRICTID}</br>Name: ${NAME}</br>Party Affiliation: ${PARTY}"),
            enableSuggestions: true,
            minCharacters: 0
         });

Search: 37 (no republicans returned):

View solution in original post

5 Replies
ChrisSmith7
Frequent Contributor

I'm not sure if this is helpful, but you could add a suggestion template, e.g.:

        sources.push({
            featureLayer: new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CongressionalDistricts/FeatureServe..."),
            searchFields: ["DISTRICTID", "PARTY"],
            displayField: "DISTRICTID",
            suggestionTemplate: "${DISTRICTID}, Party: ${PARTY}",
            exactMatch: false,
            outFields: ["DISTRICTID", "NAME", "PARTY"],
            name: "Congressional Districts",
            placeholder: "3708",
            maxResults: 6,
            maxSuggestions: 6,


            //Create an InfoTemplate and include three fields
            infoTemplate: new InfoTemplate("Congressional District", "District ID: ${DISTRICTID}</br>Name: ${NAME}</br>Party Affiliation: ${PARTY}"),
            enableSuggestions: true,
            minCharacters: 0
        });

So, when you search, say by DISTRICTID, in this case, the user could select from the suggestion list knowing their political party:

In your case, they could enter the prop ID and select the year of the record they want for 2015. Would that work?

0 Kudos
JohnWebber
New Contributor

Chris,

Thank you for your response. That is surely an approach I could take here.

My problem with it is that it seems a little confusing to the public (who will be using the site). The feedback I get from users is that simple is better, and that concepts like appraisal year, are best left to the appraisal experts. Just one school of thought though...

Would be ideal, if it were as simple is just entering an ID and being directed to the property.

Thanks again... 1st time poster!

jw

0 Kudos
ChrisSmith7
Frequent Contributor

No problem, John - it's a great community here - welcome! I've learned quite a bit trying to stay active on the forums when I've got some free time here and there. I agree that simpler is better, so if it's not valid to have users search by year in addition to ID, then we may be able to go about this another way:

FeatureLayer | API Reference | ArcGIS API for JavaScript

You might be able to create an instance of the FeatureLayer, then set the definition query:

featureLayer.setDefinitionExpression("STATE_NAME = 'South Carolina'");

And then pass this to your source (filtering on year). I haven't tested this, though!

0 Kudos
ChrisSmith7
Frequent Contributor

This works!

Filter on "Republican":

        var testFeat = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CongressionalDistricts/FeatureServe...");
        testFeat.setDefinitionExpression("PARTY = 'Republican'"); 
        sources.push({
            featureLayer: testFeat,
            searchFields: ["DISTRICTID", "PARTY"], 
            displayField: "DISTRICTID", 
            suggestionTemplate: "${DISTRICTID}, Party: ${PARTY}", 
            name: "Congressional Districts",
            placeholder: "3708",
            maxResults: 6,
            maxSuggestions: 6,


            //Create an InfoTemplate and include three fields
            infoTemplate: new InfoTemplate("Congressional District", "District ID: ${DISTRICTID}</br>Name: ${NAME}</br>Party Affiliation: ${PARTY}"),
            enableSuggestions: true,
            minCharacters: 0
        });

Search: 37 (no dems returned):

Filter on "Democrat":

   var testFeat = new FeatureLayer("http://services.arcgis.com/V6ZHFr6zdgNZuVG0/arcgis/rest/services/CongressionalDistricts/FeatureServe...");
        testFeat.setDefinitionExpression("PARTY = 'Democrat'");  
         sources.push({
            featureLayer: testFeat,
            searchFields: ["DISTRICTID", "PARTY"],  
            displayField: "DISTRICTID",  
            suggestionTemplate: "${DISTRICTID}, Party: ${PARTY}",  
            name: "Congressional Districts",
            placeholder: "3708",
            maxResults: 6,
            maxSuggestions: 6,


            //Create an InfoTemplate and include three fields
            infoTemplate: new InfoTemplate("Congressional District", "District ID: ${DISTRICTID}</br>Name: ${NAME}</br>Party Affiliation: ${PARTY}"),
            enableSuggestions: true,
            minCharacters: 0
         });

Search: 37 (no republicans returned):

JohnWebber
New Contributor

Chris,

I thank you for the answer. This is working fine, and only took 30 seconds to implement.

I hope you have a nice weekend.

jw

0 Kudos