Get Selected Object Of Kendo Drop Down List

I am using the Kendo Drop Down List. More specifically, I'm using Kendo Angular directives. Currently, I have the following in my markup:

<input id='myDropDownList' kendo-drop-down-list ng-model="selectedSport" k-data-source="sports" k-data-text-field="'name'" />
<button ng-click='send()'>Submit</button>

My controller has the following:

$scope.selectedSport = null;
$scope.sports: [
  { id: 1, name: 'Basketball' },
  { id: 2, name: 'Football' },
  { id: 3, name: 'Tennis' }
];

$scope.send = function () {
  alert($scope.selectedSport);
};

When I run this code, I get the selectedSport ID. However, I want the entire object. Every other StackOverflow post I've found uses retrieves the ID. For the life of me, I can't figure out how to get the object. Does anyone know how to get the selected JSON object instead of just the id?

Thanks!

I don't think you can configure the kendo widget to store the dataItem directly - underneath it all is still a <select> with a primitive value. So you'll probably have to get the dataItem from the widget's data source, e.g. like this:

HTML:

<div ng-controller="MyController">
    <select id='myDropDownList' kendo-drop-down-list ng-model="selectedSport" k-data-source="sports" k-data-value-field="'id'" k-data-text-field="'name'"></select>
    <button ng-click='send()'>Submit</button>
</div>

JS:

function MyController($scope) {
    $scope.selectedSport = null;
    $scope.sports = new kendo.data.DataSource({
        data: [{
            id: 1,
            name: 'Basketball'
        }, {
            id: 2,
            name: 'Football'
        }, {
            id: 3,
            name: 'Tennis'
        }]
    });

    $scope.send = function () {
        var dataItem = $scope.sports.get($scope.selectedSport);

        console.log(dataItem);
    };
}

You could, however, create your own directive for kendoDropDownList which uses a k-data-item attribute (for example) and use it like this:

HTML:

<select id='date' k-ddl k-data-source="sports" k-data-text-field="name" k-data-item="dataItem">

JS:

var app = angular.module('Sample', ['kendo.directives']).directive("kDdl", function () {
    return {
        link: function (scope, element, attrs) {
            $(element).kendoDropDownList({
                dataTextField: attrs.kDataTextField,
                dataValueField: "id",
                dataSource: scope[attrs.kDataSource],
                change: function () {
                    var that = this;
                    var item = that.dataItem();

                    scope.$apply(function () {
                        scope[attrs.kDataItem] = item.toJSON();
                    });
                }
            });
        }
    };
});

function MyController($scope) {
    $scope.sports = [{
        id: 1,
        name: 'Basketball'
    }, {
        id: 2,
        name: 'Football'
    }, {
        id: 3,
        name: 'Tennis'
    }];
    $scope.dataItem = $scope.sports[0];
    $scope.send = function () {
        console.log($scope.dataItem);
    };
}

That way, you could keep the controller free from Kendo UI DataSource-specific code and instead only work with JS data types. (see JSBin)

Using k-ng-model will bind the dataItem instead of just the text value:

<input id='myDropDownList' kendo-drop-down-list k-ng-model="selectedSport" k-data-source="sports" k-data-text-field="'name'" />

I know this is an old question, but you could use the select event of the dropdownlist to get the underlying json object:

select: function (e) {
    var item = this.dataItem(e.item.index());
    ...
}

You would then store the json object (item variable above) from the select event so that you can get to it from your submit method. There is probably a way to get the selected json object without having to use the select event as well.