Skip to Content

Default value in smart filter using Annotations or extensions

Hi all,

I would like to know is there any way to set default values to smart filter when an app is opened with standard variant . below is the screen shot for reference

Date column should be prefilled with on or after todays date.

i have extended the standard list controller but would like to know the id of the filter and add the pre fill the date on or after filter value .

is it achieved by Variant Management or is there any way ?

dd.jpg (29.0 kB)
Add comment
10|10000 characters needed characters exceeded

  • Get RSS Feed

4 Answers

  • Jan 04 at 03:29 AM

    Hi Prasad,

    You can use the initialized event of the Filter Bar to set the values in the fields.

    In order to get the field, you can do the following :

    var smartFilterBar = this.getView().byId("smartFilterBar");  // get the filter bar instance
    
    var field = smartFilterBar.getControlByKey("FieldName"); // get the field (FieldName is the name of the field in Metadata)
    
    field.setValue("My Value");

    Thanks,

    Vaibhav Maheshwari

    Add comment
    10|10000 characters needed characters exceeded

  • Jan 04 at 06:58 AM

    Hi Prasad,

    In order to add a future date, you can do it in many ways.

    var date =new Date(); // It will give you current system date (today's date)
    date.setDate(date.getDate()+ 10 );  // 10 is the no of days i want in future
    Or 
    var date = new Date();  Fri Jan 04 2019 12:26:25 GMT+0530 (India Standard Time)
    date.addDays(1);  // No of days you want to go in future  Sat Jan 05 2019 12:26:25 GMT+0530 (India Standard Time)
    Add comment
    10|10000 characters needed characters exceeded

    • Thanks Vaibhav Maheshwari

      Expecting like below "Greater than Equal to" by default we load .

      below is the code i have implemented

      	onInitSmartFilterBarExtension: function (oEvent) {
      		var smartFilterBar = this.getView().byId("listReportFilter");
      		var field = smartFilterBar.getControlByKey("enddate");
      		var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({
      			pattern: "YYYY-MM-dd"
      		});
      		var datetoDay = dateFormat.format( new Date());
      		
      		
      		field.setValue(datetoDay);
      	}
      
      
      date.jpg (11.9 kB)
  • Jan 04 at 09:42 AM

    For this you will have to use tokens to be added to your filter instead of directly setting the valuues.

    Try the following code:

    onInitSmartFilterBarExtension:function(oEvent){
    		var smartFilterBar = this.getView().byId("listReportFilter");
    		var field= smartFilterBar.getControlByKey("enddate");
    		var dateFormat =sap.ui.core.format.DateFormat.getDateInstance({pattern:"YYYY-MM-dd"});
    		var datetoDay = dateFormat.format(newDate());
    
    
    // Create a new token
    var token = new sap.m.Token({
    				key: "date", // any unique value
    				text: ">=" + datetoDay ,  //Concatenate >= sign to date
    				tooltip: ">=" + datetoDay
    			});
    
    
    // JSON to declare the values we want in actual filter
    var valueComp =   {
          "exclude": false,
          "keyField": "enddate",
          "operation": "GE",
          "tokenText": ">=" + datetoDay, //Concatenate >= sign to date
          "value1": datetoDay,  // Actual date value
          "value2": undefined }
    };
    
    
    // add the above defined values to CustomData
    var customData = new sap.ui.core.CustomData( "data", {
                                                  key : "custdata", // any unique value
                                                  value : valueComp }); // JSON declared above
    token.addCustomData(customData); // Add custom data to token
    field.addToken(token); // add token to the filter field.
    
    }

    Thanks,

    Vaibhav Maheshwari

    Add comment
    10|10000 characters needed characters exceeded

    • Thanks Vaibhav Maheshwari for the reply

      The same thing i am actually expecting. but when i am actually clicking on go button . the filter value is not passed to backend and value is getting disappeared from the field .

      Do we need to perform the filter addition in onBeforeRebindTableExtension method also?

      why the value is disappearing after clicking go button .

      Regards

      Prasad

  • Jan 04 at 03:26 PM

    Hi Prasad,

    If you have the instance of the smarttable, then it is easy to get the instance of the smart filter.

    var oBindingParams = oEvent.getParameter("bindingParams");
    		oBindingParams.parameters = oBindingParams.parameters || {};
    						
    		var oSmartTable = oEvent.getSource();
    		var oSmartFilterBar = this.byId(oSmartTable.getSmartFilterId());

    https://ui5.sap.com/#/topic/3a515829ffd74239878ebc0d453d001d

    BR,

    Mahesh

    Add comment
    10|10000 characters needed characters exceeded

    • Thanks for the reply Mahesh Kumar Palavalli .

      Implemeted below code for getting value

      var dateFormat = sap.ui.core.format.DateFormat.getDateInstance({ pattern: "YYYY-MM-dd" }); 
      if (oSmartFilterBar instanceof sap.ui.comp.smartfilterbar.SmartFilterBar) { var enddate= dateFormat.format(oSmartFilterBar.getControlByKey("endate").getDateValue());
      }

      but the value of endate is empty and we have used tokens to fill the data as above in init method of smart filter .

      is it necessary to push this values explicitly to filters for filtering data as its a standard filter field and not a custom .

      How to handle this field when we delete this token and add different token while filtering

      Regards

      Prasad