【问题标题】:Kendo DropDownList get element from json data other than dataTextField or dataValueFieldKendo DropDownList 从 dataTextField 或 dataValueField 以外的 json 数据中获取元素
【发布时间】:2014-04-17 12:31:45
【问题描述】:

所以我有两个下拉列表,我想从第二个下拉列表中获取另一个 json 属性,而不是归因于 dataTextField 或 dataValueField 的属性。 这是引用的下拉列表:

$("#campoFormLinha"+index).kendoDropDownList({

        optionLabel: "Campo",
        dataTextField: "name",
        dataValueField: "id",
        dataSource: {
            type: "json",
            serverFiltering:true,
            transport: {

                read:{
                    url:"${pageContext.request.contextPath}" + "/newlayout/mySearchesFormFieds.do",
                    data:function(){
                        return {formId: $("#dynamicFormLinha"+index).val()
                        };
                    }
                }
            }
        },
        cascadeFrom: "dynamicFormLinha"+index
    }).data("kendoDropDownList");

这是它返回的 json:

[{"id":9,"name":"Cliente","type":"STRING"},{"id":10,"name":"Contribuinte","type":"STRING" },{"id":11,"name":"Facturação","type":"STRING"},{"id":12,"name":"Conta","type":"STRING"}, {"id":13,"name":"Factura","type":"STRING"},{"id":14,"name":"Valor","type":"STRING"}]

假设所有这些,我想根据所选选项获取类型属性。

我该怎么做?

【问题讨论】:

  • 你能描述一下你有什么和你想要什么吗?
  • 您好!目前一切正常,我只希望属性“type”:“STRING”可以访问。 “id”和“name”已经可以通过 dataTextField 和 dataValueField 访问,但我想访问绑定到所选选项的 type 属性。我认为这是可能的,我尝试使用这个选择器:$("#campoFormLinha1").data("kendoDropDownList").dataSource._data,但这给了我所有的选项数据。
  • 你想要"type":"STRING" of selected item,对吧?

标签: javascript json kendo-ui telerik kendo-dropdown


【解决方案1】:

请尝试以下代码 sn-p。

<script type="text/javascript">
    function getSlectedItem() {
        var ddl = $("#color").data("kendoDropDownList");
        alert(ddl.dataSource.data()[ddl.selectedIndex - 1].type);
    }

    function onSelect(e) {
        alert(e.sender.dataSource.data()[e.item.index() - 1].type);
    }

    $(document).ready(function () {
        var data = [
                { text: "Black", value: "1", type: "string" },
                { text: "Orange", value: "2", type: "int" },
                { text: "Grey", value: "3", type: "string" }
            ];

        $("#color").kendoDropDownList({
            dataTextField: "text",
            dataValueField: "value",
            dataSource: data,
            optionLabel: "select",
            select: onSelect
        });
    });

</script>
  1. 选择后您将获得该项目的类型字段
  2. 单击按钮后,您还将获得该项目的类型字段

如果有任何问题,请告诉我。

【讨论】:

  • 哦,我看到你更新了。请问onSelect中的参数传递的是什么?
  • 您还有什么顾虑吗?
  • 查看我之前的问题。 :)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多