级联kendoDropDownList,在第一个下拉列表中获取所选文本

时间:2016-03-29 13:57:11

标签: kendo-ui cascadingdropdown

我对Kendo UI相当新,并在此处获取了我的代码的基础知识:http://demos.telerik.com/kendo-ui/dropdownlist/cascadingdropdownlist

我有2个api调用,其中第一个没有参数,如果项目(Id,Name)则返回一个列表 第二个api调用接收一个Id,并返回一个秒的项目列表(也只是一个带有Id和Name的对象) 从此我希望有2个级联的剑道下拉菜单。 但是我的问题是第二个问题,网址的id始终为null或空,我无法弄清楚正确的语法是什么:

            // First dropdown, all good
            var controllers = $("#Controller").kendoDropDownList({
                optionLabel: "Select controller...",
                dataTextField: "Name",
                dataValueField: "Id",
                dataSource: {
                    serverFiltering: true,
                    transport: {
                        read: "/SharedData/GetControllers/"
                    }
                }
            }).data("kendoDropDownList");

            // second dropdown, always hit the api method with the id being null or empty  (depending on syntax for url)
            var actions = $("#Action").kendoDropDownList({
                autoBind: true,
                cascadeFrom: "controllers",
                cascadeFromField: "Id",
                optionLabel: "Select Action...",
                dataTextField: "Id",
                dataValueField: "Name",
                dataSource: {
                    serverFiltering: true,
                    transport: {
                        // HELP:  need pass id to this route (which is id of selected controller)
                        read: "/SharedData/GetControllerActions/id=" + $("#Controller").data("kendoDropDownList").text()
                    }
                }
            }).data("kendoDropDownList");

1 个答案:

答案 0 :(得分:1)

我认为问题是您的数据源只在初始化时设置了一次 - 并且此时下拉列表的值为null。我要做的是在第一个下拉列表中添加一个更改事件,如下所示:

        var controllers = $("#Controller").kendoDropDownList({
            optionLabel: "Select controller...",
            dataTextField: "Name",
            dataValueField: "Id",
            dataSource: {
                serverFiltering: true,
                transport: {
                    read: "/SharedData/GetControllers/"
                }
            },
            change: function(e) {
               setSecondDS();
            }
        }).data("kendoDropDownList");

       var setSecondDS = function() {
           //initialize your new kendo datasource here
           var dataSource = new kendo.data.DataSource({
               //your stuff here
               transport:
               serverFiltering:
           });

          $("#Action").data("kendoDropDownList").setDataSource(dataSource); 
       }