tarzanbappa
tarzanbappa

Reputation: 4958

SAPUI5 Filter Bar Get filter values for each filter item in the filter bar

I have a filter bar with multiple filter items and I need to get the selected/typed values for each filter item in the onSearch event. I've tried but unable to figure out a way of getting all filter data for all the filter items.

<fb:FilterBar reset="onReset"
              search="onSearchFilterbar"
              showRestoreButton="true"
              showClearButton="true"
              filterBarExpanded="false"
            id="userFilterBar">
    <fb:filterItems>
        <fb:FilterItem name="A" label="User">
            <fb:control>
                <Select id="slcUser" forceSelection="false"
                        items="{ path: 'sysusers>/Users' , sorter: { path: 'Name' } }" >
                    <core:Item key="{sysusers>Name}" text="{sysusers>Name}"/>
                </Select>
            </fb:control>
        </fb:FilterItem>
        <fb:FilterItem name="B" label="Location">
            <fb:control>
                <Select id="slcLocation" forceSelection="false"
                        items="{ path: 'location>/Locations' , sorter: { path: 'Name' } }">
                    <core:Item key="{location>Name}" text="{location>Name}"/>
                </Select>
            </fb:control>
        </fb:FilterItem>
    </fb:filterItems>
</fb:FilterBar>  



onsearch:function(oEvent){
    oEvent.getSource().getFilterItems()[0];
    // But cannot find a way to get the selected data
}

Upvotes: 4

Views: 13935

Answers (3)

Kyle
Kyle

Reputation: 612

There are a few ways to do this.. but with your current code, I would suggest the following:

The short answer to your question:

The FilterBar has a method determineControlByFilterItem you can use to get the control for the filter item, which you can then use to get the selected value.

var oFilterItem = oEvent.getSource().getFilterItems()[0];
var oControl = oFilterBar.determineControlByFilterItem(oFilterItem);
var sSelectedValue = oControl.getSelectedKey();

Be cautious, though, when hard coding array indexes like this. For a more complete solution to your problem, I've suggested a full approach below.

The long answer if you want to use the filter bar to filter a result set:

First, make sure the names of your filter items align to the name of the property you want to filter by. So in your case, your filter items are named "A" and "B"... I suggest you change these to match the property name you want to filter by. Assuming that the names of the properties you want to filter by are "User" and "Location":

<FilterItem name="User" label="User">
...
<FilterItem name="Location" label="Location">
...

Then in your controller, you can use those names to build an array of sap.ui.model.Filter objects that you can use to filter your result set.

onSearch: function(oEvent) {
    //get the filter bar from the event
    var oFilterBar = oEvent.getSource();

    //get the filter items from the filter bar
    var aFilterItems = oFilterBar.getFilterItems();

    //map the array of FilterItems to a new array of sap.ui.model.Filter objects
    var aFilters = aFilterItems.map(function(oFilterItem) {
        //get the filter item name (which is now the same as the filter property name)
        var sFilterName = oFilterItem.getName();

        //use the filter bar to get the control for the filter item
        var oControl = oFilterBar.determineControlByFilterItem(oFilterItem);

        //use the control to get the selected value (selected key)
        var sSelectedValue = oControl.getSelectedKey();

        //use the filter item/property name and the selected value to create a new sap.ui.model.Filter
        var oFilter = new sap.ui.model.Filter(sFilterName, "EQ", sSelectedValue);

        //return the Filter object to the new array
        return oFilter
    });

    //use the array of sap.ui.model.Filter objects to filter your table

    //if you're using a responsive table (sap.m.Table), use:
    this.getView().byId("yourTableId").getBinding("items").filter(aFilters);

    //or if you're using a grid table (sap.ui.table.Table), use:
    this.getView().byId("yourTableId").getBinding("rows").filter(aFilters);
}

The reason I suggest this approach as opposed to others, is because it scales nicely. Say, for example, that you want to add a third Select control to filter by, you simply need to add a new <FilterItem name="NewFilterProperty" label="New Filter Property">. Because we didn't hard code anything into the event handler, it will still work without any additional changes.

The only thing to lookout for is if you use different types of controls in your FilterBar. So, for example, if you added an <Input> instead of a <Select>, you'll need to adjust the logic of your event handler to handle this.

I usually do something like this:

onSearch: function(oEvent) {
    var oFilterBar = oEvent.getSource();
    var aFilterItems = oFilterBar.getFilterItems();
    var aFilters = aFilterItems.map(function(oFilterItem) {
        var sFilterName = oFilterItem.getName();
        var oControl = oFilterBar.determineControlByFilterItem(oFilterItem);
        var sValue;
        if (oControl.getMetadata().getName() === "sap.m.Select") {
            sValue = oControl.getSelectedKey();
        } else if (oControl.getMetadata().getName() === "sap.m.Input") {
            sValue = oControl.getValue();
        }
        var oFilter = new sap.ui.model.Filter(sFilterName, "EQ", sValue);
        return oFilter;
    });

}

Upvotes: -2

D. Seah
D. Seah

Reputation: 4592

there are a few ways to do this. IMO, the best way is to use model. that's adopting the MVC approach. here is a working example, http://jsbin.com/nudewew/edit?html,output

<!DOCTYPE HTML>
<html>

  <head>
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta charset="UTF-8">
    <title>MVC</title>
    <script id="sap-ui-bootstrap" type="text/javascript"
            src="https://sapui5.hana.ondemand.com/resources/sap-ui-core.js"
            data-sap-ui-theme="sap_bluecrystal"
            data-sap-ui-libs="sap.m,sap.ui.table"
            data-sap-ui-xx-bindingSyntax="complex">
    </script>

    <script id="oView" type="sapui5/xmlview">
    <mvc:View height="100%" controllerName="myView.Template"
      xmlns="sap.m" 
      xmlns:fb="sap.ui.comp.filterbar"
      xmlns:core="sap.ui.core"
      xmlns:mvc="sap.ui.core.mvc">
    <fb:FilterBar reset="onReset"
              search="onSearchFilterbar"
              showRestoreButton="true"
              showClearButton="true"
              filterBarExpanded="false"
            id="userFilterBar">
    <fb:filterItems>
        <fb:FilterItem name="A" label="User">
            <fb:control>
                <Select id="slcUser" forceSelection="false" selectedKey="{selection>/user}"
                        items="{ path: 'sysusers>/Users' , sorter: { path: 'Name' } }" >
                    <core:Item key="{sysusers>Name}" text="{sysusers>Name}"/>
                </Select>
            </fb:control>
        </fb:FilterItem>
        <fb:FilterItem name="B" label="Location">
            <fb:control>
                <Select id="slcLocation" forceSelection="false" selectedKey="{selection>/location}"
                        items="{ path: 'location>/Locations' , sorter: { path: 'Name' } }">
                    <core:Item key="{location>Name}" text="{location>Name}"/>
                </Select>
            </fb:control>
        </fb:FilterItem>
    </fb:filterItems>
</fb:FilterBar>  
  </mvc:View>
    </script>
    <script>
      sap.ui.define([
        'jquery.sap.global',
        'sap/ui/core/mvc/Controller',
        'sap/ui/model/json/JSONModel'
      ], function(jQuery, Controller, JSONModel) {
        "use strict";

        var oController = Controller.extend("myView.Template", {
          onInit: function() {
            var oView = this.getView();

            oView.setModel(new JSONModel({
              user: "",
              location: ""
            }), "selection");
            oView.setModel(new JSONModel({
              Users: [
                {Name: "johnDoe"},
                {Name: "maryAnn"}
              ]          
            }), "sysusers");
            oView.setModel(new JSONModel({
              Locations: [
                {Name: "London"},
                {Name: "Paris"}
              ]          
            }), "location");
          },
          onSearchFilterbar: function(oEvent) {
            console.log(this.getView().getModel("selection").getData());
          }

        });

        return oController;
      });

      var oView = sap.ui.xmlview({
        viewContent: jQuery('#oView').html()
      });

      oView.placeAt('content');
    </script>
  </head>
  <body class="sapUiBody" role="application">
    <div id="content"></div>
  </body>
</html>

Upvotes: 0

I.B.N.
I.B.N.

Reputation: 1034

The value of items is on Parameters of de Event.

oEvent.getParameter('0').selectionSet

It's a array with each Control of filterbar you can use:

oEvent.getParameter('0').selectionSet[0].getValue()

Upvotes: -1

Related Questions