Set OData filter operator conditionally

5 posts, 0 answers
  1. Benjamin
    Benjamin avatar
    21 posts
    Member since:
    Apr 2015

    Posted 25 Sep 2015 Link to this post

    Hi,

     I have an OData Datasource for an Autocomplete Box. The textfield of this box contains entries which have an article number and article description as a combined string. Now I want to achieve if the user types in a numeric value, that it filters by startswith and if the user enters a string, that it gets filtered by contains.

    Does anybody have an idea how to get this?

     

    Thanks!

  2. Benjamin
    Benjamin avatar
    21 posts
    Member since:
    Apr 2015

    Posted 25 Sep 2015 Link to this post

    Hi,

     finally got it working by myself. But as you can see, it seems, that there is a bug in the data.filter property. As i set the operator to "contains" it inserted substringof in the url (which is not supported by MS Webapi). Also the field and property were reversed, so I had to implement a little swap function:

     parameterMap: function (data, type)
                        {
                            var value = data.filter.filters[1].value;
                            var pm;
                            if (value != undefined)
                            {
                                if (isNaN(value))
                                {
                                    data.filter.filters[1].operator = "contains";
                                    var swap = data.filter.filters[1].field;
                                    data.filter.filters[1].field = "'" + data.filter.filters[1].value + "'";
                                    data.filter.filters[1].value = swap;
                                    pm = kendo.data.transports.odata.parameterMap(data);
                                    pm.$filter = pm.$filter.replace("substringof", "contains").replace("'", '').replace("'", '');
                                }
                                else
                                {
                                    data.filter.filters[1].operator = "startswith";
                                    vm.$log.debug("startswith");
                                    pm = kendo.data.transports.odata.parameterMap(data);
                                }
                            }
                            delete pm.$inlinecount;
                            delete pm.$format;
                            return pm;
                        }

  3. Kendo UI is VS 2017 Ready
  4. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 29 Sep 2015 Link to this post

    Hello Benjamin,

    We are glad that you have managed to achieve the functionality you are looking for.

    Regarding the note about filter contains operator. It led me to believe you are using transport type set to odata instead of odata-v4 which is needed in order to connect to WebApi OData controller. The code which is calling the base parameterMap should call the odata-v4 parameterMap:

    kendo.data.transports["odata-v4"].parameterMap.call(this, data, type);

    Regards,
    Rosen
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
  5. Benjamin
    Benjamin avatar
    21 posts
    Member since:
    Apr 2015

    Posted 29 Sep 2015 in reply to Rosen Link to this post

    Hi,

     thanks for your reply. Yes we already use odata-v4. Here is the complete code:

    initArtikelACDataSource()
            {
                var vm = this;
                this.ArtikelACDataSource = kendo.data.DataSource.create(
                    {
                        type: "odata-v4",
                        serverFiltering: true,
                        filter:
                        {
                            logic: "and",
                            filters:
                            [
                                {
                                    field: "ArtNr",
                                    operator: "lt",
                                    value: 100000
                                }
                            ]
                        },

                        transport:
                        {
                            read:
                            {
                                url: this.webApiServer + 'odata/vwLagerArtikel',

                                type: "GET",
                                dataType: "json",
                                beforeSend: (req) =>
                                {
                                    req.setRequestHeader('Authorization', 'Bearer ' + this.token);
                                }
                            },
                            parameterMap: function (data, type)
                            {
                                var value = data.filter.filters[1].value;
                                var pm;
                                if (value != undefined)
                                {
                                    if (isNaN(value))
                                    {
                                        data.filter.filters[1].operator = "contains";
                                        var swap = data.filter.filters[1].field;
                                        data.filter.filters[1].field = "'" + data.filter.filters[1].value + "'";
                                        data.filter.filters[1].value = swap;
                                        pm = kendo.data.transports.odata.parameterMap(data);
                                        pm.$filter = pm.$filter.replace("substringof", "contains").replace("'", '').replace("'", '');
                                    }
                                    else
                                    {
                                        data.filter.filters[1].operator = "startswith";
                                        pm = kendo.data.transports.odata.parameterMap(data);
                                    }
                                }
                                delete pm.$inlinecount;
                                delete pm.$format;
                                return pm;
                            }
                        }
                    });
            }​

  6. Rosen
    Admin
    Rosen avatar
    3234 posts

    Posted 30 Sep 2015 Link to this post

    Hello Benjamin,

     

    Although, you have set odata-v4 to the transport, you are still calling the odata transport's parameterMap. As I have mentioned in my previous message you should change the call to kendo.data.transports.odata.parameterMap(data) to kendo.data.transports["odata-v4"].parameterMap.call(this, data, type);

     

    Regards,
    Rosen
    Telerik
     
    Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
     
Back to Top
Kendo UI is VS 2017 Ready