Add Item To kendoDropDownList
Asked Answered
V

4

5

I am trying to add a item to the kendoDropDownList, it seems to add the option but not set the value and text. Inspecting the select it just adds a new option

 <option></option>

Here is what I am using

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

UPDATE

Here is my datasource model and the requestEnd as suggested but the values seem to get messed up

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;
            }
        }
    });

Then Later

$("#people_list").kendoDropDownList({
                    dataTextField: "text",
                    dataValueField: "value",
                    dataSource: {
                        datasource_people,
                        requestEnd: function (e) {
                            e.response.push({text: "John", value: "John"});
                        }
                    }
                });
Venesection answered 12/5, 2015 at 20:24 Comment(0)
V
13

After some searching it was quite simple but this worked for exactly what i needed.

$("#people_list").getKendoDropDownList().dataSource.insert({
    person_id: "John",
    name: "John"
})
Venesection answered 13/5, 2015 at 19:57 Comment(0)
G
4

You can add a new item to the datasource after it has been loaded using requestEnd.

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

I've updated the other user's fiddle to show you it works. jsFiddle example

Gary answered 12/5, 2015 at 20:31 Comment(3)
This seems to work and is more a long the lines of what i need, but for some reason all of the values and text display as 'undefined' only when selected. Strange.Venesection
An up vote or mark as answered would be great since it solves your problem. You can post another question with the 'undefined' problem.Gary
btw, your dataTextField and dataValueField fields are wrong.Gary
F
1

Use this:

<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

Fig answered 12/5, 2015 at 20:32 Comment(3)
Thanks, but I should have mentioned that I already have options loaded in the list. This method would clear all of my current options rather than append to the list.Venesection
How could I call my original datasource along with the new data? transport: { read: { dataType: 'json', type: 'GET', url: '/restful/companies' } }Venesection
You can push in data new data, And after run again thus scriptFig
T
1

Not sure what exactly you want, but you can load options from a datasource, and then append more to the list after dataBound has fired.

$("#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>
Trivia answered 12/5, 2015 at 21:58 Comment(2)
We're using the data- attributes to initialize the drop down. I can't find any information on a data- attribute for dataBoundVenesection
I also tried this but does not seem to work: function onDataBound(e) { e.sender.dataSource.add({ text: "John", value: "John" }); } var dropdownlist = $("#nameList").data("kendoDropDownList"); dropdownlist.bind("dataBound", onDataBound);Venesection

© 2022 - 2024 — McMap. All rights reserved.