Adam Harkus
Adam Harkus

Reputation: 2210

FilterOperator.Contains Doesn't work correctly

I have a model which contains site info.

If I do a search on the beginning of the "SearchString" (using FilterOperator.Contains), the input list is populated fine, but if I search on a string not at the beginning of the string, nothing is returned.

e.g ... SearchString= "100 My Town"

An entry of '100' works. An entry of 'My' doesn't !

No error is displayed either.

Controller :

handleSuggest: function (oEvent) {
    var sTerm = oEvent.getParameter("suggestValue");
    var aFilters = [];
    if (sTerm) {
         aFilters.push(new Filter("SearchString", sap.ui.model.FilterOperator.Contains, sTerm ));
    }

    oEvent.getSource().getBinding("suggestionItems").filter(aFilters);

},

View :

<Input              
                    id="shopInput"
                    type="Text"
                    placeholder="Enter Shop Number ..."
                    showSuggestion="true"
                    suggest="handleSuggest"    
                    suggestionItems="{/SiteSet}"
                    change="validateInput"
                    liveChange="liveChange"
                    valueStateText="Shop Number must be valid"
                    suggestionItemSelected="suggestionItemSelected">
                    <suggestionItems>
                        <core:Item text="{SearchString}" />
                    </suggestionItems>

Any Ideas?

Upvotes: 0

Views: 7718

Answers (3)

Gustavo Fontinha
Gustavo Fontinha

Reputation: 59

I'd an other problem with FilterOperator.Contains.

My scenarios involves:

  • oData V2
  • Backend Java using Olingo
  • Frontend in Apache

I'd putted in my controller this Filter:

aFilter.push(new Filter("description", FilterOperator.Contains, `'${fDescription}'`));

But the filter didn't work.

Then, I'd investigated the problem and identify that FilterOperator.Contains, in SAPUI5 framework doesn't build the query parameter right.

Because the src/sap/ui/model/odata/ODataUtils.js package on ODataUtils._createFilterSegment function doesn't included the suffix "eq true":

case "Contains":
        return "substringof(" + oValue1 + "," + sPath + ")";

Searching in odata.org (https://www.odata.org/documentation/odata-version-2-0/uri-conventions/) and conforming de sample:

$filter=substringof('Alfreds', CompanyName) eq true

To fix the problem I'd changed the controller to use FilterOperator.EQ in this way:

aFilter.push(new Filter(`substringof('${fDescription}',description)`, FilterOperator.EQ, true));

Upvotes: 1

Adam Harkus
Adam Harkus

Reputation: 2210

I've now found the solution from right under my nose in the input.suggestions.custom section of https://sapui5.netweaver.ondemand.com/sdk/explored.html#/entity/sap.m.Input/samples

handleSuggest : function(oEvent) {
  var view = this.getView();
  var sTerm = oEvent.getParameter("suggestValue")
  view.byId("shopInput").setFilterFunction(function(sTerm, oItem) {
 // A case-insensitive 'string contains' style filter
     return oItem.getText().match(new RegExp(sTerm, "i"));
 });

},

Upvotes: 1

dotchuZ
dotchuZ

Reputation: 2641

As far as I can see you are missing to set the ID in the Suggestion item, which enables you to search for it? Your Filter goes on SiteId, but your Suggestion item is only binded to SearchString.

This should help:

<suggestionItems>
      <core:Item key="{SiteId}" text="{SearchString}" />
</suggestionItems>

Upvotes: 0

Related Questions