Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 420
SL Dev Avatar asked Dec 13 '13 20:12

SL Dev


2 Answers

This answer is probably outdated for current versions of the Kendo Angular bindings. As mentioned in hally9k's answer, there is now an attribute k-ng-model that can handle this, so you would use

k-ng-model="selectedSport"

in place of

ng-model="selectedSport"

Previous answer below; this may or may not still be relevant in case you're using an older version of Kendo UI:

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)

like image 118
Lars Höppner Avatar answered Oct 13 '22 01:10

Lars Höppner


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'" />
like image 38
hally9k Avatar answered Oct 13 '22 02:10

hally9k