SL Dev
SL Dev

Reputation: 865

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!

Upvotes: 7

Views: 27461

Answers (4)

Lars H&#246;ppner
Lars H&#246;ppner

Reputation: 18402

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)

Upvotes: 9

dalcam
dalcam

Reputation: 1077

The proper way to get the text value is to use the 'k-select' event of Kendos dropdownlist:

<select kendo-drop-down-list k-select="vm.test" k-data-text-field="'groupName'" k-data-value-field="'id'" k-data-source="vm.groupList" ng-model="vm.groupId"></select>

Then in your angular controller expose the 'test' function (example assumes you are using 'controller as vm'):

function DocumentTypeController() {
  var vm = this;
  vm.test = test;
  vm.groupId = null;

  function test(dropdown) {
    alert('text is:' + dropdown.item.text());
  }
}

I hope that helps.

Upvotes: 0

hally9k
hally9k

Reputation: 2583

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'" />

Upvotes: 6

Lars335
Lars335

Reputation: 1880

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.

Upvotes: 0

Related Questions