Spencer Ruport
Spencer Ruport

Reputation: 35117

Struggling with Kendo Grid datasource and Angular.js

I tried to make a simple example of adding a new item to a kendo grid data source but I can't seem to get it to work. The item is added to the array but the grid never updates. Is this supposed to be automatic or do I have to make a call to trigger the update?

HTML:

<kendo-grid source="people" drop="selectedPeople" groupable="true" sortable="true" columns="columns" pageable="true"></kendo-grid>
<input type="text" ng-model="nameInput">
<input type="number" ng-model="ageInput">
<button ng-click="onAdd()" type="button">Add</button>

JS:

var myApp = angular.module('myApp', []).controller('Tester', ['$scope', Tester]);

myApp.directive('kendoGrid', function() {
    return {
        restrict: 'E',
        replace: true,
        scope:{source:'=source',columns:'=columns',drop:'=drop'},
        template: '<div id="kendogrid"></div>',
        link: function(scope,element,attrs) {
            element.kendoGrid({
                        dataSource: scope.source,
                        groupable: attrs.groupable,
                        sortable: attrs.sortable,
                        pageable: {
                            refresh: true,
                            pageSizes: true
                        },
                        columns: scope.columns
                    });
        }
    };
});



function Tester($scope) {

        $scope.columns = [ {
                                field: "name",
                                width: 90,
                                title: "First Name"
                            } , {
                                field: "age",
                                width: 90,
                                title: "Last Name"
                            } , {
                                field: "id",
                                hidden: true
                            }
                        ];
    var man1 = new Man('name1', 25, 1);
    var man2 = new Man('name2', 28, 2);
    var man3 = new Man('name3', 21, 3);
    var man4 = new Man('name4', 29, 4);
    var man5 = new Man('name5', 22, 5);
    var lastId = 5;
    $scope.onAdd = function(){
        if($scope.nameInput !== "" && $scope.ageInput !== "")
        {
            lastId++;
            var myman = new Man(lastId, $scope.nameInput,$scope.ageInput);
            $scope.people.push(myman);
            alert("Added!");
        }
    }
    $scope.people = [man1, man2, man3];
    $scope.selectedPeople = [man4, man5];
}

function Man(name, age, id) {
    this.id = id;
    this.name = name;
    this.age = age;
}

The fiddle: http://jsfiddle.net/yuqorcvL/5/

Any help would be appreciated.

Upvotes: 0

Views: 758

Answers (1)

Andrea
Andrea

Reputation: 175

Use kendo observable array and it will do the magic !!.

Instead of $scope.people = [man1, man2, man3];

Use this :

$scope.people = new kendo.data.ObservableArray([man1, man2, man3]);

updated ur fiddle : JSfiddle

Upvotes: 3

Related Questions