2014-01-10 86 views
0

我想創建代表程序的多層對象,你可以看到我在做什麼這裏http://codepen.io/Irish1/pen/lbjdw以前刪除具有較小索引的對象時,刪除對象失敗。

說我加3周,每3天每3屆。我嘗試之前,我可以,只要我不刪除這些對象移除說,第二天和第三天

去除這裏的會話是代碼

HTML

<div ng-app="trainercompare"> 

<div ng-controller="programsController"> 
    <form ng-submit="addProgram()" name='form' novalidate> 

     <p> 
a program 
     </p> 

     <button type ="button" ng-click="addWeek()"> add week</button> 

     <!-- create weeks --> 
     <div ng-repeat="week in program.weeks" ng-init="weekindex = $index"> 

      <p> 
      a week 
      </p> 

      <!-- create days --> 
      <div ng-repeat="day in week.days" ng-init="dayindex = $index"> 

       <p> 
       a day 
       </p> 
       <!-- create sessions --> 

       <div ng-repeat="session in day.sessions" ng-init="sessionindex = $index"> 
        <h2>a Session</h2> 
        <button type ="button" ng-click="removeSession(weekindex, dayindex, sessionindex)"> Remove Session</button> 
       </div> 
       <button type ="button" ng-click="addSession(weekindex, dayindex)"> Add Session</button> 
       <button type ="button" ng-click="removeDay(weekindex, dayindex)"> Remove day</button> 
      </div> 
      <button type ="button" ng-click="addDay($index)"> Add Day</button> 
      <button type ="button" ng-click="remove($index)"> Remove week</button> 
     </div> 

     <button type="submit"> add program</button> 
    </form> 
</div> 

</div> 

app.js

var myModule = angular.module("trainercompare", ['ui.bootstrap']); 

function programsController($scope, $http) { 

    var numweeks = 1; 
    $scope.program = { 

    }; 

    $scope.addWeek = function() { 

     if (isDefined($scope.program.weeks)) { 
      $scope.program.weeks.push(
       { 

       } 
      ); 

     } else { 
      $scope.program = { 
       weeks: [ 
        { 

        } 
       ] 
      }; 
     } 
    }; 

    $scope.addDay = function(index) { 

     if (isDefined($scope.program.weeks[index].days)) { 
      $scope.program.weeks[index].days.push(
       { 

       } 
      ); 

     } else { 
      $scope.program.weeks[index] = { 
       days: [ 
        { 

        } 
       ] 
      }; 
     } 
    }; 

    $scope.addSession = function(weekindex, dayindex) { 

     if (isDefined($scope.program.weeks[weekindex].days[dayindex].sessions)) { 
      $scope.program.weeks[weekindex].days[dayindex].sessions.push(
       { 

       } 
      ); 

     } else { 
      $scope.program.weeks[weekindex].days[dayindex] = { 
       sessions: [ 
        { 

        } 
       ] 
      }; 
     } 
    }; 

    $scope.remove = function(index) { 
     $scope.program.weeks.splice(index, 1); 

     console.log(index); 

    }; 

    $scope.removeDay = function(weekindex, dayindex) { 
     $scope.program.weeks[weekindex].days.splice(dayindex, 1); 

     console.log(dayindex); 

    }; 

    $scope.removeSession = function(weekindex, dayindex, sessionindex) { 
     $scope.program.weeks[weekindex].days[dayindex].sessions.splice(dayindex, 1); 

     console.log(sessionindex); 

    }; 

    function isDefined(x) { 

    return x !== undefined; 
    } 

    $scope.addProgram = function() { 

     console.log($scope.program); 

     $http.post('/programs', $scope.program).success(function(data, status) { 
      if(isDefined(data.errors)) { 
       console.log(data.errors); 
       $scope.errors = data.errors; 
       } 
      if(isDefined(data.success)) { 
       console.log(data.success); 
       $scope.errors = []; 
      } 
     }); 

    }; 


} 
+0

你說,你是不是能夠只要它的前一個兄弟犯規存在刪除某些對象之前? – ProllyGeek

+0

如果我刪除一個對象,我不能刪除後續兄弟姐妹的子女。我相信可以刪除兄弟姐妹。 – Ir1sh

回答

1

好日子 檢查outthis搗鼓你想實現什麼 您刪除以前的問題,我已經完成了小提琴

http://jsfiddle.net/U3pVM/2593/

var imust accompany with code 
+0

謝謝你的回答,我現在正在翻閱它,雖然它適用於添加和刪除幾周,但我很難說它是否會幫助我目前遇到的問題,因爲它與我之前的情況截然不同接近它。我會試着弄清楚,非常感謝你 – Ir1sh

+0

這是一個很大的幫助,並沒有像我期待的那樣難以理解,它打破了我的添加方法,除非我刪除所有內容並重新開始。他們在我的問題中也很詳細,如果您有任何建議,那也會很好。 – Ir1sh