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

Guids in filters

12 Answers 708 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
John
Top achievements
Rank 1
John asked on 03 Apr 2012, 12:42 PM
Our oData service is full of Guid as primary keys. So I need to be able to use guid data types in filters. Unfortunately when I use the following in the filter for the datasource:
     filter: { field: "UserID", operator: "eq", value: e.data.UserID }

 I get the following error from oData:
   "Operator 'eq' incompatible with operand types 'System.Guid' and 'System.String'

Any ideas, The native url requires filter=UserID+eq+guid'ab4e630e-1c7f-49cb-919a-5e82786e753e' .. How can I adjust the filter command to acheive this.
Thanks

12 Answers, 1 is accepted

Sort by
0
Atanas Korchev
Telerik team
answered on 06 Apr 2012, 09:33 AM
Hi,

 This can be done like this:

dataSource: {
       type: "odata",
       serverFiltering: true,
       transport: {
           read: "<url to the service>",
           parameterMap: function(options) {
               // call the default OData parameterMap
               var result = kendo.data.transports.odata.parameterMap(options);
 
               if (result.$filter) {
                  // encode everything which looks like a GUID
                  var guid = /('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')/ig;
                  result.$filter = result.$filter.replace(guid, "guid$1");
               }
               return result;
           }
       }
   }

All the best,
Atanas Korchev
the Telerik team
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Stevo
Top achievements
Rank 1
answered on 21 Nov 2014, 01:35 PM
Hi,

this works fine as long as strings don't look like guids. 

Can you help us extending this functionality to cater for scenarios when some of the strings look like guid-s, but should be strings, and some of the strings are actually guid-s

Thanks,
Stevo
0
Atanas Korchev
Telerik team
answered on 25 Nov 2014, 12:20 PM
Hello Stevo,

I am sorry but I don't understand your requirements. Could you provide some test data? How are strings that look like guids different than strings that are guids?

Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Stevo
Top achievements
Rank 1
answered on 01 Dec 2014, 05:04 PM
Hi Atanas,

my point is that the code you've provided will replace all filters that look like guids 'xxxx-xxx...' to a 'guid' filter condition (i.e. remove the apostrophes that are used for string filters)

That is the desired behaviour if the .Net Property that you are filtering on is actually of type Guid. However if the property that you are filtering on is of type String, and it contains a string that looks like a guid, the filtering won't work, since the code will remove the apostrophes.

Does that make sense? Just use whatever solution you have to filter on guid, than change your property type to string (make sure the values in there are guids.ToString()) and watch what happens.

Hope that helps,
Stevo
0
Atanas Korchev
Telerik team
answered on 02 Dec 2014, 07:28 AM
Hello Stevo,

The solution I have given is the only workaround possible. Kendo UI (and JavaScript) don't support a GUID type and this is why a regular expression is needed. Also guids are serialized in JSON as a string.
What I can suggest is to change the regexp so it replaces only strings that fully match a guid (don't just contain a guid):

/^('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')$/ig;


Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Stevo
Top achievements
Rank 1
answered on 02 Dec 2014, 09:20 AM
Hi Atanas,

exactly, kendoui does not cope with guids in OData. OData is a rich interface, getting more popular, having only 4 data types is not going to work very well. 

One way of solving this is if the function kendo.data.transports.odata.parameterMap(options) would be more extensible (i.e. have the ability to override creating filter definition for each filter item), we could support additional data types (like guid) easily.

Also parameterMap arguments don't contain references to the datasource schema model. If we had this, we could extend the schema model with our custom attributes and take them into consideration when creating say the $filter.

Regards,
Stevo



0
Atanas Korchev
Telerik team
answered on 02 Dec 2014, 10:16 AM
Hello Stevo,

Kendo UI doesn't currently support any of those - you can't override the filter creation and parameterMap doesn't have a reference to the data source model. You may consider opening a feature request in our feedback portal about supporting GUID filtering in OData.

For now the suggestion workaround or creating a new custom transport are the only options possible. 

Regards,
Atanas Korchev
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Casimodo
Top achievements
Rank 1
answered on 01 Jul 2015, 08:42 PM

The following is Atana's hack applied to OData V4:

fixODataV4FilterParameterMap = function (data, type) {
    // This is needed for Kendo grid's filters when using OData v4.
  
    // Call the default OData V4 parameterMap.
    var result = kendo.data.transports["odata-v4"].parameterMap(data, type);
  
    if (result.$filter) {
  
        // Remove the single quotation marks around the GUID (OData v4).
        result.$filter = result.$filter.replace(/('[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}')/ig, function (x) {              
            return x.substring(1, x.length - 1);
        });          
    }
    return result;
};

0
Robert
Top achievements
Rank 2
Veteran
answered on 10 Sep 2020, 01:33 AM
JUst an FYI, I've made a pull request to fix this once and for all: https://github.com/telerik/kendo-ui-core/pull/6009.
0
Neli
Telerik team
answered on 14 Sep 2020, 03:26 PM

Hi Robert,

Thank you for suggesting an improvement.

Could you please sign our CLA? Once it is done, please post a comment here and I will forward the PR to a developer from the team. 

Regards,
Neli
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

0
Robert
Top achievements
Rank 2
Veteran
answered on 14 Sep 2020, 03:29 PM
Done, thanks!
0
Neli
Telerik team
answered on 17 Sep 2020, 11:33 AM

Hi Robert,

Thank you for signing the CLA. As you could see in the issue there is a reviewer assigned to the PR. 

Thank you once again for your collaboration.

Regards,
Neli
Progress Telerik

Five days of Blazor, Angular, React, and Xamarin experts live-coding on twitch.tv/CodeItLive , special prizes and more, for FREE?! Register now for DevReach 2.0(20).

Tags
Data Source
Asked by
John
Top achievements
Rank 1
Answers by
Atanas Korchev
Telerik team
Stevo
Top achievements
Rank 1
Casimodo
Top achievements
Rank 1
Robert
Top achievements
Rank 2
Veteran
Neli
Telerik team
Share this question
or