将项目添加到 kendoDropDownList

Add Item To kendoDropDownList

我正在尝试向 kendoDropDownList 添加一个项目,它似乎添加了选项但没有设置值和文本。检查 select 它只是添加了一个新选项

 <option></option>

这是我正在使用的

 $("#nameList").data("kendoDropDownList")
     .dataSource.add({ "text": "Joe", "value": "Joe" });

更新

这是我的数据源模型和建议的 requestEnd,但值似乎搞砸了

datasource_people = new kendo.data.DataSource({
        type: "json",
        serverFiltering: true,
        transport: {
            read: {
                dataType: 'json',
                type: 'GET',
                url: '/restful/people/'
            }
        },
        filter: {
            field: "status",
            operator: "eq",
            value: 1
        },
        schema: {
            data: function(response) {
                return response.data.plaintiffs;
            },
            model: {
                id: "person_id",
                fields: {
                    person_id: {type: "number"},
                    name: { type: "string"}
                }
            },
            errors: "error",
            total: function(response) {
                return response.data.total;
            }
        }
    });

之后

$("#people_list").kendoDropDownList({
                    dataTextField: "text",
                    dataValueField: "value",
                    dataSource: {
                        datasource_people,
                        requestEnd: function (e) {
                            e.response.push({text: "John", value: "John"});
                        }
                    }
                });

您可以在使用 requestEnd 加载数据源后将新项目添加到数据源。

requestEnd: function (e) {
  e.response.push({text: "Joe", value: "Joe"});
}

我已经更新了其他用户的 fiddle 以向您展示它是有效的。 jsFiddle example

使用这个:

<input id="nameList" value="1" />
var data = [
    { text: "Joe", value: "Joe" },
    { text: "Joe", value: "Joe"  },
    { text: "Joe", value: "Joe"  }
];

// create DropDownList from input HTML element
$("#nameList").kendoDropDownList({
    dataTextField: "text",
    dataValueField: "value",
    dataSource: data,
    index: 0,         
});

DEMO

不确定您到底想要什么,但您可以从数据源加载选项,然后在 dataBound 触发后将更多选项附加到列表。

$("#nameList").kendoDropDownList({
    dataTextField: "text",
    dataValueField: "value",
    dataSource: {
        transport: { 
            read: { 
                dataType: 'json', 
                type: 'GET', 
                url: '/restful/companies' 
            }
        } 
    },
    dataBound:onDataBound
});

<script>
    function onDataBound(e) {
        e.sender.dataSource.add({ "text": "Joe", "value": "Joe" });
    }
</script>

经过一些搜索,它非常简单,但这正是我需要的。

$("#people_list").getKendoDropDownList().dataSource.insert({
    person_id: "John",
    name: "John"
})