Vue.js multiple check box - arrays

I have 3 check boxes . Want to get result like {ceheckboxName1:value,ceheckboxName2:value,ceheckboxName3:value}.You can see my code for better understand.
<li>
<input name="" type="checkbox" value="100" v-model="checkedNames" id="incall">
<label for="incall">Incall</label>
<div class="pull-right price-now-right"><p>100</p></div>
</li>
<li>
<input name="" type="checkbox" value="200" v-model="checkedNames" id="">
<label for="lbl">view</label>
<div class="pull-right price-now-right"><p>100</p></div>
</li>
<li>
<input name="" type="checkbox" value="On Request" v-model="checkedNames" id="overnight">
<label for="ovr">lmm</label>
<div class="pull-right price-now-right"><span>On request</span></div>
</li>

Give each checkbox a :value="true" (yes, the : is important) and v-model="checkboxName1" (2, 3, etc.). That will automatically set this.checkboxName1 to true if checked.
Then, in your data function:
data() {
return {
checkboxName1: false,
checkboxName2: false,
checkboxName3: false,
};
}
this.$data will contain your desired format.

Related

Want to add comment at respective places

Below is the structure of threaded comments in angular.js form.
<form>
<ul class="unstyled">
<!-- ng-class="{ '{{childcond}}': $index == selectedIndex }" -->
<li ng-repeat="comment in comments" class="{{childcond}}" ng-init=" childcond=checkChild(comment.slug); reply.parentSlug=comment.slug; idstring=slashRemove(comment.slug);" >
<strong>{{comment.author}}</strong><br>
<p>
{{comment.body}}
</p>
<a href="javascript:void(0)" class="replycomment" >reply</a>
<div id="{{idstring}}" style="display:none;" >
<input type="text" placeholder="Your Name" ng-model="reply.replyAuthor"><br>
<span style="display:none;">
<input type="text" class="hidevalue" ng-model="reply.parentSlug" ng-value="{{comment.slug}}" >
</span>
<textarea name="" id="" cols="30" rows="10" placeholder="Comment" ng-model="reply.replyBody"></textarea>
<input class="btn-primary" type="submit" value="reply" ng-click="replyComment(reply,$index)">
</div>
</li>
</ul>
When i replying to the comment i am getting my comment in last place.
This is my reply code in angular.js file.
$scope.replyComment = function(reply,$index) {
socket.emit('comment', { author: reply.replyAuthor,
body: reply.replyBody,
parent_slug:reply.parentSlug });
clearForm();
};
socket.on('comment', function(comment) {
$scope.$apply(function() {
console.log(comment);
$scope.comments.push(comment);
});
});
console.log(comment) gives following structure.
Any help would be much appreciated!!!

How to create dynamic radio buttons with multiple options

I am trying to create a dynamic form with multiple radio button options and get selected values..
<form name="myForm" ng-submit="sendAnswers(question)">
<ul>
<li ng-repeat="question in questionForm.questions">
<p>{{question.description}}</p>
<ul>
<li ng-repeat="option in question.options">
<input type="radio" ng-model="question.answer" value="option.value">
{{option.value}}
</li>
</ul>
</li>
</ul>
<p><input type="submit" value="sendAnswers"></p>
</form>
Problem is as I click on button, both options get selected..what am I doing wrong here?
Live example: http://plnkr.co/edit/aStk4SCHzAW0AKQuH7JE?p=preview
Thanks in advance!
Add a "name" parameter:
<input name="{{question.description}}" type="radio" ng-model="question.answer" value="option.value">
Also, you need to change "value" to {{option.value}}
<input name="{{question.description}}" type="radio" ng-model="question.answer" value="{{option.value}}">
Working Example

Angular hide/show based on radio button

Plunker
How can i make yes radio button to be checked on page load and show/hide the field-set accordingly. Currently on load radio button is checked but its not showing the fieldset.
Html
<form name="myform" class="form " novalidate >
<fieldset>
<span>Would you like a receipt sent to you?</span>
<ul class="vList">
<li>
<input type="radio" name="receiptConfirmation" id="receiptAffirm" ng-model="reciept" value="yes" ng-checked="true">
<label class="form__radioLabel" for="receiptAffirm:" >Yes</label>
</li>
<li>
<input type="radio" name="receiptConfirmation" id="receiptDeny" ng-model="reciept" value="no">
<label class="form__radioLabel" for="receiptDeny">No </label>
</li>
</ul>
</fieldset>
<fieldset class="fieldset" ng-show="reciept=='yes'">
<legend class="isVisuallyHidden">Email Receipt</legend>
<ul class="vList">
<li>
<label for="firstName" class="form__label">First Name</label>
<input id="Text4" class="form__input form__input--textfield" type="text" >
</li>
<li>
<label for="emailAddress" class="form__label">Email Address</label>
<input id="email1" class="form__input form__input--textfield" type="email">
</li>
</ul>
</fieldset>
</form>
Do not mix ng-checked with ng-model. Setting ng-checked will not update the model, it will just update the element's checked property. Instead set the ng-model reciept to the desired value.
Remove ng-checked from input:-
<input type="radio" name="receiptConfirmation" id="receiptAffirm"
ng-model="reciept" value="yes">
In your controller set the model:-
app.controller('MainCtrl', function($scope) {
$scope.reciept = "yes";
});
Demo
Just set $scope.reciept = 'yes'; in your controller?
You have spelt receipt wrong also.

One checkbox should be selected from multiple checkboxs with different ng-model

How to validate checkbox with different ng-model??
For e.g:
My HTML looks like:
<form name="myFrm">
<ul>
<li><input type="checkbox" ng-model="sunday" />Sunday</li>
<li><input type="checkbox" ng-model="monday" />Monday</li>
<li><input type="checkbox" ng-model="tuesday" />Tuesday</li>
<li><input type="checkbox" ng-model="wednesday" />Wednesday</li>
<li><input type="checkbox" ng-model="thursday" />Thursday</li>
<li><input type="checkbox" ng-model="friday" />Friday</li>
<li><input type="checkbox" ng-model="saturday" />Saturday</li>
</ul>
<p ng-if="myFrm.$invalid">Atleast one day should be selected</p>
</form>
from the above code, i want to check atleast 1 checkbox should be selected OR give error message.
I've tried searching, but i found the solution in which has a same ng-model, but i have different ng-model for each checkbox.
How to do that??
DEMO JS FIDDLE
Try creating custom directives to do custom validation:
//This directive is to update Form validity when any of the elements decorated
// with customRequired is not empty.
app.directive("customRequiredContainer",function(){
return {
restrict:"A",
require:"form",
controller:function($element,$scope){
var properties = []; //store the list of properties to check.
//customRequired will register the property to be checked.
this.registerProperty = function(property){
if (properties.indexOf(property) === -1){
properties.push(property);
$scope.$watch(property,function(value){
if ($element.form){
//If any of the elements is checked, Form is valid otherwise not valid.
for (var i=0;i<properties.length;i++){
if ($scope[properties[i]]){
//we should use $setValidity(),
//I don't know why it does not work, check that later.
$element.form.$invalid = false;
$element.form.$valid = true;
return;
}
}
$element.form.$invalid = true;
$element.form.$valid = false;
}
});
}
};
},
link:function(scope,element,attrs,formController){
element.form = formController;
}
}
});
//This directive is to decorate which element should be checked for validity
app.directive("customRequired",function(){
return {
restrict:"A",
require:"^customRequiredContainer",
link:function(scope,element,attrs,containerController){
containerController.registerProperty(attrs.ngModel);
}
}
});
HTML:
<form name="myFrm" custom-required-container>
<ul>
<li>
<input type="checkbox" ng-model="sunday" custom-required/>Sunday</li>
<li>
<input type="checkbox" ng-model="monday" custom-required/>Monday</li>
<li>
<input type="checkbox" ng-model="tuesday" custom-required/>Tuesday</li>
<li>
<input type="checkbox" ng-model="wednesday" custom-required/>Wednesday</li>
<li>
<input type="checkbox" ng-model="thursday" custom-required/>Thursday</li>
<li>
<input type="checkbox" ng-model="friday" custom-required />Friday</li>
<li>
<input type="checkbox" ng-model="saturday" custom-required />Saturday</li>
</ul>
<p ng-if="myFrm.$invalid">Atleast one day should be selected</p>
</form>
DEMO
Or this:
<form name="myFrm">
<ul>
<li>
<input type="checkbox" name="sunday" ng-model="sunday" required/>Sunday</li>
<li>
<input type="checkbox" name="monday" ng-model="monday" required />Monday</li>
<li>
<input type="checkbox" name="tuesday" ng-model="tuesday" required />Tuesday</li>
<li>
<input type="checkbox" name="wednesday" ng-model="wednesday" required />Wednesday</li>
<li>
<input type="checkbox" name="thursday" ng-model="thursday" required />Thursday</li>
<li>
<input type="checkbox" name="friday" ng-model="friday" required />Friday</li>
<li>
<input type="checkbox" name="saturday" ng-model="saturday" required />Saturday</li>
</ul>
<p ng-if="myFrm.sunday.$error.required&&myFrm.monday.$error.required
&&myFrm.tuesday.$error.required&&myFrm.wednesday.$error.required
&&myFrm.thursday.$error.required&&myFrm.friday.$error.required
&&myFrm.saturday.$error.required">Atleast one day should be selected</p>
</form>
DEMO
Explanation:
By giving the inputs names: name="sunday", angular will add the input name as a property of the form: myFrm.sunday. From then, we can check whether the input is selected using its $error.required property.
Or this:
<form name="myFrm">
<ul>
<li>
<input type="checkbox" ng-model="sunday" />Sunday</li>
<li>
<input type="checkbox" ng-model="monday" />Monday</li>
<li>
<input type="checkbox" ng-model="tuesday" />Tuesday</li>
<li>
<input type="checkbox" ng-model="wednesday" />Wednesday</li>
<li>
<input type="checkbox" ng-model="thursday" />Thursday</li>
<li>
<input type="checkbox" ng-model="friday" />Friday</li>
<li>
<input type="checkbox" ng-model="saturday" />Saturday</li>
</ul>
<p ng-if="!sunday&&!monday&&!tuesday&&!wednesday&&
!thursday&&!friday&&!saturday">Atleast one day should be selected</p>
</form>
DEMO

Binding one value to checkboxes

i am getting swallowed by an issue.
I have this code currently
<div ng-repeat="list in ConnectivityDetails">
<div class="connectivity">
<div class="title">
Disable Connectivity??</div>
<div class="decision">
<p style="float: left;">
<input type="checkbox" ng-checked="list.Connectivity"/>
<label for="Yes">
</label>
<span class="spans">Yes</span>
</p>
<p>
<input type="checkbox" ng-model="list.Connectivity"/>
<label for="No">
</label>
<span class="spans">No</span>
</p>
</div>
</div>
</div>
Problem:
I am getting both the text boxes checked though the returning object is having only one value, how to bind true (Yes) for the checkbox and false (NO) for the other?
My Object Contains this:
var data = [{PracticeID: "1",Connectivity: "true", LabKey: "2154879",PracticeKey: "ABNCJFUE", CloudServiceURL: "ABC215947"}]";
I want to bind Connectivity value to one of the checkboxes
Help!
Thank you
Fiddle: http://jsfiddle.net/ymZdx/3/
So it should be:
<p style="float: left;">
<input type="checkbox" ng-model="list.Connectivity" />
<label for="Yes">
</label>
<span class="spans">Yes</span>
</p>
<p>
<input type="checkbox" ng-model="list.Connectivity" ng-true-value="false" ng-false-value="true" />
<label for="No">
</label>
<span class="spans">No</span>
</p>

Resources