I am dynamically adding new row with selects and inputs in table if option is selected in first column of last row.
It looks like this https://jsfiddle.net/zgykgery/ so when you select 'Service' new row is added and total price of that row is calculated taking service price, default 10% discount and amount.
I am getting service price a bit clumsy with
:value="item.id+'|'+item.price"
so I have filter to take it from value.
But it works and now I don't know how I can loop through all rows and sum price of service without dicount, sum discount and then full price with servce price and discount.
I know I should use computed properties or watcher but don't know how.
How can I compute sum of those values in table and display them in smaller table below?
Update
I am new in vue and didn't use computed properties so this is what I tried but without success:
var Main = {
data() {
return {
rows_top: [{
service: '',
position: '',
visit: '',
amount: '',
discount: 10
}],
services: [{
id: 1,
name: 'Service1',
price: 100
},
{
id: 2,
name: 'Service2',
price: 200
},
{
id: 3,
name: 'Service3',
price: 300
},
],
total_discount: 0,
total_full_price: 0
}
},
methods: {
addRow(row, index) {
// console.log(row)
if (this.rows_top[this.rows_top.length - 1].service !== '') {
this.rows_top.push({
service: '',
position: '',
visit: '',
amount: '',
discount: 10
})
}
},
deleteRow(index) {
this.rows_top.splice(index, 1)
}
},
computed: {
total_price: () => {
if (this.rows_top) {
return this.rows_top.map((r) => {
return r.amount * this.$options.filters.after_line(r.service)
})
}
}
},
filters: {
after_line: (value) => {
if (!value) return ''
let after = value.split('|')
return after.pop()
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
#import url("//unpkg.com/element-ui#2.0.11/lib/theme-chalk/index.css");
table {
border-collapse: collapse
}
table,
th,
td {
border: 1px solid #ddd
}
th,
td {
padding: 5px
}
tr:nth-child(odd) {
background-color: #f9f9f9
}
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui#2.2.0/lib/index.js"></script>
<div id="app">
<template>
<table>
<thead>
<tr>
<th>Service</th>
<th>Position</th>
<th>Visit</th>
<th>Amount</th>
<th>Price</th>
<th>Discount %</th>
<th>Full price</th>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in rows_top">
<td>
<el-select placeholder="Select" filterable="filterable" #change="addRow(row, index)" v-model="row.service">
<el-option v-for="item in services" :key="item.id" :label="item.name" :value="item.id+'|'+item.price"></el-option>
</el-select>
</td>
<td>
<el-select placeholder="Select" v-model="row.position" multiple="multiple" filterable="filterable" allow-create="allow-create" #change="checkNumber(result, index)"></el-select>
</td>
<td>
<el-select placeholder="Select" v-model="row.visit" allow-create="allow-create" filterable="filterable">
<el-option v-for="i in 10" :key="i" :label="i" :value="i"></el-option>
</el-select>
</td>
<td>
<el-select placeholder="Select" v-model="row.amount" allow-create="allow-create" filterable="filterable">
<el-option v-for="i in 30" :key="i" :label="i" :value="i"></el-option>
</el-select>
</td>
<td>{{ row.service | after_line }}</td>
<td>
<el-input v-model="row.discount"></el-input>
</td>
<td><span v-if="row.service && row.amount">{{ ($options.filters.after_line(row.service) * row.amount - (($options.filters.after_line(row.service) * row.amount) / 100) * row.discount).toFixed(2) }}</span><span v-else-if="row.service">{{ ($options.filters.after_line(row.service) - ($options.filters.after_line(row.service) / 100) * row.discount).toFixed(2) }}</span>
<el-button v-if="row.service" icon="el-icon-delete" size="mini" #click="deleteRow(index)" class="push-right"></el-button>
</td>
</tr>
</tbody>
</table><br/>
<table>
<tr>
<td>Total price</td>
<td>{{ total_price }}</td>
</tr>
<tr>
<td>Total discount</td>
<td>{{ total_discount }}</td>
</tr>
<tr>
<td>Total full price</td>
<td>{{ total_full_price }}</td>
</tr>
</table>
</template>
</div>
Here is also updated fiddle and total price is still empty on change https://jsfiddle.net/zgykgery/21/
As #Roy J said, just use a computed function to get you the values of your calculation, knowing that it will automatically change when the data from item changes
EDIT: a simple method is more designed in the case when you use a v-for loop
methods: {
priceID (item) {
return item.id + '|' + item.price
}
}
And you would use it simply like that in your Vue
<tr v-for="(row, index) in rows_top">
<td>
<el-select placeholder="Select" filterable="filterable" #change="addRow(row, index)" v-model="row.service">
<el-option v-for="item in services" :key="item.id" :label="item.name" :value="priceID(item)"></el-option>
</el-select>
</td>
...
Related
I want to manipulate data from different table (mariadb) in the same database, but idk how to make it good. It work with only single data but when there is more than 1 data it push it to right, and I want to make it vertically... Someone have a solution ? :-)
<v-simple-table >
<thead>
<tr>
<th class="text-left">
<v-card-subtitle><strong>Congé</strong></v-card-subtitle></th>
<th class="text-left">
<v-card-subtitle><strong>Absence</strong></v-card-subtitle></th>
</tr>
</thead>
<tbody>
<tr >
<td v-for="conge in conges" :key="conge.id">
{{ conge.dateDebut }} à {{ conge.dateFin}}
</td>
<td v-for="absence in absences" :key="absence.id">
{{ absence.dateDebut }} à {{ absence.dateFin }}
</td>
</tr>
</tbody>
</v-simple-table>
you can try this code :
<template>
<v-simple-table>
<thead>
<tr>
<th class="text-left">
<v-card-subtitle>
<strong>Congé</strong>
</v-card-subtitle>
</th>
<th class="text-left">
<v-card-subtitle>
<strong>Absence</strong>
</v-card-subtitle>
</th>
</tr>
</thead>
<tbody>
<tr v-for="(data, index) in mergedData" :key="index">
<td>{{ data.conge.dateDebut }} à {{ data.conge.dateFin }}</td>
<td>{{ data.absence.dateDebut }} à {{ data.absence.dateFin }}</td>
</tr>
</tbody>
</v-simple-table>
</template>
<script>
export default {
data() {
return {
mergedData: []
};
},
computed: {
absences() {
return [
{ id: 1, dateDebut: "2022-01-01", dateFin: "2022-01-02" },
{ id: 2, dateDebut: "2022-01-03", dateFin: "2022-01-04" }
];
},
conges() {
return [
{ id: 1, dateDebut: "2022-02-01", dateFin: "2022-02-02" },
{ id: 2, dateDebut: "2022-02-03", dateFin: "2022-02-04" }
];
}
},
created() {
for (let i = 0; i < this.conges.length; i++) {
this.mergedData.push({
conge: this.conges[i],
absence: this.absences[i]
});
}
}
};
</script>
I just want to copy data(Item and Price) of the first table to the second table and make increment in Quantity(Second table) when i click in the First Table ( Item). I am working on Angular 9
First Table:
Code | Item | Price | Unit |
1 | Mouse | 500 | Piece |
2 | Wire | 100 | Piece |
Second Table:
Item | Quantity| Price |
1 | 2 | 1000 |
2 | 1 | 100 |
I did a lot to work with it but I could not get any required output.
It is possible to solve using javaScript/Js/jquery
<table class="table table-hover" id="firstTable">
<thead class="table-primary">
<tr>
<td>Code</td>
<td>Item</td>
<td>Unit</td>
<td>Price</td>
</tr>
</thead>
<tbody>
<tr *ngFor="let data of itemList;" id="moveMeIntoMain"
style="cursor:pointer">
<td>{{data.itemCode}}</td>
<td>{{data.itemName}}</td>
<td><span>{{data.unitId}}</span>{{data.name}}</td>
<td>{{data.retailRate}}</td>
</tr>
</tbody>
</table>
<table class="table table-hover" id="secondTable">
<thead class="">
<tr class="table-primary">
<td>#</td>
<td>Item</td>
<td>Price</td>
<td>Quantity</td>
<td>Action</td>
</tr>
</thead>
<tbody>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
</tbody>
</table>
Js
$(document).ready(function () {
var items = [];
$("#firstTable tr").on("click", function () {
var newTr = $(this).closest("tr").clone();
$(newButtonHTML).children("button").click(function (e) {
});
$(newTr).children("td:last").html("");
items.push(newTr);
newTr.appendTo($("#secondTable"));
});
})
You can simply try in Angular way ;). Just add click handler to the each row and send row data
Working stackblitz
Template file
<table>
<thead>
<th>Code</th>
<th>Item</th>
<th>Price</th>
<th>Unit</th>
</thead>
<tr *ngFor="let data of firstTableData" (click)="updateSecondTable(data)">
<td>{{ data.Code }}</td>
<td>{{ data.Item }}</td>
<td>{{ data.Price }}</td>
<td>{{ data.Unit }}</td>
</tr>
</table>
<hr>
<table *ngIf="secondTableData.length">
<thead>
<th>Item</th>
<th>Quantity</th>
<th>Price</th>
</thead>
<tbody>
<tr *ngFor="let newdata of secondTableData">
<td>{{ newdata.Item }}</td>
<td>{{ newdata.Quantity }}</td>
<td>{{ newdata.Price }}</td>
</tr>
</tbody>
</table>
And in the typescript file
Declare a empty secondTableData array
And have a method, which firstly checks for the item existence in the second table array. If item already exists, then simply update quantity and price
If item doesn't exist, just push the item with required columns to the second table array
Component.ts
#Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular ' + VERSION.major;
firstTableData = [{
Code: 1,
Item: 'Mouse',
Price: 500,
Unit: 'Piece'
},
{
Code: 2,
Item: 'Wire',
Price: 100,
Unit: 'Piece'
},
{
Code: 3,
Item: 'Some Item',
Price: 300,
Unit: 'Some Unit'
}
];
secondTableData = [];
updateSecondTable(data) {
let foundItem = this.secondTableData.find((item) => item.Item === data.Item);
if (foundItem) {
foundItem.Quantity += 1;
foundItem.Price += data.Price;
return;
}
this.secondTableData.push({
Item: data.Item,
Quantity: 1,
Price: data.Price,
})
}
}
I have following code that sum price of product to existed number when user increase the quantity of product. But the issue with my code is if user decrease the amount price still gets increased.
Code
html
<el-input-number style="width: 100%;" ref="amount" v-on:change="amoutChanged($event, row)" v-model="row.amount" :min="1"></el-input-number>
script
data() {
return {
subtotal: 0,
}
},
methods: {
amoutChanged: function(event, row) {
console.log('amount row: ', row);
console.log('amount event: ', event);
// ISSUE: regardes that user increase or decrease the amount number this price gets increased
this.subtotal = parseInt(this.subtotal) + parseInt(row.price);
},
}
Console results
amount row: {barcoded: "8995078803078", …}
amount: (...)
barcode_id: (...)
barcoded: "8995078803078"
price: (...)
product: (...)
amount event: 2 // this is amount input by user
Question
How to properly increase and decrease price based on user action?
I have done it like this:
I changed subtotal to an computed propertie and sum it with .reduce() and i added a new property called singlePrice so we can multiply with it
var Main = {
data() {
return {
serial_numbers: [{
barcode_id: '45634643',
product: 'dfgs546',
amount: 1,
price: 100,
singlePrice: 100,
},{
barcode_id: '23523fd',
product: 'rgdg46546',
amount: 1,
price: 100,
singlePrice: 100,
},{
barcode_id: 'fdghdh',
product: '345634643',
amount: 1,
price: 100,
singlePrice: 100,
}],
total: 0,
num1: 1
};
},
computed: {
subtotal(){
return this.serial_numbers.reduce((a,v)=> a + v.price,0)
}
},
methods: {
addRow() {
var barcodes = document.createElement('tr');
this.serial_numbers.push({
barcode_id: '675476547',
product: 'hjfgj67',
amount: 1,
price: 100,
singlePrice: 100,
});
},
removeElement: function(index) {
this.serial_numbers.splice(index, 1);
},
amountChanged($event, index){
let amount = $event;
this.serial_numbers[index].amount = amount;
this.serial_numbers[index].price = this.serial_numbers[index].singlePrice * amount;
}
}
};
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
#import url("//unpkg.com/element-ui#1.4.0/lib/theme-default/index.css");
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui#1.4.0/lib/index.js"></script>
<div id="app">
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<td><strong>Serial Number</strong></td>
<td><strong>Product</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Price</strong></td>
<td width="50"></td>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in serial_numbers" :key="index">
<td>
<el-input ref="barcoded" v-model="row.barcode_id"></el-input>
</td>
<td>
<el-input ref="product" v-model="row.product" readonly></el-input>
</td>
<td>
<el-input-number style="width: 100%;" ref="amount" #change="amountChanged($event, index)" v-model="row.amount" :min="1"></el-input-number>
</td>
<td>
<el-input ref="price" v-model="row.price" readonly></el-input>
</td>
<td>
<el-link :underline="false" type="danger" v-on:click="removeElement(index);" style="cursor: pointer"><i class="fa-2x el-icon-remove"></i></el-link>
</td>
</tr>
</tbody>
</table>
<div>
<el-button type="primary" class="button btn-primary" round #click="addRow"><i class="el-icon-circle-plus"></i> Add row</el-button>
</div>
<el-row :gutter="10">
<el-col :span="8" :offset="16">
<table class="table table-bordered table-striped table-hover">
<tbody>
<tr>
<th width="100"><strong>Sub total</strong></th>
<td>
{{subtotal}}
</td>
</tr>
<tr>
<th width="100"><strong>Total</strong></th>
<td>
{{total}}
</td>
</tr>
</tbody>
</table>
</el-col>
</el-row>
</div>
I have multiple rows and I wish to get input value inside my tr refs but it returns undefined.
Code
Component
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<td><strong>Serial Number</strong></td>
<td><strong>Product</strong></td>
<td><strong>Amount</strong></td>
<td><strong>Price</strong></td>
<td width="50"></td>
</tr>
</thead>
<tbody>
<tr v-for="(row, index) in serial_numbers" :key="index">
<td>
<el-input ref="barcoded" v-model="row.barcode_id"></el-input>
</td>
<td>
<el-input ref="product" v-model="row.product"></el-input>
</td>
<td>
<el-input ref="amount" v-model="row.amount"></el-input>
</td>
<td>
<el-input ref="price" v-model="row.price" readonly></el-input>
</td>
<td>
<el-link v-on:click="removeElement(index);" style="cursor: pointer">Remove</el-link>
</td>
</tr>
</tbody>
</table>
<div>
<el-button type="primary" class="button btn-primary" round #click="addRow">Add row</el-button>
</div>
Script
methods: {
focusInput() {
this.$refs.barcode_id.focus();
},
addRow() {
var barcodes = document.createElement('tr');
this.serial_numbers.push({
barcode_id: '',
product: '',
amount: '',
price: ''
});
// try to get value of barcode input
console.log(this.refs.barcoded); // undefined
this.$nextTick(function () {
const nbBarcodes = this.$refs.barcoded.length;
this.$refs.barcoded[nbBarcodes - 1].focus();
});
},
}
Any idea?
Update
Demo
Using the serial_numbers object instead of refs
console.log(this.serial_numbers[this.serial_numbers.length - 1]);
// this was moved from above
this.serial_numbers.push({
barcode_id: '',
product: '',
amount: '',
price: ''
});
You should push the new element after retrieving the values you need
https://codepen.io/albertor24/pen/wvKYxOg
I'm not sure what could be the issue here but I'm using nuxt to make a SPA app. and I'm getting an error from an already compiled piece of code I got from codepen. link to codepen
https://codepen.io/jjelic/pen/yevNLZ?editors=1010
When I try this code my my nuxt app I get an error.
I've added a file called monitor.vue in pages folder and added the html and js like so
Is this root element error common as I have never encountered it before with html and how can I avoid?
Vue.filter('currencyDisplay', {
// model -> view
read: function(val) {
if (val > 0) {
return accounting.formatMoney(val, "$", 2, ".", ",");
}
},
// view -> model
write: function(val, oldVal) {
return accounting.unformat(val, ",");
}
});
Vue.directive('sortable', {
twoWay: true,
deep: true,
bind: function() {
var that = this;
var options = {
draggable: Object.keys(this.modifiers)[0]
};
this.sortable = Sortable.create(this.el, options);
console.log('sortable bound!')
this.sortable.option("onUpdate", function(e) {
that.value.splice(e.newIndex, 0, that.value.splice(e.oldIndex, 1)[0]);
});
this.onUpdate = function(value) {
that.value = value;
}
},
update: function(value) {
this.onUpdate(value);
}
});
var vm = new Vue({
el: '#app',
data: {
rows: [
//initial data
{
qty: 5,
description: "Something",
price: 55.20,
tax: 10
},
{
qty: 2,
description: "Something else",
price: 1255.20,
tax: 20
},
],
total: 0,
grandtotal: 0,
taxtotal: 0,
delivery: 40
},
computed: {
total: function() {
var t = 0;
$.each(this.rows, function(i, e) {
t += accounting.unformat(e.total, ",");
});
return t;
},
taxtotal: function() {
var tt = 0;
$.each(this.rows, function(i, e) {
tt += accounting.unformat(e.tax_amount, ",");
});
return tt;
}
},
methods: {
addRow: function(index) {
try {
this.rows.splice(index + 1, 0, {});
} catch (e) {
console.log(e);
}
},
removeRow: function(index) {
this.rows.splice(index, 1);
},
getData: function() {
$.ajax({
context: this,
type: "POST",
data: {
rows: this.rows,
total: this.total,
delivery: this.delivery,
taxtotal: this.taxtotal,
grandtotal: this.grandtotal,
},
url: "/api/data"
});
}
}
});
<template>
<div class="panel-body" id="app">
<table class="table table-hover">
<thead>
<tr>
<th style="width: 20px;">No.</th>
<th>Description</th>
<th style="width: 80px;">Qty</th>
<th style="width: 130px;" class="text-right">Price</th>
<th style="width: 90px;">Tax</th>
<th style="width: 130px;">Total</th>
<th style="width: 130px;"></th>
</tr>
</thead>
<tbody v-sortable.tr="rows">
<tr v-for="row in rows" track-by="$index">
<td>
{{ $index +1 }}
</td>
<td>
<input class="form-control" v-model="row.description" />
</td>
<td>
<input class="form-control" v-model="row.qty" number />
</td>
<td>
<input class="form-control text-right" v-model="row.price | currencyDisplay" number data-type="currency" />
</td>
<td>
<select class="form-control" v-model="row.tax">
<option value="0">0%</option>
<option value="10">10%</option>
<option value="20">20%</option>
</select>
</td>
<td>
<input class="form-control text-right" :value="row.qty * row.price | currencyDisplay" v-model="row.total | currencyDisplay"
number readonly />
<input type="hidden" :value="row.qty * row.price * row.tax / 100" v-model="row.tax_amount | currencyDisplay"
number />
</td>
<td>
<button class="btn btn-primary btn-xs" #click="addRow($index)">add row</button>
<button class="btn btn-danger btn-xs" #click="removeRow($index)">remove row</button>
</td>
</tr>
</tbody>
<tfoot>
<tr>
<td colspan="5" class="text-right">TAX</td>
<td colspan="1" class="text-right">{{ taxtotal | currencyDisplay }}</td>
<td></td>
</tr>
<tr>
<td colspan="5" class="text-right">TOTAL</td>
<td colspan="1" class="text-right">{{ total | currencyDisplay }}</td>
<td></td>
</tr>
<tr>
<td colspan="5" class="text-right">DELIVERY</td>
<td colspan="1" class="text-right"><input class="form-control text-right" v-model="delivery | currencyDisplay"
number /></td>
<td></td>
</tr>
<tr>
<td colspan="5" class="text-right"><strong>GRANDTOTAL</strong></td>
<td colspan="1" class="text-right"><strong>{{ grandtotal = total + delivery | currencyDisplay }}</strong></td>
<td></td>
</tr>
</tfoot>
</table>
<button #click="getData()">SUBMIT DATA</button>
<pre>{{ $data | json }}</pre>
</div>
</template>
This problem is actually a very simple problem. I don't know vue, but the render method has the same limits of react's one: every component must have only one root element in its template.
This means that a situation like this isn't accepted:
<template>
<div></div>
<div></div>
</template>
But like this is correct:
<template>
<div></div>
</template>
This means that surely, somehow in the code you didn't show us, you're putting two elements as root of your template