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

Setting an optional data in Datasource

1 Answer 187 Views
Grid
This is a migrated thread and some comments may be shown as answers.
SENG PHING
Top achievements
Rank 1
SENG PHING asked on 11 Apr 2016, 12:00 AM

Hi,

I would like to have a private method that creates a Datasource to be used. This method can create a datasource either with a data object to send to the URL or one without a data object to send to the URL.

How can I go about achieving this?

 

   ChangeGridDataSource: function (newDatasourceUrl, gridName, dataToSend) {
            // Sets the grid to a new datasource and forces it to read
            var gridId;
            if (gridName[0] !== '#') {
                gridId = '#' + gridName;
            } else {
                gridId = gridName;
            }

            var newDataSource = new kendo.data.DataSource({
                transport: {
                    read: {
                        url: newDatasourceUrl,
                        type: 'POST',
                        dataType: 'JSON'
                    }
                },
                pageSize: 20,
                schema: {
                    model: {
                        fields: {
                            // the model fields have to specified that it is of a date type object.
                            dSECSYSTimeStampDate: { type: 'date' },
                            dExecutedDateValue: { type: 'date' },
                            dEffectiveDateValue: { type: 'date' }
                        }
                    },
                    data: 'Data',
                    parse: function (response) {
                        // This will parse the dates and transform it into the proper date format. This is done 
                        // after the date has been retrieved in a JSON format. This will allow the formatting of the dates
                        // to be done in the grids.
                        return Transactions.Methods.ParseDatesFromKendoGrid(response);
                    },
                    total: function (response) {
                        return response.Total;
                    }
                }
            });

            if (typeof dataToSend !== 'undefined') {
                // Does not seem to work.
                newDataSource.transport.read.data = dataToSend;
            }

            console.log(newDataSource);
            console.log(dataToSend);
            $(gridId).data('kendoGrid').setDataSource(newDataSource);
        },

 

I have the above and the line 'newDataSource.transport.read.data = dataToSend' doesn't seem to work for some reason. Is there a method that I can call or a different property that can be used to change a Data Object to send in side the data source?

1 Answer, 1 is accepted

Sort by
0
Rosen
Telerik team
answered on 12 Apr 2016, 02:22 PM

Hello SENG PHING,

In order to send additional request data, you should use the transport.read.data option. Note that this should be set during initial declaration of the DataSource. The actual data value can be a map where the key is the parameter's name and the value is either the value or a function which to be called when request is about to be made. Both of this usages are demonstrated in the API docs here.

Regards,
Rosen
Telerik
 
Join us on our journey to create the world's most complete HTML 5 UI Framework - download Kendo UI now!
 
Tags
Grid
Asked by
SENG PHING
Top achievements
Rank 1
Answers by
Rosen
Telerik team
Share this question
or