How to toggle nested checkboxes (options) on check parent checkbox in Vue Js - vue.js

i have simple questions list. It has some options and those options has some nested suboptions.
So need to keep hidden all nested checkboxes until checks parent checkbox.
i tried to do it but could not get it work.
i have tried this so far:
For better understanding please have a look at this fiddle
<div id="app">
<h2>Questions:</h2>
<ul>
<li v-for="question in questions">
<div class="question">{{ question.question }}</div>
<div class="label__wrap" v-for="option in question.options">
<label>
<input type="checkbox" v-model="question.answer" :value="option.option">
<span>
{{ option.option }}
</span>
</label>
<div class="sub__label__wrap" v-for="opt in option.subOptions">
<label>
<input type="checkbox" v-model="option.subOptsAnswers" :value=" opt.option">
<span>
{{ opt.option }}
</span>
</label>
</div>
</div>
<br><br>
</li>
</ul>
</div>
Thanks.

Updated Fiddle : https://jsfiddle.net/rcam67b9/
check the answers array whether the selected answer exist
v-if="question.answer.indexOf(option.option) !== -1"
Final Code
<div id="app">
<h2>Questions:</h2>
<ul>
<li v-for="question in questions">
<div class="question">{{ question.question }}</div>
<div class="label__wrap" v-for="option in question.options">
<label>
<input type="checkbox" v-model="question.answer" :value="option.option">
<span>
{{ option.option }}
</span>
</label>
<div class="sub__label__wrap" v-for="opt in option.subOptions" v-if="question.answer.indexOf(option.option) !== -1">
<label>
<input type="checkbox" v-model="option.subOptsAnswers" :value=" opt.option">
<span>
{{ opt.option }}
</span>
</label>
</div>
</div>
<br><br>
</li>
</ul>
</div>

Related

Correct bootstrap3 way to give padding on grid

I have four inputs in the same row and one of the input is conditionally visible by clicking "Click" button.
So if all four inputs are visible, the inputs datepicker1 and datepicker2 are sticked on the inputs above.
What is the correct bootstrap way to arrange and beautify the distances between the inputs?.
Gutters are not available on bootstrap 3.
<div class="col-md-3">
<select class="lookup-type-select form-control" name="created_lookup_type_1">
<option value="range">Range</option>
<option value="range__exclude">Exclude Range</option>
</select>
</div>
<div class="lookup-value">
<div class="col-md-3">
<select name="created__1_0" class="filter-input shortcut form-control" id="id_created__1_0">
</select>
</div>
<div class="col-md-8 row custom" style="display: inline;">
<div class="col-md-6">
<div class="datetimepicker input-group start-date date" data-date-format="YYYY-MM-DD HH:mm:ss">
<input class="filter-input form-control" name="created__1_1" value="" type="text" id="datepicker1" placeholder="From">
<span class="input-group-addon">
<span class="glyphicon-calendar glyphicon"></span>
</span>
</div>
</div>
<div class="col-md-6">
<div class="datetimepicker input-group end-date date" data-date-format="YYYY-MM-DD HH:mm:ss">
<input class="filter-input form-control" name="created__1_2" value="" type="text" id="datepicker2" placeholder="To">
<span class="input-group-addon">
<span class="glyphicon-calendar glyphicon"></span>
</span>
</div>
</div>
</div>
<div class="col-md-3 row month_only" style="display: none;">
<div>
<div class="datetimepicker input-group start-date date month_only" style="display: none;">
<input class="filter-input form-control" name="created__1_3" value="" type="text" id="id_created__1_3" placeholder="Month">
<span class="input-group-addon">
<span class="glyphicon-calendar glyphicon"></span>
</span>
</div>
</div>
</div>
</div>
<button type="button" class="btn remove-button">
<span class="glyphicon glyphicon-remove"></span>
</button>
<button onclick="myFunction()">Ckick</button>
<script>
function myFunction() {
if (document.getElementById('id_created__1_0').style.display != "none") {
document.getElementById("id_created__1_0").style.display = "none";
} else {
document.getElementById("id_created__1_0").style.display = "inline";
}
}
</script>
Jsfiddle example
Should be like the following pic

VueJS v-model in v-for not reactive

Consider the following:
<ul class="park-list row row-pd">
<li class="col-12" v-for="(item, index) in list.items" v-bind:key="index">
<div class="park-note px-3 py-2">
<p class="font-italic" >
{{ item.notes }} <a v-bind:href="'#collapseNoteTest'+item.id"
data-toggle="collapse" role="button" aria-expanded="false"
v-bind:aria-controls="'#collapseNoteTest'+item.id">
<i class="svg-12 ic-cyan-path" v-html="icons['ic-edit']"></i></a>
</p>
<div class="collapse" v-bind:id="'collapseNoteTest'+item.id">
<div class="form-group">
<label for="exampleFormControlTextarea1">Note:</label>
<textarea class="form-control" v-model="item.notes" rows="3"></textarea>
</div>
<div class="form-row">
<div class="col-auto ml-auto">
<button type="button" class="btn btn-outline-primary" v-bind:href="'#collapseNoteTest'+item.id" data-toggle="collapse">Cancel</button>
</div>
<div class="col-auto">
<button class="btn btn-success mb-2" v-bind:href="'#collapseNoteTest'+item.id" data-toggle="collapse">Save</button>
</div>
</div>
</div>
</div>
</li>
</ul>
No matter what I do here, item.notes does not update when I enter text into the textarea. What am I doing wrong?

Vue three columns of checkboxes in Bootstrap 4

Can anyone help me here with a 3 column layout via vue-js in bootstrap-4. I want to get my checkboxes displaying as 3 columns. The users are in order and I want the order going down the first column, then down the second and finally the third.
<div v-for="(user, index) in users">
<div class="{'controls' : (index % (users.length/3)===0)}">
<input type="checkbox" :id="'user_'+user.id" :value="user.id" class="form-check-input" v-model="form.checkedUsers">
<label class="form-check-label" for="'user_'+userr.id">
<img :src="user.photo_url" class="small-photo mx-2"> #{{ user.first_name }} #{{ user.last_name }}
</label>
</div>
</div>
Thanks
Use a Vue method to group the items into 3 groups using an array "chunk" method. use nested v-for to repeat the groups, and then the items in each group. This will put them in 3 columns ordered top-to-bottom...
Vue2 controller:
methods: {
chunk: function(arr, size) {
var newArr = [];
for (var i=0; i<arr.length; i+=size) {
newArr.push(arr.slice(i, i+size));
}
this.groupedItems = newArr;
}
},
Markup:
<div class="container" id="app">
<div class="row">
<div class="col-sm-4 py-2" v-for='(g, gIndex) in groupedItems'>
<form class="form-inline" v-for='(item, index) in g'>
<div class="form-check">
<input class="form-check-input" type="checkbox">
<label class="form-check-label">
{{ item.name }}
</label>
</div>
</form>
</div>
</div>
</div>
Demo: https://www.codeply.com/go/ZaiUsUupsr
An alternate option is to put them in 3 columns without re-iterating the .row every 3 items in the loop. All of the checkboxes can go in a single row, and they will be in 3 columns ordered left-to-right.
Demo: https://www.codeply.com/go/3gOvXFzaOw
<div class="container">
<div class="row">
<div v-for="item in items" class="col-sm-4 py-2">
<form class="form-inline">
<div class="form-check">
<input class="form-check-input" type="checkbox" >
<label class="form-check-label">
{{ item.name }}
</label>
</div>
</form>
</div>
</div>
</div>

v-if and v-else shows while page is loading?

Problem is that both divs shows on load. I tried to use v-cloak only on one but its not working. If i put on parent div then its not look good while is hidden. Any suggestion how can i display only one div while page is loading, but without v-cloak?
<div class="center_image" v-if="avatarImageSet">
<div class="checked">
<div class="seller_image {{ in_role('BusinessUsers') ? 'agency' : '' }}">
<img src="{{ home_asset('img/very_big_user_icon.png') }}" alt="" />
</div>
<div class="check_profile business">
<i class="fa fa-check"></i>
</div>
</div>
</div>
<div class="center_image" v-else>
<div class="checked">
<div class="seller_image">
<img v-bind:src="user_credentials.avatar" alt="Avatar"/>
</div>
<div class="check_profile business">
<i class="fa fa-check"></i>
</div>
</div>
</div>
This might be happening because of other errors in your code, like:
<img src="{{ home_asset('img/very_big_user_icon.png') }}" alt="" />, you should use v-bind with src
class="seller_image {{ in_role('BusinessUsers') ? 'agency' : '' }}" You should be using dynamic class binding here.
See modified code:
<div class="center_image" v-if="avatarImageSet">
<div class="checked">
<div class="seller_image" :class="{`yourclass`: in_role('BusinessUsers') === 'agency'}">
<img :src="home_asset('img/very_big_user_icon.png')" alt="" />
</div>
<div class="check_profile business">
<i class="fa fa-check"></i>
</div>
</div>
</div>
<div class="center_image" v-else>
<div class="checked">
<div class="seller_image">
<img v-bind:src="user_credentials.avatar" alt="Avatar"/>
</div>
<div class="check_profile business">
<i class="fa fa-check"></i>
</div>
</div>
</div>

How to vertically align radio buttons

I have the following markup:
<div class="container-fluid">
<h2>Transfer Options:</h2>
<div class="row well well-sm">
<div class="col-xs-6">
<div id="sendTypeRadio" class="btn-group-vertical" >
<span class="input-group-addon">
<input id="radioEmail" type="radio" name="sendType" value="email" disabled>Email
</span>
<span class="input-group-addon">
<input id="radioAttachments" type="radio" name="sendType" value="attachments" disabled>Attachments
</span>
<span class="input-group-addon">
<input id="radioBoth" type="radio" name="sendType" value="both" disabled>Email and attachments
</span>
</div><!-- /input-group -->
</div><!-- /.col-xs-6 -->
</div>
</div>
The problem is the buttons are all next to each other on the x-axis. I have been changing things for a day. Moving it out of the row tag and back in, removing almost all the formatting and such. What I want to maintain is the input-group-addon styling, but I want each button on a new row. I've searched and found nothing, any clues?
In asking my question I came up with a really crappy answer:
<div class="container-fluid">
<h2>Transfer Options:</h2>
<div class="row well well-sm">
<div class="col-xs-6">
<div id="sendTypeRadio" class="btn-group-vertical" >
<div class="row">
<div class="col-xs-2">
<span class="input-group-addon">
<input id="radioEmail" type="radio" name="sendType" value="email" disabled>Email
</span>
</div>
</div>
<div class="row">
<div class="col-xs-2">
<span class="input-group-addon">
<input id="radioAttachments" type="radio" name="sendType" value="attachments" disabled>Attachments
</span>
</div>
</div>
<div class="row">
<div class="col-xs-2">
<span class="input-group-addon">
<input id="radioBoth" type="radio" name="sendType" value="both" disabled>Email and attachments
</span>
</div>
</div>
</div><!-- /input-group -->
</div><!-- /.col-xs-6 -->
</div>
This gives me each item on a new row but absolutely ruins the styling of Bootstrap radio buttons