Skip to Content
Technical Articles
Author's profile photo Jay Malla

SAP Fiori/UI5 custom app – Outsmarting the Smart filter dynamically for overriding the default behavior

This blog is about manipulating the Smart filter behavior and is a a follow on blog for SAP S/4 HANA Custom Fiori app for Project & WBS using CDS View Table Function, SmartFilter & Tree table

 

Smart filter bar and the smart filter is great and hides a lot of complexity of passing the values to the OData calls to the backend through the $filter parameter in the Get URL call.

 

But I have come across cases where I need to dynamically change the smart filter values on the fly.  This is not how things are done usually and I thought I came up with an interesting approach that may come in handy for another SAP Fiori/UI5 developer.

 

So here we go with the scenario.  I have built an application that you can search for Project, WBS Elements that are child nodes for the Project, and settlement cost centers that are tied to the WBS elements.

The screen looks like this – as shown in the original blog above.

Project%20WBS%20Application%20initial%20screen

Project WBS Application initial screen

 

So when I search for the settlement cost center 53344001, this field is buried in the hierarchy at the associated WBS element that can be several levels nested within the Project – e.g. a few WBS levels deep as shown here.

Settlement%20cost%20center%20with%20associated%20search

Settlement cost center with associated WBS Element

 

But we really want to see this settlement cost center with the full hierarchical representation.  So we have this nice button called “ShowHierarchy” and if you click on this, some magic happens behind the scenes….

 

Click%20on%20the%20ShowHierarchy%20button

Click on the ShowHierarchy button

 

And then viola….. we see the settlement cost center in the tree view with the full hierarchy….

Settlement%20cost%20center%20with%20full%20hierarchy%20being%20shown

Settlement cost center with full hierarchy being shown

 

It’s like a magic trick!  The filter has the settlement cost center 53344001 but the data being shown is like the query for the Project I-000066.  And that is the magic trick….. and it’s all happening by dynamically manipulating the filter on the fly but just not showing this to the user.

So now it’s time to explain the magic trick…. and you’ll see that it is not magic… but some cool coding in the back….  kinda like when you see a person in a box chopped in half… there are actually two people – one person is squatting on one side and the other is squatting on the other side and when they pull the boxes, it appears the person is pulled in a half.  Are you serious?

 

Okay – so here’s how I achieved this.  When I called the backend OData service and got the results, I capture the list of projects in a unique list and store this.

InvokeOData%20call

_InvokeODataCall method

 

 

And then in the _extractUniqueProjectList method….

Extracting%20the%20unique%20project%20list%20for%20later%20on

Extracting the unique project list for later on

 

 

And then for the finale……. when you click on the ShowHierarchy button….. that’s where I am doing the magic trick – outsmarting the smart filter….. in other words, dynamically manipulating the smart filter values to the backend call and telling it to query based on the stored projects…

onShowHierarchyButtonPress

onShowHierarchyButtonPress

 

Here I am getting the filters from the Projects stored previously as a JSONModel and passing this back as an array of Filters:

get%20Project%20Filters

get Project Filters

 

So there we have it…. we have replaced the filters from the Smart filters dynamically… so instead of having the filter as the settlementcostcenter which is at a lower level in the hierarchy, we are passing in the list of projects instead so that we can show the entire hierarchy and that particular settlementcostcenter will be there buried within the hierarchy.

onShowHierarchyButton%20Logic

onShowHierarchyButton Logic

 

 

So there you have it.  If you read this article to this point, I am assuming that you were trying to find out how you can change the Filters dynamically… and hopefully this article will shed some light on how you can creatively manipulate the filters dynamically which can come in really handy.  I’ve already had several apps that I needed to do some interesting work arounds as such.

Also, I’m hoping to you liked the article and the analogies to the magic trick….  I tried to add some humor to make it interesting.  Besides being a hard core SAP technology enthusiast (I’ve been a solution architect and development/integration lead for many large clients over 20 years), I am also a musician – and you can find my music on Spotify/ITunes/YouTube – just search for Jay Malla.  When I was consulting at PG&E for four years, my SAP manager played drums in my band – and we still are really good friends this this day way after those days we worked together!

Happy Coding!

Would be great to get some feedback on this creative approach if you are able to utilize these concepts.

Cheers,

Jay Malla

Licensed To Code

 

 

 

Assigned Tags

      Be the first to leave a comment
      You must be Logged on to comment or reply to a post.