具有一个DataSource Kendo的多个下拉列表

时间:2016-11-29 14:27:15

标签: javascript kendo-ui datasource kendo-dropdown

我有一个页面,为用户提供5个带有安全问题的下拉列表。它们是通过本地DataSource对象设置的,基本上是对象中的10个问题。我想将所有5个下拉列表绑定到同一个DataSource,并选择一个Question,将其从DataSource中删除,因此您无法为下一个问题选择它。到目前为止,这是我的代码:

var questions =
        [{
            value: "Your first pet\'s name?"
        },
            {
                value: "Your favorite teacher?"
            },
            {
                value: "The city you were born in?"
            },
            {
                value: "Your mother\'s maiden name?"
            },
            {
                value: "The high school you attended?"
            },
            {
                value: "First name of the first person you kissed?"
            },
            {
                value: "What did you want to be when you grow up?"
            },
            {
                value: "The brand of your first car?"
            },
            {
                value: "Your favorite city?"
            }];
    var localDataSource = new kendo.data.DataSource({
        data: questions
    });
    var dropdown = $('.dropdownlist');
        dropdown.kendoDropDownList({
            dataTextField: "value",
            dataValueField: "value",
            dataSource: localDataSource
        });

我的HTML用于渲染字段:

<input class="dropdownlist w250px" name="questions[1][question]" />

每个问题的时间5。

1 个答案:

答案 0 :(得分:2)

要实现所需的行为,您可以使用相同的数据,但单独的DataSource实例,否则您将无法为每个DropDownList以不同方式过滤它们。< / p>

以下是一个示例,您可以将其用作起点并进一步对其进行自定义以符合您的方案。

http://dojo.telerik.com/aJeHa

使用的API包括:

<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>Related Kendo UI DropDownLists</title>

    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1118/styles/kendo.common.min.css">
    <link rel="stylesheet" href="http://kendo.cdn.telerik.com/2016.3.1118/styles/kendo.default.min.css">

    <script src="http://code.jquery.com/jquery-1.12.3.min.js"></script>
    <script src="http://kendo.cdn.telerik.com/2016.3.1118/js/kendo.all.min.js"></script>
  </head>
  <body>

    <p><input id="ddl1" name="ddl1" class="related-dropdown" /></p>
    <p><input id="ddl2" name="ddl2" class="related-dropdown" /></p>
    <p><input id="ddl3" name="ddl3" class="related-dropdown" /></p>

    <script>

      var data = [
        { id: 1, text: "question 1" },
        { id: 2, text: "question 2" },
        { id: 3, text: "question 3" }
      ];

      for (var j = 1; j <= data.length; j++) {
        $("#ddl" + j).kendoDropDownList({
          dataSource: {
            data: data,
            filter: {}
          },
          dataTextField: "text",
          dataValueField: "id",
          optionLabel: "Select a question",
          change: onChange
        });
      }

      function onChange(e) {
        if (e.sender.value()) {
          var otherDropDowns = $("input.related-dropdown").not(e.sender.element);
          for (var j = 0; j < otherDropDowns.length; j++) {
            var ddl = $(otherDropDowns[j]).data("kendoDropDownList");
            var newCondition = { field: "id", operator: "neq", value: e.sender.value() };
            var currentFilter = ddl.dataSource.filter();
            if (currentFilter && currentFilter.filters) {
              currentFilter.filters.push(newCondition);
              ddl.dataSource.filter(currentFilter);
            } else {
              ddl.dataSource.filter(newCondition);
            }
          }
        } else {
          // clear the freed question from the DropDownLists' filter criteria
        }
      }

    </script>

  </body>
</html>