Search code examples
javascriptkendo-uikendo-comboboxkendo-dropdown

Updating kendo dataSource via transport


I'm trying to set the remote data url based on the selected value from another dropdownlist (cboSearchString1DDL.element[0].value). I continue to learn the Kendo web controls so I'm not sure how to most efficiently complete this task. I tried a couple avenues but still am getting an error when the dataSource is read. I'm trying to set it onclose of DDL1. Here's where I'm at, I know it's not the correct way:

// search string 1 DDL
var cboSearchString1DDL = $("#cboSearchString1DDL").kendoDropDownList({
    autoBind: false,
    optionLabel: " "
}).data("kendoDropDownList");

// search string 2 DDL
var cboSearchString2DDL = $("#cboSearchString2DDL").kendoDropDownList({
    autoBind: false,
    optionLabel: " "    
}).data("kendoDropDownList");

// 1st DDL dataSource                   
var dataSourceTowns = new kendo.data.DataSource({                   
    transport: {       
       read: {
           url: _urlTowns,
           dataType: "json",
        }
    },
    schema: {
        data: "Towns"
    }
});

$("#cboSearchString1DDL").data("kendoDropDownList").wrapper.show();

// 1st DDL                               
var townsDDL = $("#cboSearchString1DDL").kendoDropDownList({
    autoBind: false,
    dataTextField: "Column1",
    dataValueField: "Column1",
    dataSource: dataSourceTowns,
    close: function () {
        alert(cboSearchString1DDL.element[0].value);
        streetsDDL.enable(true);
        streetsDDL.setDataSource(dataSourcestreetsDDL);
        streetsDDL.refresh();
    }
}).data("kendoDropDownList");

// show 2nd DDL 
$("#cboSearchString2DDL").data("kendoDropDownList").wrapper.show();
cboSearchString2DDL.enable(false);

// 2nd DDL datasource 
var dataSourcestreetsDDL = new kendo.data.DataSource({
    transport: {
        read: {
            url: _urlSOESearchAddress + "townName=" + cboSearchString1DDL.element[0].value + "&f=",
        dataType: "json",
        },
    },
    schema: {
        data: "StreetsinTown"
    }
});

// 2nd DDL
var streetsDDL = $("#cboSearchString2DDL").kendoDropDownList({
    autoBind: false,
    dataTextField: "Street",
    dataValueField: "Street",
    requestStart: function (e) {
        console.log("request started");
    },
    requestEnd: function (e) {
        var response = e.response;
        var type = e.type;
        console.log(type);
        console.log(response.length);
    }
}).data("kendoDropDownList");

Thanks in advance!


Solution

  • Keep in mind that the url can be a function that returns the URL as a string. So if it is easier to code, you could also do something like:

    var dataSourcestreetsDDL = new kendo.data.DataSource({
        transport: {
            read: {
                url: function () {
                    var searchAddress = ...;
                    var searchString = ...;
                    return searchAddress + "townName=" + searchString + "&f=";
                }
            ...
    

    The function will be called whenever the datasource needs to read data. It might be easier than changing things when the dropdown closes.