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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook 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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
JavaScript

From novice to tech pro — start learning today.