I have a table that fills with data from json. So how i can append a new after cllicked element ?
<tbody>
<tr v-for="f in firm" class="main" >
<td id="ccc">{{ f.name.toUpperCase() }}</td>
<td>{{ f.open }}</td>
<td>{{ f.max }}</td>
<td>{{ f.min }}</td>
<td>{{ f.close }}</td>
<td>{{ f.change }}</td>
<td>{{ f.trans }}</td>
<td>{{ f.vol }}</td>
</tr>
</tbody>
This code creata for me my table with data And now i want to click one element and after this element append new tr. I try create methods but every my idea wail
If you change the data that your HTML is rendering, the HTML will update automatically. Since the code renders a <tr> for each item in the firm array, add an item to the firm array to make a new <tr>. See the demo below.
new Vue({
el: '#app',
data: {
firm: []
},
methods: {
addRow() {
this.firm.push({
name: 'new row',
open: 'open',
max: 'max',
min: 'min',
close: 'close',
change: 'change',
trans: 'trans',
vol: 'vol',
})
}
}
})
<div id="app">
<button #click="addRow">Add new row</button>
<table>
<tbody>
<tr v-for="f in firm" class="main">
<td id="ccc">{{ f.name.toUpperCase() }}</td>
<td>{{ f.open }}</td>
<td>{{ f.max }}</td>
<td>{{ f.min }}</td>
<td>{{ f.close }}</td>
<td>{{ f.change }}</td>
<td>{{ f.trans }}</td>
<td>{{ f.vol }}</td>
</tr>
</tbody>
</table>
</div>
<script src="https://unpkg.com/vue"></script>
Related
I am getting some data from an Api and I want to check if the vtype is "car". Here is how I loop :
<tr v-for="vehicles_group in vehicles_groups.data" :key="vehicles_group.gid">
<td>
<input type="checkbox">
</td>
<!-- <td>{{ vehicles_groups.gid }} </td> -->
<td>{{ vehicles_group.title }}</td>
<td>{{ vehicles_group.vtype }}</td>
<td>{{ vehicles_group.company }}</td>
<td>{{ vehicles_group.active }}</td>
<td>{{ vehicles_group.priceincludes }}</td>
<td>{{ vehicles_group.categories }}</td>
<td>{{ vehicles_group.earlybook }}</td>
<td>{{ vehicles_group.earlybook }}</td>
</tr>
Is there a way in vue to check if the value of a Api output is car console something?
For example
if {{ vehicles_group.vtype }} == car{
console.log('its a car', vehicles_group.vtype )
}
Sure.
If you want to use v-if in your template:
<td v-if="vehicles_group.vtype == 'car'">
It's a car
</td>
<td v-else>
It's not a car
</td>
If you want to this in your component JavaScript code, you can do it as usual:
if (vehicles_group.vtype == "car") {
console.log('its a car', vehicles_group.vtype )
}
I want to use a click event in my table to go to the detailpage of the clicked row with the exception on the first and last td element.
Example:
<tr v-for="(row, key) in $rows" :key="row.id">
<td>*Input checkbox*</td>
<td>{{ row.user.type }}</td>
<td>{{ date | moment('MMMM') }}</td>
<td>{{ row.customer.name }}</td>
<td>{{ row.user.name }}</td>
<td>{{ row.total }}</td>
<td>*Icon to archive*</td>
</tr>
What I can't use because of exception on first and last element:
<tr #click="detailPage(row.user.id)" v-for="(row, key) in $rows" :key="row.id">
What I don't want to use because of repeating code:
<tr v-for="(row, key) in $rows" :key="row.id">
<td>*Input checkbox*</td>
<td #click="detailPage(row.user.id)">{{ row.user.type }}</td>
<td #click="detailPage(row.user.id)">{{ date | moment('MMMM') }}</td>
<td #click="detailPage(row.user.id)">{{ row.customer.name }}</td>
<td #click="detailPage(row.user.id)">{{ row.user.name }}</td>
<td #click="detailPage(row.user.id)">{{ row.total }}</td>
<td>*Icon to archive*</td>
</tr>
What I tried but didn't trigger the click event:
<tr v-for="(row, key) in $rows" :key="row.id">
<td>*Input checkbox*</td>
<template #click="detailPage(row.user.id)">
<td>{{ row.user.type }}</td>
<td>{{ date | moment('MMMM') }}</td>
<td>{{ row.customer.name }}</td>
<td>{{ row.user.name }}</td>
<td>{{ row.total }}</td>
<td>*Icon to archive*</td>
</template>
</tr>
How I could solve with JQuery:
$('table#tbl > tbody > tr > td').not(':first').not(':last').click(function() {
// detailpage
})
Method from click event
methods: {
detailPage (id) {
this.$router.push({path: `/timesheets/view/${id}`})
},
Is there a right way in Vue without repeating code?
You can add this event modifier on the td element you don't want to trigger with the click :
v-on:click.stop=""
or just :
#click.stop
and leave the #click on the tr element.
You can check the documentation for more informations :
https://v2.vuejs.org/v2/guide/events.html
new Vue({
el: '#app',
data: {
message: 1
},
methods:{
add(){
this.message++;
}
}
})
td {
border: 2px solid black;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/vue/2.5.17/vue.js"></script>
<div id="app">
<table>
<tr #click="add()">
<td>Hello</td>
<td v-on:click.stop="">Visitor n° {{message}}</td>
</tr>
</table>
</div>
I have a empty table to start, A form with a add button is used to populate rows in table one by one.
Last column in the table is delete and i have populated a hyperlink with ng-click attribute but its not getting fired... i tried onclick function it works.
HTML :
<tr ng-repeat="entity in entities">
<td>{{ $index + 1 }}</td>
<td>{{ entity.entityType }}</td>
<td>{{ entity.orgType }}</td>
<td>{{ entity.entityId }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.addressLine1 }}</td>
<td>{{ entity.city }}</td>
<td>{{ entity.state }}</td>
<td>{{ entity.zip }}</td>
<td ng-bind-html="entity.edit"></td>
<td ng-bind-html="entity.remove"></td>
</tr>
Javascript this is the click event that is called to add a row in table :
$scope.entities = [];
$scope.addEntity = function() {
console.log($scope.entity);
$scope.entity.$index = $scope.entities.length;
var $el = "<a ng-click='deleteEntity(" + $scope.entity.$index + ")'>Delete</a>";
$scope.entity.edit = $sce.trustAsHtml("<a ng-click='editEntity(" + $scope.entity.$index + ")'>Edit</a>");
$scope.entity.remove = $sce.trustAsHtml($el);
$scope.entities.push($scope.entity);
$scope.reset();
$compile($el)($scope);
}
HTML is created properly, with deleteEntity function have index values.
You can use this directly:
<tr ng-repeat="entity in entities track by $index">
<td>{{ $index + 1 }}</td>
<td>{{ entity.entityType }}</td>
<td>{{ entity.orgType }}</td>
<td>{{ entity.entityId }}</td>
<td>{{ entity.name }}</td>
<td>{{ entity.addressLine1 }}</td>
<td>{{ entity.city }}</td>
<td>{{ entity.state }}</td>
<td>{{ entity.zip }}</td>
<td><a ng-click='editEntity($index)'>Edit</a></td>
<td><a ng-click='deleteEntity($index)'>Delete</a></td>
</tr>
I think this will solve your problem.
I am displaying a table of customers generated dynamically using v-for. Each row has a button that adds the customer ID in an object that will be sent to the API. The thing is, I want to add the Bootstrap .success class to the clicked row, so the user knows that the customer has been selected, but I can only achieve that all the rows in the table get the .success class. Also, I would like that when the user clicks another customer, the selected customer loses the .success class.
Here's my code:
<table class="table table-responsive table-striped table-hover">
<tbody>
<tr v-for="customer in customers">
<td><button class="btn btn-default" v-on:click.prevent="selectCustomer(customer.id)"><i class="glyphicon glyphicon-ok"></i></button></td>
<td>{{ customer.first_name }}</td>
<td>{{ customer.last_name }}</td>
<td>{{ customer.oib }}</td>
<td>{{ customer.phone }}</td>
<td>{{ customer.email }}</td>
<td>{{ customer.street }} {{ customer.city }}, {{ customer.country }}</td>
</tr>
</tbody>
export default {
data(){
return {
customers: [],
selectedCustomer: ''
}
},
methods: {
selectCustomer(id, clicked){
this.selectedCustomer = id;
console.log(this.selectedCustomer);
}
}
Thank you!
Binding success class to the row based on selectedCustomer value should help you achieve what you're looking for. Something like this, untested:
<table class="table table-responsive table-striped table-hover">
<tbody>
<tr v-for="customer in customers" v-bind:class="{'success': (customer.id == selectedCustomer)}">
<td><button class="btn btn-default" v-on:click.prevent="selectCustomer(customer.id)"><i class="glyphicon glyphicon-ok"></i></button></td>
<td>{{ customer.first_name }}</td>
<td>{{ customer.last_name }}</td>
<td>{{ customer.oib }}</td>
<td>{{ customer.phone }}</td>
<td>{{ customer.email }}</td>
<td>{{ customer.street }} {{ customer.city }}, {{ customer.country }}</td>
</tr>
</tbody>
I'm trying to figure out how to do an if statement so that if the player's batting average is more than .250 it will add a class to the tr of success.
I found the following stack question but I"m not sure which way of utilizing these I could use or should use and how.
http://stackoverflow.com/questions/15810278/if-else-statement-in-angularjs-templates
<tbody>
<tr ng-repeat="player in startingLineupArray track by $index" {{ player.playerBattingAverage > '.250' ? 'class="success"' : ' ' }}>
<td>{{ $index }}</td>
<td>{{ player.playerName }}</td>
<td>{{ player.playerPosition }}</td>
<td>{{ player.playerBattingAverage }}</td>
<td>{{ player.playerBats }}</td>
</tr>
</tbody>
Here is how you can do it...
<tbody>
<tr ng-repeat="player in startingLineupArray track by $index" ng-class="{success: player.playerBattingAverage > .250}">
<td>{{ $index }}</td>
<td>{{ player.playerName }}</td>
<td>{{ player.playerPosition }}</td>
<td>{{ player.playerBattingAverage }}</td>
<td>{{ player.playerBats }}</td>
</tr>
</tbody>
if you want to dynamically add a class, you should use ngClass instead.
AngularJS ngClass conditional