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

Passing parameters to DataSource

2 Answers 1015 Views
Data Source
This is a migrated thread and some comments may be shown as answers.
Kate | D-Flo
Top achievements
Rank 1
Kate | D-Flo asked on 01 Nov 2013, 05:56 PM
Hello,

First of all, sorry for asking the question which has been asked many times before! I looked around, and I seem to be doing everything right, but still getting no result.
Here's the problem: I have a Grid with DataSource with autoBind set to false. Upon a certain action, I'd like to pass additional parameters to DataSource, and to read the data. However, after setting the parameters, when "read" happens I can see in the "Network" tab in Dev Tools, that my additional parameters are not sent. I have tried to do it two different ways:

1. Set transport read data:
    $('#lookupDataGrid').data('kendoGrid').dataSource.transport.read.data ={"UserLookupTableID":123};
console.log($('#lookupDataGrid').data('kendoGrid').dataSource.transport.read.data); // outputs correct object
    $('#lookupDataGrid').data('kendoGrid').dataSource.read();
The network tab shows this (my additional parameter is not there): 
.../lookup-tables/get-lookup-data?take=15&skip=0&page=1&pageSize=15

2. Change the read URL to include my parameter:
var url = '.../lookup-tables/get-lookup-data?UserLookupTableID=123;
$('#lookupDataGrid').data('kendoGrid').dataSource.transport.read.url = url;
$('#lookupDataGrid').data('kendoGrid').dataSource.read();
The network tab shows the same output as above!! It is as if the chances I'm making are ignored...

I'm stumped. Am I don't something wrong? Please let me know...

2 Answers, 1 is accepted

Sort by
0
Andrew
Top achievements
Rank 1
answered on 01 Nov 2013, 08:42 PM
The read() method of DataSource can take parameters. Did you try:
$('#lookupDataGrid').data('kendoGrid').dataSource.read({"UserLookupTableID":123});
0
Accepted
Kate | D-Flo
Top achievements
Rank 1
answered on 04 Nov 2013, 10:48 AM
Hi Andrew,

Your solution worked exactly right - but only for the initial call. When the automatic calls are done later for paging/filtering, that data is missing. 

I have actually found a solution on how to make the data persist, which I'll post here in case its helpful to someone in the future.

When I generate the grid, I set the parameterMap to a method (setParameterMapForDataTable). On the initial call (editLookupTableButtonClick) I save the the variable, and then use ParameterMap to make sure that this field is set every time Read is executed:

var viewLookupTableID;
 
function setParameterMapForDataTable(data, type) {
    if(type == 'read') {
        data.UserLookupTableID = viewLookupTableID;
    }
    return data;
}
 
function editLookupTableButtonClick(e){
    var selectedRows = this.select();
    var dataItem = this.dataItem(selectedRows[0]);
    viewLookupTableID = dataItem.ID;
 
    $('#lookupDataGrid').data('kendoGrid').dataSource.read();
}
Thank you.
Tags
Data Source
Asked by
Kate | D-Flo
Top achievements
Rank 1
Answers by
Andrew
Top achievements
Rank 1
Kate | D-Flo
Top achievements
Rank 1
Share this question
or