0

I'm going to create a table with dynamic rows comes from APIs that look liks:

var app = angular.module("myapp", []);
app.controller("ListController", ['$scope', function($scope) {
    $scope.personalDetails = [    {
      "1": {
        "name":               "EZ-Catering",
        "name_label":         "EZ-Catering",
        "address":            "234 W. 11th St.\r\nBloomington IN 23484",
        "address_label":      "234 W. 11th St.\r\nBloomington IN 23484",
        "contractDate_label": "2016-05-31",
        "contractDate":       "2016-05-31 00:00:00",
        "currentClient":      "1",
        "currentClient_label":"true"
      },
      "2": {
        "name":               "Smithee Lawn Service",
        "name_label":         "Smithee Lawn Service",
        "address":            "555 Main St.\r\nEvansville IN 46234",
        "address_label":      "555 Main St.\r\nEvansville IN 46234",
        "contractDate_label": "2016-05-08",
        "contractDate":       "2016-05-08 00:00:00",
        "currentClient":      "0",
        "currentClient_label":"false"
      }
    }];

        $scope.addNew = function(personalDetail){

        };

        $scope.remove = function() {
            var newDataList=[];
            $scope.selectedAll = false;
            angular.forEach($scope.personalDetails, function(selected){
                if(!selected.selected){
                    newDataList.push(selected);
                }
            }); 
            $scope.personalDetails = newDataList;
        };

    $scope.checkAll = function () {
        if (!$scope.selectedAll) {
            $scope.selectedAll = true;
        } else {
            $scope.selectedAll = false;
        }
        angular.forEach($scope.personalDetails, function(personalDetail) {
            personalDetail.selected = $scope.selectedAll;
        });
    };    


}]);

The table should have a button to add more rows with empty fields.

Please notice that JSON data comes with numbering

"1": { ...

"2": { ...

I tried to use array.push but it's not working!

user1994739
  • 366
  • 4
  • 7
  • Please show what you have tried. What you are showing us isn't even an array so it is no surprise `push()` doesn't work. Also note any errors thrown when you update the question. Take a few minutes to read through [ask] and [mcve] – charlietfl May 27 '18 at 22:14
  • Thank you! I just want to create a function to add data to the list of objects that shown above. – user1994739 May 27 '18 at 22:37
  • So are you currently able to render that object? – charlietfl May 27 '18 at 22:52
  • Is there some reason that you need to preserve that awkward structure? Personally, I would remap that object into an array that I could `push` and `slice`. – georgeawg May 28 '18 at 02:22
  • Yes I can render that object but I can't add more rows, that structure comes from a framework and I can't change it. I just want to add more dynamically from angular – user1994739 May 28 '18 at 06:58
  • See my answer in [Angularjs create table with add/delete row, from ng-repeat in object of objects](https://stackoverflow.com/a/50573766/5535245). – georgeawg May 28 '18 at 21:55

0 Answers0