Programmatically filter attribute table

6630
24
Jump to solution
06-17-2016 02:39 PM
Ravichandran_M_Kaushika
Occasional Contributor

dear Readers,

thank you for taking the time to read the contents of the post.

How to programmatically filter records of the attribute grid?

there is a filter menu that open a popup window which allows the users to select the column by which the condition should be applied.  it also provides the ability to add 1 or 2 more columns with the option to make it ANY or AND.\

Is there a way to perform this operation programmatically?

thanks and regards

ravi.

0 Kudos
24 Replies
LefterisKoumis
Occasional Contributor III

THank you Robert. However, I was referring to the query and not the solution for initializing the AT.

But based on your response this:

if(attWidget){  
              this.wManager.openWidget(attWidget);  
              var partsObj = {  
                "expr": "Crime_Type='Auto Theft'",  
                "parts": []  
              };  
              console.info(attWidget);  
              var activeTable = attWidget._tableFunctionController.getActiveTable();  
              activeTable.setFilterObj(partsObj);  
              activeTable.refresh();  ‍‍‍‍‍‍‍‍‍‍

needs to be modified to this, line 7?

if(attWidget){  
              this.wManager.openWidget(attWidget);  
              var partsObj = {  
                "expr": "Crime_Type='Auto Theft'",  
                "parts": []  
              };  
var activeTable = attWidget._activeTable.active();??
activeTable.setFilterObj(partsObj);   
activeTable.refresh(); ‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Nope. it is exactly like I posted previously. Just change the expr to whatever you desire.

0 Kudos
LefterisKoumis
Occasional Contributor III

ok. Got it.

At the risk to be an idiotic question here is:

I am modifying the  _FeatureTable.js which is under the AT/table  folder. 

The code presented here is to call the 

attWidget._activeTable;

where attWidget is AT widget. 

In the _FeatureTable.js how to get access to the _activeTable function which in the widget.js? 

Do I need to re-create the _activeTable in the current file?

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris,

   The attWidget is a reference to the AttributeTable widget (That could be coming from any widget). I was not providing code specific to the _FeatureTable.js, actually I am not sure why you would can to do it there.

0 Kudos
LefterisKoumis
Occasional Contributor III

Understood. I cannot get the activeTable.refresh working.

I am trying to filter the clicked column based on text entered in textbox. The script is in  in  _FeatureTable.js which is under the AT/table  folder. 

_onHeaderClick: function (evt) {
      var cell = this.grid.cell(evt);
      var column = cell.column;
      console.log(column)
      filter_name = column.field

-----
-----

  registry.byId(this.thetextbox.id).on("change", function (value) {
        console.log(value.length)
        //  console.log(userVal)       
        var query = new Query();
        if (value.length >= 3) {
          this.wManager = WidgetManager.getInstance();         
          var attWidget = this.wManager.getWidgetsByName('AttributeTable');
          var partsObj = {
            "expr": filter_name + " LIKE '%" + value + "%'",
            "parts": []
          };
          var activeTable = attWidget[0]._activeTable;   
          activeTable.setFilterObj(partsObj);   
          activeTable.refresh();‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
}‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
RobertScheitlin__GISP
MVP Emeritus

OK. I finally understand what you are trying to do and the changes that are now needed for the latest version. The fact that you have this code in the _FeatureTable.js makes it a lot simpler. there is no need to get the AttributeTable widget object at all.

    createToolbar: function() {
...
      this.thetextbox = new TextBox({
        value: "",
        placeHolder: "type in your filter value",
        onChange: lang.hitch(this, this.filterTextChanged),
        intermediateChanges: true
      });
      toolbar.addChild(this.thetextbox);‍‍‍‍‍‍‍
...

    filterTextChanged: function(value) {
      if (value.length >= 3) {
        var partsObj = {
          "expr": "UPPER(" + filter_name + ") LIKE '%" + value.toUpperCase() + "%'",
          "parts": []
        };
        this.setFilterObj(partsObj);
        this.clearSelection(false);
        this.startQuery();

        this.emit('apply-filter', {
          layerInfoId: this.layerInfo.id,
          expr: partsObj.expr
        });
      }
      if(value === ""){
        var partsObj2 = {
          "expr": "1=1",
          "parts": []
        };
        this.setFilterObj(partsObj2);
        this.clearSelection(false);
        this.startQuery();

        this.emit('apply-filter', {
          layerInfoId: this.layerInfo.id,
          expr: partsObj.expr
        });
      }
    },‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍
0 Kudos
LefterisKoumis
Occasional Contributor III

Thank you Robert. This is very close of what I am trying to accomplish.  Didn't realized that it was under the  onFilterMenuItemClick function. In the definition of the textbox.I set it as disabled,  so user will need to click on the header of column before they can specify filter string in the textbox. The reason why I wanted to use the activeTable function, is because the filtering of a column could be followed by another filter on another column. So I assume that the activeTable has the resultant features on the table (after a filter operation) and you can filter on those features.

The code you provide works great (I committed the sin of not thinking about the lang.hitch) for applying a filter on a single column.

UPDATE: I just realized the answer was just glaring at me. Add to the expr. and it's done.

0 Kudos
LefterisKoumis
Occasional Contributor III

Is there way to prgrammatically to prevent the map refreshing when you click on a header? After you click on a header and you get the filtering results, I would like to keep these results displayed after you click on another column header. I try the stop progragation but the headerclick still refreshes the map.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Lefteris, 

  Sorry I do not see it doing that with the code I provided.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Frisky Weasel,

  I Have not tested this but it should be as simple as setting the expr to 1=1:

              var partsObj = { 
                "expr": "1=1", 
                "parts": [] 
              };  
              var activeTable = attWidget._tableFunctionController.getActiveTable(); 
              activeTable.setFilterObj(partsObj); 
              activeTable.refresh();