3
votes

I am using the Kendo UI package for Angular JS. I would like to use an Angular template for the row detail, very similar to what is done here:

Kendo Grid Detail Template

Essentially I would like to fetch some data when details are expanded, and pass an angular model to the template. Is this possible?

1

1 Answers

2
votes

What I have done (so far) with this same need is use the changed event on the Grid to populate a 'SelectedRow' object on the $scope in my controller. For the DetailTemplate, I have a div that contains a directive that loads the template either from the $templateCache or using $http and compiles it and links it to the $scope. One of the problems with templates is compiling and linking them to the $scope and the timing of when that takes place. (My problem was even worse as I needed a different detail template for each row)

                $scope.vm.options.productGridOptions = {
                dataSource: new kendo.data.DataSource({
                    data: $scope.vm.solution.Products,
                    pageSize: 10
                }),
                change: $scope.vm.events.productSelected,
                columns: $scope.vm.columns.productColumns,
                detailTemplate: '<div data-template-detail type="#= EntityTemplateSK #"></div>',
                filterable: false,
                groupable: false,
                pageable: true,
                reorderable: true,
                resizable: true,
                selectable: 'single',
                sortable: true
            };

 myApp.directive('templateDetail', ['$compile', '$http', '$templateCache', 
        function ($compile, $http, $templateCache) {
            var detailTemplateNumbers = ['21', '22', '23', '26', '45', '69'];
            var getTemplate = function (templateNumber) {
                var baseUrl = '/App/Product/Views/',
                    templateName = 'productdetail.html',
                    templateUrl = baseUrl + templateName;

                if (detailTemplateNumbers.filter(function (element) { return element === templateNumber; })[0]) {
                    templateName = 'productTemplate' + templateNumber + '.html';
                    templateUrl = baseUrl + templateName;
                }

                return $http.get(templateUrl, { cache: $templateCache });
            };

            var linker = function ($scope, element, attrs) {

                var loader = getTemplate(attrs.type.toString());

                if (loader) {
                    loader.success(function (html) {
                        element.html(html);
                    }).then(function () {
                        element.replaceWith($compile(element.html())($scope.$parent));
                    });
                }
            };

            return {
                restrict: 'A',
                scope: {
                    type: '='
                },
                link: linker
            };
    }]);