删除动态生成的数据?

Delete dynamic generated data?

我的删除功能有问题!只要我删除来自 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 = {};
        }

      });

    })();

问题是您在 UI 上使用 angular 过滤器过滤数组,从未过滤的数组中删除数组是在删除不同的日期对象。

您只需要像下面这样更改 deleteTripDay 函数。

代码

$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

希望对您有所帮助,谢谢。