Murali Murali - 1 year ago 72
AngularJS Question

New row is not added in component

Here I am using angular Tree for component. When the data is entering first time it is working fine.

But the data is coming from backend, then the adding row button is not adding.

Plunker
Here is my code.

$scope.newSubItem = function(scope) {

var nodeData = scope.$modelValue;
nodeData.items.push({
id: nodeData.items.length?(nodeData.items[nodeData.items.length-1].id)+1:nodeData.id * 10,
rowId: nodeData.rowId + '.' + ((nodeData.items.length?(parseInt(nodeData.items[nodeData.items.length-1].rowId.split('.').pop()))+1:1)),
items: []
});
};


The error is

TypeError: Cannot read property 'push' of null
at Scope.$scope.newSubItem (app.js:19)
at $parseFunctionCall (angular.js:12332)
at callback (angular.js:22949)
at Scope.$eval (angular.js:14383)
at Scope.$apply (angular.js:14482)
at HTMLAnchorElement.<anonymous> (angular.js:22954)
at HTMLAnchorElement.eventHandler (angular.js:3011)

Answer Source

This is happening because when a node has no child elements you are not returning empty array in items from the server

enter image description here

Either add code on server side which adds an empty array in items

or

@Sajeetharan is right you need to add this code in your newSubItem function

$scope.newSubItem = function(scope) {

    var nodeData = scope.$modelValue;
    if(!nodeData.items)
        nodeData.items =[];
    nodeData.items.push({
        id: nodeData.items.length?(nodeData.items[nodeData.items.length-1].id)+1:nodeData.id * 10,
                rowId: nodeData.rowId + '.' + ((nodeData.items.length?(parseInt(nodeData.items[nodeData.items.length-1].rowId.split('.').pop()))+1:1)),
                items: []
    });
};
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download