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

Datasource Read loses params with paging

2 Answers 252 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Marcel
Top achievements
Rank 1
Marcel asked on 09 Sep 2013, 01:46 PM
Hello,
I'm using a kendo.data.Datasource with serverPaging and ServerSorting options in an MVVM implementation.
This datasource is roughly defined like this:
var MyDatasource = new kendo.data.DataSource({
  type: 'odata' /* for odata style IQueryable web api */
  serverPaging: true,
  serverSorting: true,
  pageSize: 15,
  transport: {
      read: {
          dataType: "json",
          url: "mywebapi/mydata"
})

The DataSource is used by a Kendo grid with scrollable: { virtual: true }.
A user has the ability to search for data where the searchparam is set on my viewmodel.
within my viewmodel i simply call "MyDatasource.read({ searchvalue: userparam })" to get the data from my web-api which works perfectly.

However, in combination with paging the parameter is lost when reading the next page.

Is there any way to preserve that param so it gets send with the next pages as well ?

Thanks in advance.

2 Answers, 1 is accepted

Sort by
0
Daniel
Telerik team
answered on 11 Sep 2013, 10:42 AM
Hello Marcel,

You should use the request data to send the parameter each time: 

read: {
    dataType: "json",
    url: "mywebapi/mydata",
    data: function (e) {
        return { searchvalue: userparam }
    }   
}

The object passed with the read method will be used only once. 

Regards,
Daniel
Telerik
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
0
Marcel
Top achievements
Rank 1
answered on 11 Sep 2013, 11:22 AM
Thanks for the answer Daniel.
The problem in this case is that the DataSource is created in a generic way since i'm a lazy developer and hate redundant code.
So i solved it in another way.
transport: {
    read: {
        dataType: "json",
        url: function () {
            return restUri;
        },
        data: function (args) {
            if (args.params) {
                this.params = args.params;
            }
 
            delete args.params;
 
            if (this.params) {
                for (var param in this.params) {
                    if (this.params[param] !== null)
                        args[param] = this.params[param];
                    else
                        delete args[param];
                }
            }
        }
    },
MyDataSource.read({ params: { searchvalue: value } });

This way the params are preserved by the DataSource itself and will be used with further requests.
Tags
Data Source
Asked by
Marcel
Top achievements
Rank 1
Answers by
Daniel
Telerik team
Marcel
Top achievements
Rank 1
Share this question
or