angularjs retrieve existing values

http://plnkr.co/edit/EEdmDmcWQDgu9URRyIbz?p=preview


Ok I have Json data that matches the form by QuestionID. if the modelList QuestionID matches ExistingList QuestionID and has a value coming from the object Response in the ExistingList. I want to insert the Response Data in the textbox.
LVL 9
Seven priceFull StackAsked:
Who is Participating?
 
Ioannis ParaskevopoulosCommented:
Yes, sure:

The plunker.

Change from :

    $scope.myNum = function (num) {

        $scope.mypage = num;
         
        //console.log($scope.ExistingList);
        var questions = $filter('filter')($scope.myData,{GroupID:num});
        angular.forEach(questions,function(item){
          //console.log(item);
          var question = $filter('filter')($scope.existingList,{GroupID:num,QuestionID:item.QuestionID});
          console.log($scope.existingList,question,num,item.QuestionID);
          if(!item.answer && question.length>0){
            console.log(question);
            item.answer = question[0].Response;
          }
        })
         
         
        // alert($scope.mypage);
         //$scope.todo.items.push({ GroupID: num, done: false });

     }
                  

Open in new window


To:

    //var questions = $filter('filter')($scope.myData,{GroupID:num});
    angular.forEach($scope.myData,function(item){
      //console.log(item);
      var question = $filter('filter')($scope.existingList,{QuestionID:item.QuestionID});
      //console.log($scope.existingList,question,num,item.QuestionID);
      if(!item.answer && question.length>0){
        console.log(question);
        item.answer = question[0].Response;
      }
    })

                         
    $scope.myNum = function (num) {

        $scope.mypage = num;
         
        //console.log($scope.ExistingList);
         
         
        // alert($scope.mypage);
         //$scope.todo.items.push({ GroupID: num, done: false });

     }

Open in new window


So, actually, instead of having it in the click, add it outside of it. And then of course you are checking all the questions once.

Giannis
0
 
Ioannis ParaskevopoulosCommented:
Hi again...

I made several changes on your code. You may see the full plunker here.

The changes i did where:

1. Added the existing array in the $scope

$scope.existingList = ExistingList;

Open in new window


3. Changed the Groups in the Existing List

It seems that the existing list had wrong group values. If you think i am wrong and the group values should be as they were, then you should probably change the way i am linking the questions with the existing answers in Step 3.

2. Added code to handle the values

        //This will get add in the questions var the questions of the selected group.
        var questions = $filter('filter')($scope.myData,{GroupID:num});

        //angular.forEach will help us iterate through the group's questions 
        angular.forEach($scope.questions,function(item){

          //So for each question we try to find out if there is already an answer, so we filter the existing list.
          // If i shouldn't have altered the groups in the existing list then you should probably remove "GroupID:num,"
          //from the line below.
          var question = $filter('filter')($scope.existingList,{GroupID:num,QuestionID:item.QuestionID});

          //if the question we are reviewing doesn't have an answer already (this effectively mean that the user hasn't 
          //altered the answer) and there is an answer in the existing list, then we update the question accordingly.
          if(!item.answer && question.length>0){
            item.answer = question[0].Response;
          }
        })

Open in new window


Keep in mind that if the user alters the answer, then this is what will be posted.

Good luck,
Giannis
0
 
Seven priceFull StackAuthor Commented:
I see you put it on the onclick. Can this happen onload, is it possible.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Seven priceFull StackAuthor Commented:
Ok i put everything together. the problem is I am using a dropdown. So on the onload sometime I retrieve the values and sometime it does not load unless I click a button to load it. would this because the dom does not allow it to populate the textboxes.
0
 
Ioannis ParaskevopoulosCommented:
To be honest, i wouldn't know without seeing the actual code. I think it should work though.
0
 
Seven priceFull StackAuthor Commented:
yes it works. maybe because I have 3 post loading data. but thanks again you been great as a teacher.
0
 
Seven priceFull StackAuthor Commented:
have a great restful weekend.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.