I would love to change the background color on a row in a table when i click on the tr.
That's the code of the table:
<div class="table-responsive">
<table class="table table-hover">
<thead>
<tr>
<th><small class="text-muted">#</small></th>
<th>Name</th>
<th>Heading</th>
<th>Productor</th>
</tr>
</thead>
<tbody>
<tr
v-for="company in allCompanies"
:key="company.id"
role="button"
#click="selectCompany(company)"
>
<td class="text-primary">
<small>#{{ company.id }}</small>
</td>
<td class="fw-bold">{{ company.name }}</td>
<td>{{ company.heading }}</td>
<td>
{{ company.productor }}
</td>
</tr>
</tbody>
</table>
</div>
And here the code of the method I call on the click event:
selectCompany(company) {
this.storeSelectedCompamy(company);
this.fetchShows(company.id);
console.log("change css");
}
Any help would be appreciated.
Thank you in advance
Valerio
Have a look here:
https://v2.vuejs.org/v2/guide/class-and-style.html
It looks like each <tr> corresponds to a company, so my approach would be to attach each row's state to its corresponding company as a property, and then toggle that property in selectCompany. Something like:
selectCompany(company) {
allCompanies.forEach(_company => _company.isSelected = false);
company.isSelected = true;
}
<tr
v-for="company in allCompanies"
:key="company.id"
:class="company.isSelected ? 'selected' : ''"
role="button"
#click="selectCompany(company)"
>
Related
I am trying to create easy HTML table with Vue.js. I would like to add easy Transition, but the animation does not show(it just waits for 2s) and I do not know why?
HTML:
`
<tr >
<td colspan="12">
<table style="background: rgb(188, 5, 131)" class="table table-responsive">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Character ID</th>
<th scope="col">Is alive?</th>
<th scope="col">Years</th>
<th scope="col">Actions</th>
</tr>
</thead>
<tbody>
<tr class="child" v-for="child in item.children.has_nemesis.records" :key="child.data.ID">
<td>{{ child.data.ID }}</td>
<td>{{ child.data['Character ID'] }}</td>
<td>{{ child.data['Is alive?'] }}</td>
<td>{{ child.data.Years }}</td>
<td>
<button #click="removeItem(child, item)">
Remove
</button>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</template>
</transition>
`
whole code:https://jsfiddle.net/barabas/vzqLdxkb/2/
i am trying to get the Bootstrap Tooltip to work within a vue v-for loop.
i am using vue 3 and bootstrap 5.20
my code:
<script>
....
$(document).ready(function(){
$('[data-bs-toggle="tooltip"]').tooltip();
});
export default {
.....
}
</script>
<template>
<div id="listForm" style="">
<table class="table table-striped table-bordered">
<thead >
<tr>
.....
</tr>
</thead>
<tbody>
<tr v-for="userEntry in userEntrys">
<td class="text-center">{{ userEntry.date }}</td>
<td class="text-center">{{ userEntry.place }}</td>
<td class="text-center">{{ userEntry.hours }}</td>
<td class="text-center"><a data-bs-toggle="tooltip" data-bs-placement="left" data-bs-container="body" :title="userEntry.hash">Log</a></td>
</tr>
</tbody>
</table>
</div>
</template>
i tried the tooltip outside of the v-for loop and it worked fine..
everytime i change something on the file and the vue live view in the browser updates, the tooltip within the loop works too.. but if i refresh the browser it never works again.
i hope someone can help me.
best regards Tom!
Suppose I have the following angular table
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.name}}
</td>
<td>
{{company.price}}
</td>
<td>
{{company.profit}}
</td>
</tr>
</table>
How can I make a neat solution to have table cells have a background color depending on the cell value ? lets say if company profit is positive, make it green.
in css
td.highlight {
background-color: green;
}
in html :-
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.price}}
</td>
<td [ngClass]="{'highlight': company.profit > 0}">
{{company.profit}}
</td>
</tr>
</table>
You can add this to your html :
<td [style.positive]="company.profit > 0">
and this to your css :
td.positive{
background-color: green;
}
Look into ngStyle and ngClass. Simplest use case would be to directly bind to the style property.
<table>
<tr *ngFor="let company of investTable; let i = index">
<td>
{{company.name}}
</td>
<td>
{{company.price}}
</td>
<td [style.background-color]="company?.profit > 5 ? 'green' : 'red'">
{{company.profit}}
</td>
</tr>
</table>
I'm using wkhtmltopdf to generate a pdf from html.
In the html, there is a table with thead and tbody. I use Vue in order to generate the content:
<table class="data-table">
<thead>
<tr>
<th class="title" colspan="5">Control ##{{ index + 1 }}</th>
</tr>
<tr>
<th class="number">#</th>
<th>Tittel:</th>
<th>Kommentar:</th>
<th>Mangelpunkt<br> Registrert:</th>
<th>Mangelpunkt<br> Utbedret:</th>
</tr>
</thead>
<tbody v-for="(checkpoint, cpIndex) in control.checkpoints">
<tr>
<td class="number">#{{ cpIndex + 1}}</td>
<td>#{{ checkpoint.title }}</td>
<td>#{{ checkpoint.comment }}</td>
<td class="danger center">#{{ checkpoint.rejectedDate }}</td>
<td class="success center">#{{ checkpoint.approvedDate }}</td>
</tr>
<tr v-if="checkpoint.images.length > 0">
<td class="number">#{{ cpIndex + 1 }}</td>
<td class="images" colspan="4">
<img v-for="(image, imgIndex) in checkpoint.images" :key="imgIndex" :src="base64Images[image]" alt="print image">
</td>
</tr>
</tbody>
</table>
This works fine in the browser, but if one image is tall enough to not fit on the same page as the other images, it breaks to the next page (as wanted). However, the image is overlapping the thead..
I've tried to apply all sorts of hacks to this the last hours but I simply cannot get the image to respect the thead on the next print page..
Is there anything I'm doing wrong here?
I have a is-fullwidth Bulma table in my angular7 app which works fine. But now I need to add a angular component inside each row, and td tags lies inside that component. But when I do that, table rows not spans to its full width.
<table class="table is-fullwidth">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of rows">
<app-table-row [details]="row">
</app-table-row>
</tr>
</tbody>
</table>
table-row.component.html
<td>
<div *ngIf="!showField"> {{ details.name }} </div>
<div *ngIf="showField" class="field">
<div class="control">
<input [value]="details.name" class="input" type="text">
</div>
</div>
</td>
<td>{{ details.address }}</td>
This outputs the following
if I remove the angular component and add tds normally, it spans correctly. Like this.
Why is it not work when angular component added? And how should I fix it?
The reason this is happening is probably because the td elements are not direct children of the tr, but children of the app-table-row component.
Not sure that it would make sense in your case, but you can change the selector property in the app-table-row component to select the tr itself, like so:
#Component({
selector: 'tr[app-table-row]',
...
})
export class AppTableRowComponent {}
And use it like this:
<table class="table is-fullwidth">
<thead>
<tr>
<th>Column 1</th>
<th>Column 2</th>
</tr>
</thead>
<tbody>
<tr *ngFor="let row of rows" app-table-row [details]="row">
</tr>
</tbody>
</table>
This way the app-table-row is not a separate node in the DOM.