Autocomplete with webapi 2 and odata 4

11 posts, 0 answers
  1. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 16 Sep 2014 Link to this post

    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











  2. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 18 Sep 2014 Link to this post

    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!
     
  3. Kendo UI is VS 2017 Ready
  4. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 18 Sep 2014 in reply to Daniel Link to this post

    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
  5. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 22 Sep 2014 Link to this post

    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!
     
  6. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 22 Sep 2014 in reply to Daniel Link to this post

    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

  7. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 23 Sep 2014 Link to this post

    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!
     
  8. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 23 Sep 2014 in reply to Daniel Link to this post

    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;

              }

          });
      });
  9. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 23 Sep 2014 in reply to Alan Mosley Link to this post

    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
  10. Daniel
    Admin
    Daniel avatar
    2117 posts

    Posted 24 Sep 2014 Link to this post

    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!
     
  11. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010

    Posted 24 Sep 2014 in reply to Daniel Link to this post

    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
  12. Alan Mosley
    Alan Mosley avatar
    200 posts
    Member since:
    Apr 2010
Back to Top
Kendo UI is VS 2017 Ready