radio button groups nested inside checkbox list with angularjs ng-repeat - angularjs-ng-repeat

In this example I'm nesting radio buttons inside checkboxes:
html:
<div ng-app ng-controller="Controller">
<div name="myForm">
<ul>
<li ng-repeat="item in items">
<label>
<input type="checkbox" ng-model='item.selected' />{{item.name}}</label>
<ul>
<li ng-repeat="format in item.formats">
<label>
<input type="radio" ng-model='format.selected' name='what name ?'/>{{format.title}}</label>
</li>
</ul>
</li>
</ul>
</div>
<button ng-click='onClick()'>Go</button>
</div>
js:
function Controller($scope) {
$scope.items = [{
name: 'cb1',
selected:false,
formats: [{
title: 'rb1a',
selected: false
}, {
title: 'rb1b ',
selected: false
}]
}, {
name: 'cb2 ',
selected:false,
formats: [{
title: 'rb2a ',
selected: false
}, {
title: 'rb2b',
selected: false
}]
}];
$scope.onClick = function () {
console.log('clicked');
for (var i = 0; i < $scope.items.length; i++) {
if ($scope.items[i].selected) {
console.log('Controller item selected', $scope.items[i]);
}
for (var j = 0; j < $scope.items[i].formats.length; j++) {
if ($scope.items[i].formats[j].selected)
console.log('Controller format selected', $scope.items[i].formats[j]);
}
}
};
};
I have two questions:
when I select an rb in one group the selected rb in the other group gets unselected. So they are not groups yet. How can I make real groups? I thought by using the name directive, but how?
when I click the Go button I'd like to have set the selected properties in $scope.items correctly, both for checkboxes and radio buttons.
I tried a lot already, that's why I'm now just providing stripped down code.
http://jsfiddle.net/JeffW/crn1t7wy/

1) You need to set the name to be unique for each group of radio buttons. You can do it with the $index argument angular provides with ng-repeat.
2) You misplaced single quotes and this is why it doesn't work. HTML accepts only double quotes or single quotes as attribute values but not mixed.
<div ng-app ng-controller="Controller">
<div name="myForm">
<ul>
<li ng-repeat="item in items">
<label>
<input type="checkbox" ng-model='item.selected' />{{item.name}}</label>
<ul>
<li ng-repeat="format in item.formats">
<label>
<input type="radio" ng-value="format.title" ng-model="item.selectedRadio" name="myName{{$parent.$index}}"/>{{format.title}}</label>
</li>
</ul>
</li>
</ul>
</div>
<button ng-click='onClick()'>Go</button>
</div>
Fixed version is here:
http://jsfiddle.net/crn1t7wy/3/
EDIT: Fixed the answer based on the comments

Related

Vue.js 3 - Add list items from component dynamically

I am new to vue, I found some examples but I could not reproduce in my situation.
I have some inputs user will fill out and once it clicks the button, the information will populate a list of items. I am trying to crete < li > with a OrderListItem component. But I am getting this error:
runtime-core.esm-bundler.js?5c40:217 Uncaught ReferenceError: machines is not defined
at eval (OrderListItem.vue?fb66:14)
Here is the form
<template>
<div>
<div class="flex flex-col mb-4 md:w-full m-1">
<label for="planter_model_id">Planter</label>
<select v-model="registration.planter_model_id" name="planter_model_id">
<option disabled selected>Planter</option>
<option v-for="planter in planters" :key="planter" :value="planter.id">{{planter.brand.name}} {{planter.name }}</option>
</select>
</div>
<div class="flex flex-col mb-4 md:w-full m-1">
<label class=" for="lines">Lines</label>
<Input v-model="registration.lines" type="number" name="lines" />
</div>
<Button type="button" #click="addItemOrder">Add</Button>
</div>
<div>
<ul>
<OrderListItem :machines="machines" />
</ul>
<div>
</template>
Here is my code
export default {
components: {
OrderListItem,
Button,
},
props: {
planters:{
type:Array,
required: true
},
},
data() {
return {
machines: [], //this what I wish to be passed to OrderListItem component
registration:{
lines:null,
planter_model_id:null,
},
}
},
methods:{
addItemOrder(){
let item = {}
item.planter_model_id = this.registration.planter_model_id
item.lines = this.registration.lines
this.machines.push(item)
}
}
};
And here is my OrderListItem component that I created on another file:
<template>
<li v-for="machine in machines" :key="machine">
<div class="flex-initial w-3/5">
<p>Planter: {{machine.planter_model_id}}</p>
<p>Lines: {{machine.lines}}</p>
</div>
</li>
</template>
<script>
export default {
name: 'OrderListItem',
props:{
machines,
}
}
</script>
I don’t know if it is relevant but I am using vue3 and laravel.
I am very newbie here and from what I learned I think if machines array was a prop I would be able to access it on my component. But I will dynamically add a and remove itens from machines array and I think props receive data from server so It should not be declared there, that why I declared inside data().
Thanks =)
You should define the prop by adding its type and default value :
export default {
name: 'OrderListItem',
props:{
machines:{type:Array,default:()=>[]},
}
}

Populate formControlName checkbox from pre-defined data in Angular 2+

I have a dynamically created checkbox list and I'm having trouble to check the some trues according to a pre-defined list.
HTML:
<div class="row">
<div class="example-container col-md-6">
<div *ngFor="let atribuicao of atribuicoesOcorrencia" formArraylName="inputAtribuicaoOcorrencia">
<mat-checkbox [value]="atribuicao.id" (change)="onChange(atribuicao, $event)">
<div style="white-space: pre-wrap;">
{{ atribuicao.descricao }}
</div>
</mat-checkbox>
</div>
</div>
</div>
CLASS TS:
I try populate formControl name inputAtribuicaoOcorrencia in a list, in this case
the only one checekd was id 3, but nothing happens
this.atribuicoesOcorrencia.forEach(listAtibuicoes=> {
ocorrencia.atribuicoesDTO.forEach(x => {
if(listAtibuicoes.id == x.id){
this.formCadastro.get('inputAtribuicaoOcorrencia').setValue('checked');
}
});
});
CLASS TS2:
Or the code bellow for one ID checked only
this.formCadastro.patchValue({
inputAtribuicaoOcorrencia: 'checked',
});
You need to use the [checked] attribute for the mat-checkbox
// example
<mat-checkbox
[value]="atribuicao.id"
[checked]="atribuicao.id" // This is what you need to add. If id is there, it will get checked
(change)="onChange(atribuicao, $event)"
>
<div style="white-space: pre-wrap;">
{{ atribuicao.descricao }}
</div>
</mat-checkbox>

How to submit checked input?

I am using vuejs v.2, In my data structure I have products and keywords
they have many-to-many relationship. To attach keywords to product I have list of keyword's checkbox and when user submit only checked keyword should be attach to product
<div class="col-md-6" v-for="keyword in keywords">
<div class="checkbox">
<label>
<input type="checkbox" />
{{ keyword.name }}
</label>
</div>
</div>
Here I can not bind keyword.id as value (v-bind:value).
I just want to submit checked keyword ids to server
Please show me the correct way
I think the mistake you might be doing is not using v-model with an array data variable, following is working code:
vue component:
var demo = new Vue({
el: '#demo',
data: function(){
return {
keywords: [
{name: 'key1', id: 1 },
{name: 'key2', id: 2 },
{name: 'key3', id: 3 }
],
checked: []
};
}
})
and in HTML:
<div id="demo">
<div class="checkbox">
<label v-for="keyword in keywords">
<input type="checkbox" :id="keyword.name" v-bind:value="keyword.id" v-model="checked"/>
{{ keyword.name }}
<br>
</label>
<br>
checked value: {{checked}}
</div>
</div>
Working fiddle here

Django ajax-generated hidden field not in request.POST

I'm using https://drew.tenderapp.com/faqs/autosuggest-jquery-plugin/options-api to render an autocomplete field:
<h1>Quick Tags</h1>
<div class="fieldset">
{{ vocabularies.vocabulary_1.errors }}
<p>{{ vocabularies.vocabulary_1 }}</p>
<script type="text/javascript">
////http://code.drewwilson.com/entry/autosuggest-jquery-plugin
$("input#id_vocabulary_1").autoSuggest("/staff/taxonomy/ajax", {selectedItemProp: "name", selectedValuesProp: "name", searchObjProps: "name", startText: "Enter terms.", keyDelay: 50, minChars: 1, queryParam: "term", asHtmlID: "vocabulary_1", extraParams: "&app=taxonomy&model=TaxonomyData&vocabulary=1"});
</script>
</div>
Which renders a hidden field:
<li class="as-original" id="as-original-vocabulary_1">
<input id="vocabulary_1" type="text" name="vocabulary_1" maxlength="200" autocomplete="off" class="as-input">
<input type="hidden" class="as-values" name="as_values_vocabulary_1" id="as-values-vocabulary_1" value="test,new term,">
</li>
However, the values from this field are not present in the POST dictionary. What could be causing this problem?
I found out the problem; if the div that contains the form opening tag is closed, all fields after that are not contained in the POST dictionary.

Prototype function to set onclick event observer in checkboxes

I want to set an observer for each of the following checkboxes:
<div id="listr">
<ul id="lr" style="list-style-type: none">
<p class="tir">Text 1</p>
<li class="ro"><input type="checkbox" name="r1" id="r1"/>A1</li>
<li class="ro"><input type="checkbox" name="r2" />A2</li>
<p class="tir">Text 2</p>
<li class="rubro"><input type="checkbox" name="r3" />B1</li>
<p class="tir">Text 3</p>
<li class="ro"><input type="checkbox" name="r4" />B2</li>
</ul>
</div>
It works if I write one observer per checkbox, but I want to do it in a short fashion, so I need something like
$$('listr.lr.li.input').invoke('observe',click,function(field) {
alert(this.name + ' clicked ' + this.checked);
// other stuff ...
});
Which is not working
Thanks in advance
Try using a valid CSS3 selector:
$$('#listr input[type="checkbox"]').invoke('observe','click',function(field) {
alert(this.name + ' clicked ' + this.checked);
// other stuff ...
});
http://prototypejs.org/doc/latest/dom/dollar-dollar/

Resources