Vue.js Rendering table with multiple tbody - vue.js

Basically I have five objects in an array, in every object, there is an attribute called isLastRow with boolean value.
I want to have a table with multiple tbody tag, when the item.isLastRow is true, the next index would create new tbody with tr. Obviously, my current attempt just iterates the items and render five tbody, the expect result would be just two tbody. The first tbody will have two tr and the last tbody has three tr.
let items = [
{
id: 0,
text: a,
isLastRow: false,
},
{
id: 1,
text: a,
isLastRow: true,
},
{
id: 2,
text: b,
isLastRow: false,
},
{
id: 3,
text: b,
isLastRow: false,
},
{
id: 4,
text: b,
isLastRow: true,
},
]
<template v-for="item in items">
<tbody :key="item.id">
<tr>
<td>
{{ item.text }}
</td>
</tr>
</tbody>
</template>
This is the result I expect to be
<table>
<tbody>
<tr>
<td rowspan="2"></td>
<td>a</td>
</tr>
<tr>
<td>a</td>
</tr>
</tbody>
<tbody>
<tr>
<td rowspan="3"></td>
<td>b</td>
</tr>
<tr>
<td>b</td>
</tr>
<tr>
<td>b</td>
</tr>
</tbody>
</table>

You are going to have to use a computed property to format your data structure to be something like
[
[
{ id: 0, text: a, isLastRow: false },
{ id: 1, text: a, isLastRow: true },
],
[
{ id: 2, text: b, isLastRow: false },
{ id: 3, text: b, isLastRow: false },
{ id: 4, text: b, isLastRow: true },
],
];
e.g.,
computed: {
getFormatedItems() {
const sections = [[]];
let index = 0;
this.items.forEach(item => {
if (item.lastRow) {
index++;
section[index] = [];
}
section[index].push(item);
});
},
},
Then you can write you template like
<template v-for="(section, index) in getFormatedItems">
<tbody :key="index">
<tr v-for="item in section" :key="item.id">
<td>{{item.text}}</td>
</tr>
</tbody>
</template>

Related

Vue JS Checkbox select all With Nested Array

I’m not sure If I am doing this correctly but I am trying to collect an Array of Id’s that I will use to update data on my back end,
I implemented checkboxes but I do not know how to add support when parent checkbox is selected to select all children checkbox , and also when select all children checkbox to select parent checkbox ?
here is i'm tried this so far:
export default {
data() {
return {
userlistes: [
{
id: 2,
username: "Larry",
department_id: 3,
department: {
department_name: "IT",
id: 3,
},
worklists: [
{
id: 278,
user_id: 2,
task_id: 1,
date: "2021-07-30",
hour: 2,
description: "A",
is_overtime: false,
overtime_hour: 0,
task: {
taskname: "Task A",
},
hr_checked: false,
},
{
id: 277,
user_id: 2,
task_id: 1,
date: "2021-07-30",
hour: 3,
description: "B",
is_overtime: false,
overtime_hour: 0,
task: {
taskname: "Task B",
},
hr_checked: false,
},
],
},
{
id: 4,
username: "Tom",
department_id: 2,
department: {
department_name: "Business",
id: 2,
},
worklists: [
{
id: 259,
user_id: 4,
task_id: 7,
date: "2021-07-27",
hour: 6.5,
description:
"A",
is_overtime: false,
overtime_hour: 0,
task: {
taskname: "Task A",
},
hr_checked: false,
},
{
id: 260,
user_id: 4,
task_id: 7,
date: "2021-07-27",
hour: 0.5,
description: "B",
is_overtime: false,
overtime_hour: 0,
task: {
taskname: "Task B",
},
hr_checked: false,
},
],
},
],
isChecklist: [],
checkAll: false,
};
},
methods: {
clickCheckAll() {
var _this = this;
_this.checkAll = !_this.checkAll;
for (var i = 0; i < _this.userlistes.worklists.length; i++) {
var checkedData = _this.userlistes.worklists[i];
checkedData.hr_checked = _this.checkAll;
updateWorkhourAPI(checkedData.id, checkedData);
}
},
clickCheckbox(id, worklist) {
var _this = this;
worklist.hr_checked = !worklist.hr_checked;
if (worklist.manager_checked) {
_this.isChecklist.push(id);
updateWorkhourAPI(id, worklist);
} else {
var last = _this.isChecklist.length - 1;
_this.isChecklist.splice(last, 1);
updateWorkhourAPI(id, worklist);
}
if (_this.isChecklist.length == _this.userlistes.length) {
_this.checkAll = true;
} else {
_this.checkAll = false;
}
},
},
};
<b-card no-body class="mb-1" v-for="users in userlistes" :key="users.id">
<b-card-header header-tag="header" class="p-0" role="tab">
<div class="d-grid gap-2">
<b-button
block
variant="outline-primary"
v-b-toggle="`accordion-${users.id}`"
>
{{ users.username }}
</b-button>
</div>
</b-card-header>
<b-collapse
:id="`accordion-${users.id}`"
accordion="table-accordion"
role="tabpanel"
>
<table class="table table-striped table-bordered">
<thead>
<tr>
<th colspan="10">
<h3 style="text-align: center">
{{ users.username }} Work-Lists
</h3>
</th>
</tr>
<tr>
<th>Task Name</th>
<th>Date</th>
<th>Description </th>
<th>Hour (hr)</th>
<th>Overtime </th>
<th>Overtime Hour (hr)</th>
<th>
<label class="form-checkbox">
<input
type="checkbox"
#click="clickCheckAll()"
v-model="checkAll"
/>
<i class="form-icon"></i>
</label>
</th>
</tr>
</thead>
<tbody>
<tr v-for="worklist in users.worklists" :key="worklist.id">
<td>{{ worklist.task.taskname }}</td>
<td>{{ worklist.date }}</td>
<td>{{ worklist.description }}</td>
<td>{{ worklist.hour }}</td>
<td>{{ worklist.is_overtime ? "Yes" : "No" }}</td>
<td>{{ worklist.overtime_hour }}</td>
<td>
<label class="form-checkbox">
<input
type="checkbox"
#click="clickCheckbox(worklist.id, worklist)"
v-model="worklist.hr_checked"
/>
<i class="form-icon">
{{ worklist.hr_checked }}
</i>
</label>
</td>
</tr>
</tbody>
</table>
</b-collapse>
</b-card>
i figured it out how to solve this problem: count array index ,
thanks advanced
<b-card no-body class="mb-1" v-for="(users, idx) in userlistes" :key="users.id">
<b-collapse
:id="`accordion-${users.id}`"
accordion="table-accordion"`enter code here`
role="tabpanel"
>
<table class="table table-striped table-bordered">
<thead>
<th>
<label class="form-checkbox">
<input
type="checkbox"
#click="clickCheckAll(idx)"
:value="allChecked(idx)"
/>
<i class="form-icon"></i>
</label>
</th>
</tr>
</thead>
</table>
</b-collapse>
</b-card>
export default {
data() {
return { //.. };
},
methods: {
allChecked(idx) {
return this.userlistes[idx].worklists.some((el) => el.hr_checked)
},
clickCheckAll(idx) {
this.checkAll = !this.checkAll;
const currentUser = this.userlists[idx]
for (let i = 0; i < currentUser.worklists.length; i++) {
currentUser.worklists[i].hr_checked = this.checkAll;
}
},
},
}

[Vue.Draggable]I want to drag only some columns of the table

I want to be able to drag only some columns using this library.
https://sortablejs.github.io/Vue.Draggable/#/table-column-example
What I want to do is have a header with two lines and allow only some columns to be dragged.
However, that is not possible now.
The element specified by handle(item-handle) can be moved, but the result does not change.
The error is output to the console.
[Vue warn]: Error in v-on handler: "TypeError: Cannot read property 'key' of undefined"
TypeError: Cannot read property 'key' of undefined
Columns that are not subject to dragging are displayed so that their positions can be changed by dragging.
How can I eliminate these problems?
Version
"vue": "^2.6.11"
"vuedraggable": "^2.24.0"
"bootstrap-vue": "^2.16.0"
Vue Source
<template>
<div class="row">
<div class="col-8">
<h3>Draggable table</h3>
<table class="table table-striped">
<thead class="thead-dark">
<draggable
v-model="headers"
element="tr"
:options="{ handle: '.item-handle', group:'data-group' }"
#end="draggableEnd"
>
<th>Check</th>
<th v-for="header in headers" :key="header.key" scope="col" id="data-group">
<span class="item-handle">::</span>
{{ header.name }}
</th>
<th>End</th>
</draggable>
<tr>
<th>none</th>
<th v-for="header2 in headersSecond" :key="header2.key" scope="col">
<input type="text" :name="header2.key" :placeholder="header2.name" />
</th>
<th>none</th>
</tr>
</thead>
<tbody>
<tr v-for="item in list" :key="item.name">
<td>
<input type="checkbox" />
</td>
<td v-for="header in headers" :key="header.key">{{ item[header.key] }}</td>
<td>END</td>
</tr>
</tbody>
</table>
</div>
</div>
</template>
<script>
import draggable from "vuedraggable";
export default {
name: "Drag",
methods: {
draggableEnd(event) {
console.log(event);
this.headers.forEach((data, index) => {
console.log("index:" + index, data.key, data.name);
});
},
},
components: {
draggable,
},
data() {
return {
headers: [
{ key: "id", name: "ID" },
{ key: "name", name: "NAME" },
{ key: "sport", name: "SPORTS" },
],
headersSecond: [
{ key: "id", name: "ID2" },
{ key: "name", name: "NAME2" },
{ key: "sport", name: "SPORTS2" },
],
list: [
{ id: 1, name: "Abby", sport: "basket" },
{ id: 2, name: "Brooke", sport: "foot" },
{ id: 3, name: "Courtenay", sport: "volley" },
{ id: 4, name: "David", sport: "rugby" },
],
dragging: false,
};
},
};
</script>
You can try to set pointer-events:none for the th elements that are not supposed to be draggable.

Vue - Array push within an array push

I'm trying to achieve adding different rows for each genres array push. The problem is all the rows are simultaneously added for each genre and if I input the text, it will reflect in all rows. How do I separate the entity of each rows?
Here is my code.
Template
<table class="table">
<v-btn #click="addGenre()">Add Genre</v-btn>
<tbody>
<tr v-for="genre in genres" :key="genre.id">
<td>
<v-select/>
<v-btn #click="addRow()">Add Row</v-btn> // add row for each genre added
<tr v-for="(row, index) in rows" :key="row.id">
<td><v-textarea/></td>
</tr>
</td>
</tr>
</tbody>
</table>
export default {
data: ()=> ({
genres: [],
rows: [],
}),
methods: {
addGenre () {
this.genres.push({
genre: '',
});
},
addRow () {
this.rows.push({
row: '',
});
},
}
If I understand your issue, I think you should just store rows inside of the genre objects.
<tr v-for="(genre, genreIndex) in genres" :key="genre.id">
<td>
<v-select/>
<v-btn #click="addRow(genreIndex)">Add Row</v-btn> // add row for each genre added
<tr v-for="(row, index) in genre.rows" :key="row.id">
<td><v-textarea v-model="row.row"/></td>
</tr>
</td>
</tr>
data: ()=> ({
genres: [],
}),
methods: {
addGenre () {
this.genres.push({
genre: '',
rows: [],
});
},
addRow (genreIndex) {
this.genres[genreIndex].rows.push({
row: '',
});
},
}

Update Table Item Quantity

Guys I'm starting with Vue and I'm having a little difficulty. In the image below I have a table with some items and when I will increase the amount of the item Orange for example is increased all other items, how to fix it?
enter image description here
My code
new Vue({
el: '#app',
data() {
return {
quantity: 1,
fruits: [
{ Code: 1, Name: 'Abacaxi', Price: "50.00" },
{ Code: 2, Name: 'Abacate', Price: "50.00" },
{ Code: 3, Name: 'Morango', Price: "60.00" },
{ Code: 4, Name: 'Maçã', Price: "17.00" },
{ Code: 5, Name: 'Laranja', Price: "30.00" }
]
}
},
methods: {
add() {
this.quantity++
},
remove() {
if(this.quantity === 0) {
this.quantity = 0
} else {
this.quantity--
}
}
}
})
<script src="https://unpkg.com/vue"></script>
<div id="app">
<template>
<div class="user-list">
<table>
<thead>
<tr>
<th>#Code</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr v-for="fruit in fruits" :key="fruit.Code">
<td>
<button #click="remove">-</button>
<input type="text" :value="quantity">
<button #click="add">+</button>
</td>
<td>{{ fruit.Name }}</td>
<td>{{ fruit.Price }}</td>
</tr>
</tbody>
</table>
</div>
</template>
</div>
You should just need to have a quantity on each item in your list. You'd then pass the relevant item to add or remove.
new Vue({
el: '#app',
data() {
return {
fruits: [
{ Code: 1, Name: 'Abacaxi', Price: "50.00", quantity: 1 },
{ Code: 2, Name: 'Abacate', Price: "50.00", quantity: 1 },
{ Code: 3, Name: 'Morango', Price: "60.00", quantity: 1 },
{ Code: 4, Name: 'Maçã', Price: "17.00", quantity: 1 },
{ Code: 5, Name: 'Laranja', Price: "30.00", quantity: 1 }
]
}
},
methods: {
add(fruit) {
fruit.quantity++
},
remove(fruit) {
if(fruit.quantity !== 0) {
fruit.quantity--
}
}
}
})
<script src="https://unpkg.com/vue#2.6.10/dist/vue.js"></script>
<div id="app">
<template>
<div class="user-list">
<table>
<thead>
<tr>
<th>#Code</th>
<th>Name</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<tr v-for="fruit in fruits" :key="fruit.Code">
<td>
<button #click="remove(fruit)">-</button>
<input type="text" v-model.number="fruit.quantity">
<button #click="add(fruit)">+</button>
</td>
<td>{{ fruit.Name }}</td>
<td>{{ fruit.Price }}</td>
</tr>
</tbody>
</table>
</div>
</template>
</div>
I've also switched :value to v-model.number, which seems more likely to be what you'd want though it's not directly related to the problem mentioned in the question.

How to select All checkboxes based on condition with Vue.js

I am trying to select checkboxes based on some condition.
Select all checkboxes (select all checkboxes)
select all unread (select all unread where data array has status of
unread)
select all read (select all read where data array has status of read)
I am able to select all checkboxes on checkbox click,but unable to select it with links
I also need to fetch the ids of selected checkboxes,so i can perform action against these selected checkboxes.
Can you guys please have a look at this.
new Vue({
el: "#app",
data: {
selected: [],
messages: [{
id: 1,
text: "Learn JavaScript",
status: 'read'
},
{
id: 2,
text: "Learn Vue",
status: 'unread'
},
{
id: 3,
text: "Play around in JSFiddle",
status: 'read'
},
{
id: 4,
text: "Build something awesome",
status: 'unread'
}
]
},
methods: {
},
computed: {
selectAll: {
get: function() {
return this.messages ? this.selected.length == this.messages.length : false;
},
set: function(value) {
var selected = [];
if (value) {
this.messages.forEach(function(item) {
selected.push(item.id);
});
}
this.selected = selected;
}
}
},
})
body {
background: #20262E;
padding: 20px;
font-family: Helvetica;
}
#app {
background: #fff;
border-radius: 4px;
padding: 20px;
transition: all 0.2s;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/vue#2.5.16/dist/vue.js"></script>
<div id="app">
<h2>Message App:</h2>
<table class="table">
All,
Read,
Unread,
<thead>
<tr>
<th><input type="checkbox" v-model="selectAll"></th>
<th>Status</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in messages">
<td><input type="checkbox" v-model="selected" :value="item.id"></td>
<td>{{ item.status }}</td>
<td>{{ item.text }}</td>
</tr>
</tbody>
</table>
</div>
You might want to use click event on a element:
All,
and in javascript code:
methods: {
selectAllHandler() {
if (this.selected.length) {
this.selected = []
} else {
this.selected = this.messages.map(item => item.id);
}
}
}
Your Javascript:
new Vue({
el: "#app",
data: {
selected: [],
messages: [{
id: 1,
text: "Learn JavaScript",
status: 'read'
},
{
id: 2,
text: "Learn Vue",
status: 'unread'
},
{
id: 3,
text: "Play around in JSFiddle",
status: 'read'
},
{
id: 4,
text: "Build something awesome",
status: 'unread'
}
]
},
methods: {
selectAll() {
this.selected = this.messages.map((message) => message.id);
}
}
})
Your HTML:
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://cdn.jsdelivr.net/npm/vue#2.5.16/dist/vue.js"></script>
<div id="app">
<h2>Message App:</h2>
<table class="table">
All,
Read,
Unread,
<thead>
<tr>
<th><input type="checkbox" v-model="selectAll"></th>
<th>Status</th>
<th>Message</th>
</tr>
</thead>
<tbody>
<tr v-for="(item,index) in messages">
<td><input type="checkbox" v-model="selected" :value="item.id"></td>
<td>{{ item.status }}</td>
<td>{{ item.text }}</td>
</tr>
</tbody>