This is a migrated thread and some comments may be shown as answers.

Set OData filter operator conditionally

4 Answers 257 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Benjamin
Top achievements
Rank 1
Benjamin asked on 25 Sep 2015, 07:42 AM

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!

4 Answers, 1 is accepted

Sort by
0
Benjamin
Top achievements
Rank 1
answered on 25 Sep 2015, 09:33 AM

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;
                    }

0
Rosen
Telerik team
answered on 29 Sep 2015, 08:04 AM

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!
 
0
Benjamin
Top achievements
Rank 1
answered on 29 Sep 2015, 08:32 AM

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;
                        }
                    }
                });
        }‚Äč

0
Rosen
Telerik team
answered on 30 Sep 2015, 02:27 PM

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!
 
Tags
Data Source
Asked by
Benjamin
Top achievements
Rank 1
Answers by
Benjamin
Top achievements
Rank 1
Rosen
Telerik team
Share this question
or