删除动态生成的数据?

时间:2015-03-01 10:50:33

标签: json angularjs sorting dynamic

我的删除功能有问题!只要我删除来自我的json文件的数据一切正常。但是如果你要添加更多的数据 - 它将删除错误的旅行或天数,这取决于它是旧的还是更新的(例如,如果你将输入较旧的日期,它会删除第二天或旅行)。 问题显示在删除功能(deleteTrip和delTripDay)中。索引必定存在错误。有什么想法吗?

此外,我想将数据导出到json - 到目前为止效果很好。如果添加一些数据,它会将新数据放到我的json对象的末尾。我想在我的保存功能中添加一个过滤器,以便像上面显示的那样对新保存的JSON数据进行排序!

- >降序日期!

有没有解决旅行和DAYS的解决方案?

plunkr

代码:

(function() {
  var app = angular.module('showTrips', []);


      app.controller('TripController', ['$scope', '$http',
        function($scope, $http) {
          $http.get('trips.json').success(function(data) {

            $scope.trips = data;

            $scope.addTrip = function() {
              $scope.trips.push({
                'Startdate': $scope.newtrip,
                DAYS: []
              })
              $scope.newtrip = ''
            }

            $scope.deleteTrip = function(index) {
              $scope.trips.splice(index, 1);
            }

            $scope.delTripDay = function(trip, index) {
              trip.DAYS.splice(index, 1);
            }


            $scope.savedJSON = '';

            $scope.save = function() {
              $scope.savedJSON = angular.toJson($scope.trips);
            };


          });


        }
      ]);


      app.controller("DayController", function() {

        this.day = {};
        this.addDay = function(trip) {
          trip.DAYS.push(this.day);
          this.day = {};
        }

      });

    })();

1 个答案:

答案 0 :(得分:1)

问题是您使用角度过滤器在UI上过滤数组,并且从非过滤数组中删除数组正在删除不同日期对象。

您只需更改deleteTripDay功能,如下所示。

<强> CODE

$scope.delTripDay = function(trip, index) {
    //creating array structure like UI
    var deleteDays = $filter('orderBy')($filter('filter')(trip.DAYS, $scope.query), 'DATE');
    deleteDays.splice(index, 1); // then delete by index
    trip.DAYS = deleteDays; //reassigning update array to trip days
}

Working Plunkr

希望这可以帮助你,谢谢。

相关问题