Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kendoui angular grid selection event

I am trying to handle a selection event from a KendoUI Grid in AngularJS.

I have got my code working as per below. However it feels like a really nasty way of having to get the data for the selected row. Especially using _data. Is there a better way of doing this? Have I got the wrong approach?

<div kendo-grid k-data-source="recipes" k-selectable="true" k-sortable="true" k-pageable="{'refresh': true, 'pageSizes': true}"
            k-columns='[{field: "name", title: "Name", filterable: false, sortable: true},
            {field: "style", title: "Style", filterable: true, sortable: true}]' k-on-change="onSelection(kendoEvent)">
</div>

$scope.onSelection = function(e) {
  console.log(e.sender._data[0].id);
}
like image 609
SimRo Avatar asked Sep 04 '13 06:09

SimRo


2 Answers

Joining the party rather late, there is a direct way to do it without reaching for the grid object:

on the markup:

k-on-change="onSelection(data)"

in the code:

$scope.onSelection = function(data) {
    // no need to reach the for the sender
}

note that you may still send selected, dataItem, kendoEvent or columns if needed.

consult this link for more details.

like image 142
jajdoo Avatar answered Oct 12 '22 19:10

jajdoo


Directive for two-way binding to selected row. Should be put on the same element as kendo-grid directive.

Typescript version:

interface KendoGridSelectedRowsScope extends ng.IScope {
        row: any[];
    }

// Directive is registered as gridSelectedRow
export function kendoGridSelectedRowsDirective(): ng.IDirective {
        return {
            link($scope: KendoGridSelectedRowsScope, element: ng.IAugmentedJQuery) {

                var unregister = $scope.$parent.$on("kendoWidgetCreated", (event, grid) => {
                    if (unregister)
                        unregister();

                    // Set selected rows on selection
                    grid.bind("change", function (e) {

                        var selectedRows = this.select();
                        var selectedDataItems = [];

                        for (var i = 0; i < selectedRows.length; i++) {
                            var dataItem = this.dataItem(selectedRows[i]);
                            selectedDataItems.push(dataItem);
                        }

                        if ($scope.row != selectedDataItems[0]) {

                            $scope.row = selectedDataItems[0];
                            $scope.$root.$$phase || $scope.$root.$digest();
                        }
                    });


                    // Reset selection on page change
                    grid.bind("dataBound", () => {
                        $scope.row = null;
                        $scope.$root.$$phase || $scope.$root.$digest();
                    });

                    $scope.$watch(
                        () => $scope.row,
                        (newValue, oldValue) => {
                            if (newValue !== undefined && newValue != oldValue) {
                                if (newValue == null)
                                    grid.clearSelection();
                                else {
                                    var index = grid.dataSource.indexOf(newValue);
                                    if (index >= 0)
                                        grid.select(grid.element.find("tr:eq(" + (index + 1) + ")"));
                                    else
                                        grid.clearSelection();
                                }
                            }
                        });
                });
            },
            scope: {
                row: "=gridSelectedRow"
            }
        };
    }

Javascript version

function kendoGridSelectedRowsDirective() {
        return {
            link: function ($scope, element) {
                var unregister = $scope.$parent.$on("kendoWidgetCreated", function (event, grid) {
                    if (unregister)
                        unregister();
                    // Set selected rows on selection
                    grid.bind("change", function (e) {
                        var selectedRows = this.select();
                        var selectedDataItems = [];
                        for (var i = 0; i < selectedRows.length; i++) {
                            var dataItem = this.dataItem(selectedRows[i]);
                            selectedDataItems.push(dataItem);
                        }
                        if ($scope.row != selectedDataItems[0]) {
                            $scope.row = selectedDataItems[0];
                            $scope.$root.$$phase || $scope.$root.$digest();
                        }
                    });
                    // Reset selection on page change
                    grid.bind("dataBound", function () {
                        $scope.row = null;
                        $scope.$root.$$phase || $scope.$root.$digest();
                    });
                    $scope.$watch(function () { return $scope.row; }, function (newValue, oldValue) {
                        if (newValue !== undefined && newValue != oldValue) {
                            if (newValue == null)
                                grid.clearSelection();
                            else {
                                var index = grid.dataSource.indexOf(newValue);
                                if (index >= 0)
                                    grid.select(grid.element.find("tr:eq(" + (index + 1) + ")"));
                                else
                                    grid.clearSelection();
                            }
                        }
                    });
                });
            },
            scope: {
                row: "=gridSelectedRow"
            }
        };
    }
like image 37
STO Avatar answered Oct 12 '22 19:10

STO