Select to view content in your preferred language

Where expression on Query Task using Action Script

2606
14
Jump to solution
07-12-2012 11:05 AM
ionarawilson1
Deactivated User
I am developing a website that has 2 comboboxes and 1 text input. The user can search for a record based on entering the name of a company on the text input, or search based on the name of a company and name of the county (on the dropdown box) or search based on the name of the company, name of the county and type of industry (the other drowpdown box). He can also select one criteria at time or two at a time. So because there 3 elements, there is a possibilty of 7 different types of search. I first had created a query for each (a query usin mxml tags) and it worked fine. But what I want to do is to create just one query and change the where clause according to the search options, but it is not working. I think there is something wrong with my query expression. Does anybody have any ideas on what needs to be changed?  The query occurs after the user selects the options and click a search button. Then the graphic layer is created showing the points relative to the search and a datagrid shows the data


Here is the function for the button click to create the query

  protected function buttonclick():void                            {     var querytask:QueryTask =  new QueryTask();     querytask.url = "http://tfs-24279/ArcGIS/rest/services/ForestProducts/dynamic_layer_forest_products/MapServer/0"          var query:Query = new Query();     query.outSpatialReference = myMap.spatialReference;     query.outFields = ["Company"];     query.returnGeometry = true;     query.where = expr;     var expr:String = new String;     querydg.dataProvider = querytask.executeLastResult.attributes;              querydg.height=querytask.executeLastResult.attributes.length + 150          querydg.visible= querytask.executeLastResult !=null && querytask.executeLastResult.attributes.length>0                if (qText.text != "" && countycb.selectedItem != " " && industrycb.selectedItem != " ")           {      expr="Company Like '%{qText.text}%' AND County = '{countycb.selectedItem}' AND prim_bus = '{industrycb.selectedItem}'"           }          if (qText.text != " "  && countycb.selectedItem == " " && industrycb.selectedItem == " " )           {      expr= "Company Like '%{qText.text}%' "           }                           if (qText.text =="" && countycb.selectedItem != " " && industrycb.selectedItem == " " )           {      expr="County = '{countycb.selectedItem}'"     }                    if (qText.text =="" && countycb.selectedItem == " " && industrycb.selectedItem != " " )           {      expr= "prim_bus = '{industrycb.selectedItem}'"          }          if (qText.text != "" && countycb.selectedItem != " " && industrycb.selectedItem == " ")           {      expr="Company Like '%{qText.text}%' AND County = '{countycb.selectedItem}'"           }                                   if (qText.text != "" && countycb.selectedItem == " " && industrycb.selectedItem != " ")           {      expr = "Company Like '%{qText.text}%' AND prim_bus= '{industrycb.selectedItem}'"           }               if (qText.text =="" && countycb.selectedItem != " " && industrycb.selectedItem != " ")           {      expr = "County = '{countycb.selectedItem}' AND prim_bus= '{industrycb.selectedItem}'"           }                     querytask.execute(query, new AsyncResponder (onResult, onFault));     function onResult(featureSet:FeatureSet, token:Object = null):void     {            if (featureSet.features.length == 0)      {       Alert.show("No matching records found. Please try again.");       resizableDraggableTitleWindow.visible = false;       querydg.visible = false;       info.text = " ";      }             else             {       querydg.visible = true;       resizableDraggableTitleWindow.visible = true;       querydg.rowCount = querydg.dataProvider.length;       myGraphicslayer.visible = true;              import com.esri.ags.utils.GraphicUtil;      }                         if (featureSet.features.length > 1) {       info.text = "There are " + featureSet.features.length + " matching records";             }      if (featureSet.features.length == 1) {       info.text = "There is " + featureSet.features.length + " matching record";      }                 }                    function onFault(info:Object, token:Object = null):void     {      Alert.show(info.toString(), "Query Problem");     }              }       
Tags (2)
0 Kudos
14 Replies
RobertScheitlin__GISP
MVP Emeritus
Ionara,

   Are you sure there is anything in qText.text when myMap_initializeHandler is called?
0 Kudos
ionarawilson1
Deactivated User
Robert, I think it is blank when it is initialized, then the user will enter something, then do the search after clicking on the search button.

I am attaching the code. Thank you so much for your help!!!
0 Kudos
ionarawilson1
Deactivated User
Thank you for the explanation Ken! I am printing the code and your explanation and will analyze tonight !!! Thanks!!!


Sure.

In the first If statement, it checks if anything was entered in the qText and if so, it puts that into the Expr variable

The next If statement checks if anything was entered in the countycb combobox.. If so, then if there is something in the Expr variable (meaning the first If statement was true), then it appends "AND County = '{countycb.selectedItem}'" to the variable. If not, it just puts "County = '{countycb.selectedItem}'" into the variable.

Finally, the third If statement check if anything was entered in the industrycb combobox. If so, then if there is something in the Expr variable (meaning either of the first two If statement were true), then it appends "AND prim_bus = '{industrycb.selectedItem}'" to the  variable. If not, it just puts "prim_bus = '{industrycb.selectedItem}'" into  the variable.
0 Kudos
RobertScheitlin__GISP
MVP Emeritus
Ionara,

  Here is your code fixed and cleaned. There is a great command in Flash Builder source menu called "organize imports" you need to use it. Also notice the coding convention, Clean and free of excess white space (much easier to read). The main part of the fix was to build the expression in the buttonclick function.

Make sure you promote Kens posts as well.

Don't forget to click the Mark as answer check on this post and to click the top arrow (promote).
Follow the steps as shown in the below graphic:
0 Kudos
ionarawilson1
Deactivated User
Thank you Robert ! You are my hero!!!  I know I need to clean up the data better and I will be more careful with that!
The only thing I added is the else condition in case the user selects nothing and all data is retrieved:

else if (qText.text == "" && industrycb.selectedItem == " " && countycb.selectedItem == " ")
    {
    
     expr = "1 = '1'"
  
    }

Thank you Ken for the help with the code explanation. You guys are so helpful. I can't thank you enough!
0 Kudos