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

Autocomplete with webapi 2 and odata 4

10 Answers 238 Views
AutoComplete
This is a migrated thread and some comments may be shown as answers.
Alan Mosley
Top achievements
Rank 1
Alan Mosley asked on 16 Sep 2014, 12:36 PM
I am trying to get autocomplete to filter, with no luck using webapi 2 and odata 4
when i look at the url decoded i see
/odata/Locations?filter[logic]=and&filter[filters][0][value]=mun&filter[filters][0][operator]=contains&filter[filters][0][field]=Name&filter[filters][0][ignoreCase]=true

but looking at webapi odata doc filtering syntax looks different, see
for example the syntax for conatins is
$filter=substringof('zz',Name)

am I missing something?
Thanks











10 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 18 Sep 2014, 10:06 AM
Hello Alan,

Have you set the autocomplete dataSource type to odata? The parameters will not be formatted if the type is not set.
Also, I am not sure if I understand which OData version you are using because the link that your provided describes how to use OData 3.  Could you clarify? The dataSource currently supports only version 2. Adding support for OData 4 is planned for the next release. It is possible to override the built-in functions to add support for server filtering with OData 3 or 4 but the needed changes will depend on the version.


Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Alan Mosley
Top achievements
Rank 1
answered on 18 Sep 2014, 10:23 AM
Thanks, I have tried version 3 and 4.
I have made a hack to get the value out of the request, this was not too hard as I only need the one value.

When do you think we will have the update for oData4.
Do you have any examples of overriding methods for version 4?
Thanks
0
Daniel
Telerik team
answered on 22 Sep 2014, 06:20 AM
Hello again Alan,

The next official release is expected in November.
As for overriding the methods - for version 3 it should be sufficient to override the schema data and total functions:
schema: {
    data: function (data) {
        return data.value;
    },
    total: function (data) {
        return data["@odata.count"];
    }
}
and set the request dataType to "json".
For OData 4 you should also override the transport parameterMap function and replace $inlinecount with $count and substringof with contains:
parameterMap: function (options) {
    var result = kendo.data.transports.odata.parameterMap(options);
  
    if (result.$filter) {
        result.$filter = result.$filter.replace(/substringof\('(\w+)',(\w+)\)/g, "contains($2, '$1')");                           
    }
 
    delete result.$inlinecount;
    result.$count = true;
  
    return result;
}


Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Alan Mosley
Top achievements
Rank 1
answered on 22 Sep 2014, 06:14 PM
Thanks, I tried the code but had no affect.
but looking at the code, is it the wrong way around?
shouldn't we be replacing with substingof, rather then be searching for substringof?

This is what autocomplete is sending
http://localhost:54761/odata/oLocationViews?filter[logic]=and&filter[filters][0][value]=xxxxxx&filter[filters][0][operator]=contains&filter[filters][0][field]=FormatedLocation&filter[filters][0][ignoreCase]=true

this is what I need
$filter=substringof('xxxx',Name)

I am not good enough with regex to get it to work.
Thanks

0
Daniel
Telerik team
answered on 23 Sep 2014, 01:06 PM
Hi Alan,

Could you provide the code that you are currently using? From the provided URL it seems that the parameters are still not formatted with the odata transport.

As for the substringof operator - in version 4 it was changed to contains:
http://docs.oasis-open.org/odata/new-in-odata/v4.0/cn01/new-in-odata-v4.0-cn01.html


Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Alan Mosley
Top achievements
Rank 1
answered on 24 Sep 2014, 12:47 AM
My code thanks

​$(document).ready(function () {


      var dataSource = new kendo.data.DataSource({
          dataType: "json",
          serverFiltering: true,
          transport: {
              read: "/odata/oLocationViews"
          },
          schema: {
              data: function (data) {
                  console.log(data.value);
                  return data.value;
              }
          }
      });


      $("#locations").kendoAutoComplete({
          dataTextField: "FormatedLocation",
          filter: "contains",
          minLength: 3,
          type: "odata",
          dataSource: dataSource,
          select: LocationIdObject.locationIdOnSelect,
          parameterMap: function (options, type) {
              var paramMap = kendo.data.transports.odata.parameterMap(options);
              delete paramMap.$inlinecount;
              delete paramMap.$format;

              return paramMap;

          }

      });
  });
0
Alan Mosley
Top achievements
Rank 1
answered on 24 Sep 2014, 01:42 AM
sorry to makes thing clear,
I have tried changing datatype from odata to json on both datasouce and autocomplete.
I have also changed api to use odata 3, using nugget package odata for webapi 2.2 v1 to 3
still cant get it to work
0
Daniel
Telerik team
answered on 24 Sep 2014, 01:10 PM
Hello Alan,

There are a few configuration issues in the code that you provided:
  • the parameterMap function should be set to the transport and not the grid.
  • the type should be set to the dataSource and not the grid.
  • the dataType should be set to the request options and not the dataSource
var dataSource = new kendo.data.DataSource({
  type: "odata",
  serverFiltering: true,
  transport: {
      read: {
        url: "/odata/oLocationViews",
        dataType: "json",
      },
      parameterMap: function (options, type) {
          var paramMap = kendo.data.transports.odata.parameterMap(options);
          delete paramMap.$inlinecount;
          delete paramMap.$format;
 
          return paramMap;
      }
  },
  schema: {
      data: function (data) {
          console.log(data.value);
          return data.value;
      }
  }
});
 
 
$("#locations").kendoAutoComplete({
  dataTextField: "FormatedLocation",
  filter: "contains",
  minLength: 3,     
  dataSource: dataSource,
  select: LocationIdObject.locationIdOnSelect
});


Regards,
Daniel
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
0
Alan Mosley
Top achievements
Rank 1
answered on 25 Sep 2014, 04:24 AM
Thanks
I had moved things around a lot trying to get things working, after reading many things on internet.
I have working now.

I did have to add
total: function (data) {
                    return data.length;
 
                }

as well as serverfiltering

That got things working but very slow, I had been reading the below article that stats to return an IQueryable, but changing to IEnumerable got working fast.

Thanks for your help once again
0
Alan Mosley
Top achievements
Rank 1
answered on 25 Sep 2014, 04:24 AM
Tags
AutoComplete
Asked by
Alan Mosley
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Alan Mosley
Top achievements
Rank 1
Share this question
or