Select to view content in your preferred language

Using a dynamic date for the Filter widget

4300
3
Jump to solution
05-29-2017 06:43 AM
SvivaManager
Frequent Contributor

Hello,

I have a WAB application which is used by a helpdesk team and it shows a layer that contains a lot of incidents.

At the moment I have 2 different layers for them in the webmap.

One layer contains all incidents regardless the type of information.

Another layer is already filtered through the MXD to show only last week's incidents.

Aside those two layers, I've added the filter widget to enable them to filter the complete layer (which is not shown by default at startup).

This situation of two similar layers causes a lot of confusion and not user friendly!

I want to enable them to have only ONE single layer which is filtered by default to show only TODAY's incidents.

Robert Scheitlin, GISP‌ was extremely helpful to show me how to start the app with the filter enabled!

But when I have a datetime filter which asks for parameters.. it becomes impossible to initialize that filter on startup + I cannot find any way to have the filter's date value dynamic for today's date.

So for a dynamic date of today I can find a workaround.. a HORRIBLE one but it's better than nothing - Run a script every night to inject today's date in the configuration file on the filter widget.

But.. because I cannot initialize this filter on startup of the widget.. it does not help me show a filtered layer.. It takes an extra click from the user each  time the application starts and it's just not right.

I tried using the Enhanced Query widget but it does not know functions such as GETDATE().. I also tried do "convert(date,GETDATE())".

I found a great article to show how it's possible to filter a webmap layer dynamically with dates (by Jake Skinner - https://community.esri.com/docs/DOC-8191, which then GETDATE() do works! but that same technic does not work on the widget.

I also tried the enhanced search widget of Robert, but when running the SQL statement, my browser just freeze to death

This is so basic for a filter widget! I'm shocked to see it does not exists or so complicated to achieve! despite the fact it's an SQL statement!

Can anyone think of a better solution please?

Let me just emphesis that the layer cannot be filtered through the webmap or the MXD project, because the user should have the option to change the filter and investigate the rest of the records.

Regards,

Shay.

P.S. I just notice it might be a similar post to Toni Sevenius

https://community.esri.com/thread/76651

0 Kudos
1 Solution

Accepted Solutions
SvivaManager
Frequent Contributor

So surprisingly, just when I dedice to post a long help request after spending SO much hours on this problem, suddently I find the solution by myself

I hope it will help others, I think this ability is extremely important and helpful when creating a real-time application in any kind of organization!

So here is my final result:

- One full layer of all incidents

- A filter widget with 5 pre-made filters: Incidents of Today, All open Incidents, Incidents between dates, incidents by keywords*2

- Filter widget shows-up on startup of the application

- In the PostCreate function of the Filter Widget I apply the first filter (all of today's incidents)

- The user can choose to turn that filter off and/or use other filters, such as dates and keyword search.

With no previous knowledge or basic understanding of how the code works this might get a bit messy and confusing.. I hope it will make sense to whoever finds it relevant.

How to implement:

* Create a date filter with the WAB that just simply filters the layer by a specific date (any date).

* To be able to run the filter on startup on the widget, the filter SHOULD NOT have an interactive parameters option.

* To enable the filter when the widget is opened, insert the following code inside the PostCreate function of the Widget.js file in the Filter widget's folder-

    if (filterObj.name === 'NAME OF THE FILTER AS DEFINED IN THE WIDGET SETTINGS'){
   query('.header',node)[0].click();
    }

* Open the Filter configuration file (usually located in the following path - C:\Program Files\ArcGIS\WebAppBuilderForArcGIS\server\apps\*APPID*\configs\Filter\ ) and find the targeted filter you created on the first step. If you followed the steps correctly, you should find a field named "expr" TWICE for this filter. Just replace the existing value with the following syntax - "your_datetime_field >= CURRENT_DATE()-0.5".

* I've tried using SQL functions such as GETDATE and DATEADD and such which are not supported... apparentaly CURRENT_DATE works perfectly  and -0.5 is a must for today's records. When using -1 I get records for 2 days.

Overall it's a bit hard to explain but hopefully it was clear enough.

Regards,

Shay.

View solution in original post

3 Replies
SvivaManager
Frequent Contributor

So surprisingly, just when I dedice to post a long help request after spending SO much hours on this problem, suddently I find the solution by myself

I hope it will help others, I think this ability is extremely important and helpful when creating a real-time application in any kind of organization!

So here is my final result:

- One full layer of all incidents

- A filter widget with 5 pre-made filters: Incidents of Today, All open Incidents, Incidents between dates, incidents by keywords*2

- Filter widget shows-up on startup of the application

- In the PostCreate function of the Filter Widget I apply the first filter (all of today's incidents)

- The user can choose to turn that filter off and/or use other filters, such as dates and keyword search.

With no previous knowledge or basic understanding of how the code works this might get a bit messy and confusing.. I hope it will make sense to whoever finds it relevant.

How to implement:

* Create a date filter with the WAB that just simply filters the layer by a specific date (any date).

* To be able to run the filter on startup on the widget, the filter SHOULD NOT have an interactive parameters option.

* To enable the filter when the widget is opened, insert the following code inside the PostCreate function of the Widget.js file in the Filter widget's folder-

    if (filterObj.name === 'NAME OF THE FILTER AS DEFINED IN THE WIDGET SETTINGS'){
   query('.header',node)[0].click();
    }

* Open the Filter configuration file (usually located in the following path - C:\Program Files\ArcGIS\WebAppBuilderForArcGIS\server\apps\*APPID*\configs\Filter\ ) and find the targeted filter you created on the first step. If you followed the steps correctly, you should find a field named "expr" TWICE for this filter. Just replace the existing value with the following syntax - "your_datetime_field >= CURRENT_DATE()-0.5".

* I've tried using SQL functions such as GETDATE and DATEADD and such which are not supported... apparentaly CURRENT_DATE works perfectly  and -0.5 is a must for today's records. When using -1 I get records for 2 days.

Overall it's a bit hard to explain but hopefully it was clear enough.

Regards,

Shay.

by Anonymous User
Not applicable

Great post thanks everyone here!

I'd recommend Esri allow full SQL for the webmap filter itself.  

For example, if you create a point layer of 'Project Points' to be reviewed. Let's say they have an Expiration Date. You'd want to only display the points IF  Expiration Date is After TodaysDate  where TodaysDate would auto-populate by the date of today.  (We want to allow our administrative staff to add project points to a mini embedded map on a regulatory site, and they will drop off if they expire so they don't have to manually hide them by changing a status field or something)

Simple request -  Allow SQL in the webmap filter. Then, the webmap can be used in multiple apps, including ones that are not WAB.

0 Kudos
captain-oski
New Contributor

So here is the solution we came up that is pretty neat and works very well. And is reusable for any project.

You will need to edit the configuration of the filter widget file usually something like that ( src/widgets/Filter/Widget.js ) and add this snippet in the POSTMIXINPROPERTIES  function :

 

        this.config.filters.map(filter => {
          filter.filter.parts.map(part => {
            part.valueObj.value = new Date(Date.now()).toISOString()
          })
        })

 

This will update the configuration file valueObj.value dynamically before the widget is created and will fill the widget with the current date. Then the widget will pass the value into the query expression that will filter the data accordingly.

 

Hope this helps 

0 Kudos