Filtering a webmap layer within a web appbuilder application on startup

5078
24
Jump to solution
05-24-2017 01:03 AM
SvivaManager
Occasional Contributor II

Hello,

Is there any way for me to filter a layer on startup of an application?

My current solution was to publish the same layer twice in a web map service. One layer shows all records. The other one is filtered by date.

I want to have only one layer published and when the user starts the appilcation, filter it by date and let him do as he pleased.

Is there a way to configure a widget with a certain filter to kick-in on startup?

Regards,

Shay.

24 Replies
RobertScheitlin__GISP
MVP Emeritus

Shay,

   Did you add the require for FilterManager in your code?

0 Kudos
SvivaManager
Occasional Contributor II

Actually it's not my code, it was created with the app and I found that the FilterManager was already there by default, so I searched the command you mentioned in the post you were referring to and found it inside already.. So yes, they call the FilterManager in the require section before using it. see the attached screenshot -

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shay,

   In that case you would add a new line of code like:

this.filterManager.applyWidgetFilter(layerId, null, "your sql expression");

0 Kudos
SvivaManager
Occasional Contributor II

Robert,

Thanks for helping! I Just duplicated line 67 in the screenshot above and the instance came back but then it falls on a different function of finding a layer's info.

I did a debug on the code when I apply a filter with the widget and then of course it happens perfectly fine.

as from what I understand, the code I'm trying to insert is in the wrong spot... Could you guide me where should I put the code exactly? is this the right file for it?? where can I inject my own code after the app has loaded succefully??

Shay.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shay,

  OK after re-examining your requirement the best solution seems to be set your filter widget to open at start and then add lines 38-40 to your Filter Widgets postCreate function:

      postCreate: function(){
        this.inherited(arguments);
        this._store = {};
        this.layerInfosObj = LayerInfos.getInstanceSync();
        this.filterUtils = new FilterUtils();
        this.filterManager = FilterManager.getInstance();

        var filters = this.config.filters;
        array.forEach(filters, function(filterObj, idx) {
          var isAskForValue = this.filterUtils.isAskForValues(filterObj.filter);

          var parse = {
            icon: filterObj.icon ? jimuUtils.processUrlInWidgetConfig(filterObj.icon, this.folderUrl) :
              this.folderUrl + '/css/images/default_task_icon.png',
            index: idx,
            title: filterObj.name,
            toggleTip: this.nls.toggleTip,
            hasValue: isAskForValue ?
              (window.appInfo.isRunInMobile ? 'block !important' : '') : 'none',
            isAskForValue: isAskForValue,
            apply: lang.getObject('jimuNls.common.apply', false, window) || 'Apply'
          };

          if (!this._store[filterObj.layerId]) {
            this._store[filterObj.layerId] = {
              mapFilterControls: {}
              // filter_item_idx
            }; // filter_item_idx, mapFilterControls
          }

          var template = lang.replace(this._itemTemplate, parse, /\$\{([^\}]+)\}/ig);
          var node = html.toDom(template);
          html.place(node, this.filterList);
          this.own(
            query('.header', node)
            .on('click', lang.hitch(this, 'enableFilter', node, filterObj, parse))
          );
          if(filterObj.name === 'ParksFinder'){
            query('.header', node)[0].click();
          }

Where 'ParksFinder' is the name of your filter in the widget you want applied on startup.

SvivaManager
Occasional Contributor II

Great thanks a lot Robert. I highly appreciate all your help.

I hope you don't mind me asking, there is another small issue I'm having with this solution - the filter widget does not allow a default parameter of a CURRENT date.

I found the configuration file and it's all JSON based... which means I cannot insert a function to get the current date and inject it as a parameter.

have you ever got to deal with a work around for this? I'll probably use your filter widget instead of the built-in filter widget since I want the filter results to be available as a new layer. Otherwise, the filter get cancelled when the filter widget is closed.

Shay.

0 Kudos
RobertScheitlin__GISP
MVP Emeritus

Shay,

   No I have never run into that need yet. My eSearch as the ability to use a url parameter to search and has the ability to specify last48, thismonth, thisyear as date search values.

SvivaManager
Occasional Contributor II

Thanks again  

0 Kudos
AlistairFox
Occasional Contributor

Robert, this is a really good help. i got this working. But now I want to make it more invisible to the user. I have a URL variable I can grab and want to use that to change the value the filter is carried out on dynamically. So 2 questions:

1: where in this code can I intercept the filter query string set by default and change it and

2: how can I close the filter dialogue after the filter has run.

TimConfare
New Contributor III

Alistair, this is exactly what I was trying to do.  Have you found a solution?

0 Kudos