Radio Buttons in Dynamicaly Generated Angular Form - angularjs

https://gist.github.com/djdaniels90/948704c58242c4bb08b5
I am having trouble with a dynamically generated quiz structure. I am aware of $parent. scope. I have attempted to implement it but the form is still not working as desired. I have linked the gist site; any help on this would be greatly appreciated.
Each question is a form in itself and the entire quiz is a form of question forms. Each question form contains x number of possible answers. Furthermore, a quiz can have y number of questions. Therefore we have a loop within a loop; ie: we are creating some nested child scopes.
Ultimately, each question can only have one answer, ie: only one checkbox can be clicked for each subforms. I can rather easily hack together a method using pure JS or jquery to grab the form elements but I know there is a way to do this in Angular, I am pretty close I believe but can't get the last little bit. Ideally, I would like either the questions to update corresponding models, which could be created in a array within the controller. Or the form submit action to upload the entire quiz data.
Any help on where I am going wrong would be great.

You are missing one attribute and need a variable for assigning answers to.
The NAME attribute takes care of the grouping and the model cannot be the question object itself, but rather an attribute of the question itself.
<input type="radio" name='{{question.question}}' ng-value="{{answer}}" ng-model="question.answer">
Here's a working version:
http://plnkr.co/edit/fACdq8DGvJel9An87GyA?p=preview

Related

how to validate answers for quiz app, values which is fetched from json in reactjs?

I am developing a ReactJs quiz app in which I am having problem with validating the answers that is in the json.
I did this quiz app using react version 16.8 using state components and fetched the json data and stored in state using map function I have the completed the view part, now I started to validate the quiz and I am struggling in that part.
here is the full code:https://codesandbox.io/s/mystifying-firefly-2d2x5
and also ill add my json link: http://myjson.com/kpop9
I want the answer should be validated, and if user clicks the submit button before attempting all the questions it should show that you have unanswered questions and if user clicks submit after attempting all the quiz it should display the total marks that user got.
Replying to your comment from above, here is your sandbox code slightly edited. You can submit an answer at any time and these are the alerts you should see:
When you don't select an answer: Alert with message No nulls
When answer is wrong: Alert with message wrong
When answer is correct: Alert with message correct
Here is your edited sandbox
The solution provided is only example, and should not be treated as a perfect solution, it's only to give you an idea of how this may work (you may still want to implement the bit, when after submitting the last answer, scores are calculated - for that you may want to store scores)
Explanation:
In this example I decided to add selected_answer variable in the index.js that stores currently selected answer on the form.
Next, I created setAnswer function in index.js which accepts a selected answer as parameter and sets the selected_answer in state to whatever is passed in. You are welcome to implement as many checks for the value that is passed in as you want
setAnswer function is then passed to your Answer component, so when the value is changed, it can be saved inside index.js state
Result component receives the index.js state as a prop. This allows it to have access to current question, the array of all questions and currently selected value
Inside Result component there is a validateAnswer function that is triggered when submit button is clicked. Inside that function I use the props.current_question (which is the index of a question) to extract the whole question object from your JSON file. Next I filter over the array of answers from previously created question object, and I extract the one that has is_right set to 1. Finally, I check if the props.selected_answer is empty, and display a message if so. If it isn't, I check if it equals to the value of previously extracted correct answer object. And voila!
As mentioned before, this is not the best solution, but one that works on top of your code without changing much. Please let me know if you have any further questions, but hope that helps a bit.

How to set id dynamically in ng-repeat in angular js

I have to load multiple answers when user click a question.There are multiple questions and each has multiple answers.but when i click second question,the answers of first question are also changing.
I have assigned div id dynamically from my controller in ng-repeat,but when i load data into the div,it always loading data into the first question answers div.div ids are unique in ng-repeat but answers of every question are displaying in all divs.
Sounds to me that you are using the same ng-model for both questions. If you link some of your code I could better examine it.
Use
$('#QuestionID').find('#answerId');
select answer id inside particular question instead of directly taking
$('#answerId')

Backbone CollectionBinder giving last model only

I am creating a survey app. In this a question can have multiple answers. So I am storing answers in a collection. I am using Backbone.CollectionBinder to render views by passing view class. Each view have "Remove Answer" link.
Lets say there are 3 answers added to collection. Now if I remove 1st answer it always gives me last model in that view class. So the problem is it always gives me last model inside answer view.
We need some more information. The docs (http://backbonejs.org/#Collection-remove) state that the removed model is returned from the collection. I've used it heavily without issue. It sounds like your reusing the same view or iterating incorrectly. Please post some code.
I got the answer of my question from following post
http://lostechies.com/derickbailey/2011/10/11/backbone-js-getting-the-model-for-a-clicked-element/

How to pass var from controller into a different view with Cakephp

I am new to Cakephp and indeed OOP, so forgive me if i haven't fully grasped the MVC concept yet. I have search a lot but cannot find an answer - perhaps my way of working below is not correct. I hope you can help.
I am building a site which will have many elements relating to their tables and data. I intend to use a view to pick and choose the relevant elements and any parameters needed.
For example, the homepage of my site will have two elements - a latestusers element and a latestscores element. I am trying to use a view not related to either the users or scores models/controllers, stored in 'other/index.ctp'.
I have tried using set() to pass a variable from the users controller (latestusers action) into the other/index.ctp view, but the viewVars remain empty. Could this be due to scope of the variable (i think it is fine for a view in the users folder, i.e. a view specific to the users controller).
I could achieve what i want to do by using global variables, but i think this is missing the point of MVC/OOP. Would be grateful for any suggestions.
I can include code if need be - it is fairly basic at this stage - but i feel my problem lies with how i am going about things, not the code itself.
Cheers,
James
Yes, the issue is with the scope. If you're going to use variables in the element you'll need to pass them in from your view. So the flow would look something like this
Controller $this->set()s the variable into your current view/layout
Your view/layout calls $this->element with the current element path.
Your element uses those variables.
In number 2 you need to pass your variables as an array of data. This section on the cookbook gives more information : http://book.cakephp.org/view/1081/Elements
<?php echo$this->element('helpbox',
array("helptext" => "Oh, this text is very helpful."));?>
Note - I didn't understand part of the question. Just want to make sure you are passing data to the correct view. You should not be calling the view of another controller in your active controller.
Your other/index.ctp should be an element and that element should be called from your layout.

Salesforce.com visualforce between 2 objects

In salesforce I need to create a visualforce page that includes the fields of 2 objects. The first object is the QUOTE object. The second objects is a custom object with several fields.
I want to create a visualforce page that shows the records of both QUOTE and the new object. Can I do this without creating a custom controller? If no any hints on the code for this new controller?
Can I do calculations between fields in a visualforce page?
Ideally I want this page to appear as soon as the QUOTE is set to ACCEPTED
1: You can't do this without a custom controller unfortunately, unless one object is related to the other and you're just happy displaying it as a related list on the parent object's page. For calculations you could use rollup summaries for some basic sums etc..
As for a custom controller, have a look at field sets for a super easy way to get fields into a VF page, you essentially configure groups of fields on your objects and then you can stick those groups onto a page with minimal markup.
2: For fields with complex calculations you'll want to do the sums in the controller and then expose the results through variables onto the page in the usual manner.
3: Not really possible without creating a custom edit page in the first place — you'd be better off having a button on the quote page to open up the Visualforce page, that page can simply display an error if the quote is not yet accepted. There are some other alternatives that might work though, like using a forumla field to generate a link to the page when the status is as you desire.
I'm happy to elaborate on any of this, but the fact that you're asking about number 2 would suggest to me that you don't have much experience developing on the platform (not a dig, just an observation), so unless you're comfortable coding in other environments you could find this quite tricky. That said, you're on stackoverflow so I'm thinking you probably know a little about coding at least!

Resources