How can I multiple the two fields? - vue.js

How can I multiple the Unit price and the value of the Unit price and the vat?
Example:
Quantity: 2
Vat: 15%
Unit price: 2000
=2000x1.15 (Unit price x vat)
=2300x2
Can someone help me with this? Thank you. I am using Vue/Nuxt. I don't know how to do it.
<el-table-column label="Quantity">
<template slot-scope="scope">
<el-form-item>
<el-input
v-model="scope.row.quantity"
placeholder="Input quantity..."
class="w-full"
required
>
</el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="Unit Price">
<template slot-scope="scope">
<el-form-item>
<el-input
v-model="scope.row.unit_price"
placeholder="Input unit price..."
class="w-full"
required
>
</el-input>
</el-form-item>
</template>
</el-table-column>
<el-table-column label="VAT">
<template slot-scope="scope">
<el-select
v-model="scope.row.vat"
:remote-method="remoteMethod"
remote
filterable
style="width: 100%"
>
<el-option
v-for="{ name } in tax"
:key="name"
:label="name"
:value="name"
>
</el-option>
</el-select>
</template>
</el-table-column>
<el-table-column label="Total">
<template slot-scope="scope">
<el-form-item>
<el-input
v-model="scope.row.total"
placeholder="Input total..."
class="w-full"
required
value="scope.row.quantity * scope.row.vat"
>
</el-input>
<span></span>
</el-form-item>
</template>
</el-table-column>

If I read the code correctly you are very close:
In this segment:
<el-input
v-model="scope.row.total"
placeholder="Input total..."
class="w-full"
required
value="scope.row.quantity * scope.row.vat"
>
You should remove the v-model and use v-bind:value instead of value:
<el-input
placeholder="Input total..."
class="w-full"
required
v-bind:value="scope.row.quantity * scope.row.vat"
>
// or: #value="scope.row.quantity * scope.row.vat"
If you want the total to be editable by the user, you could do the following
<el-input
placeholder="Input total..."
class="w-full"
required
v-bind:value="scope.row.quantity * scope.row.vat"
#input="scope.$row.total = $event.target.value"
>
But, this means that $row.total will only be set when the user changes it.
Another strategy would be to calculate the total field for each row before you give it to the template, then you would only use v-model="scope.$row.total" and omit v-bind:value.

Related

How to delete a row in el-table by using vue js

Here is my el-table code-
<div class="row">
<div class="col-sm-12">
<el-table :data="membersList">
<el-table-column label="Name" prop="member_name" align="center">
</el-table-column>
<el-table-column label="Email" prop="member_email">
</el-table-column>
<el-table-column label="Role" prop="member_role"> </el-table-column>
<el-table-column
align="center"
label="Designation"
prop="member_designation"
></el-table-column>
<el-table-column
align="center"
label="Country"
prop="member_country"
></el-table-column>
<el-table-column label="Operations">
<template slot-scope="scope">
<base-button
type="info"
#click="editMember(scope.$index, scope.row)"
class="btn-sm mr-2"
>Edit</base-button
>
<base-button
type="danger"
#click="deleteMember(scope.$index, scope.row)"
class="btn-sm"
>Delete</base-button
>
</template>
</el-table-column>
</el-table>
</div>
</div>
I'm expecting to delete the rows from the table by clicking the delete button. The data rendering from the backend server, now I added the delete button but don't know how to create a function to delete the row when clicking on it.
You already have the index of the row, so just use vue.delete (this.$delete) like this-
deleteMember(index, row) {
this.$delete(this.membersList, index);
},
Read more about vue.delete in the documentation.

add an icon next to each row in the table using bootstrap vue?

I have a table that uses bootstrap Vue's table. Each row of the table corresponds to an item. My current problem is I need to add an icon next to each row, and display them every time I hover over that row, and then do some function when I click on this icon. But I can't find a way to add an icon. I have tried following the instructions on https://bootstrap-vue.org/docs/components/table with using slots but it only works for #head and #cell. Need an idea on this issue. This is my code and a picture describe my problem
<b-table
ref="table"
class="minh--30 mh--100 overflow-y-auto"
bordered
responsive
:items="items"
:fields="fields"
>
<template #head()="data">
<span>{{ $t(data.field.label) }}</span>
</template>
<template #cell(field)="data">
<span
v-if="data.item.isDrag"
class="d-block p-3"
>{{ data.item.field }}</span>
<b-dropdown
v-else
right
no-caret
variant="white"
class="minw--40 w-100"
menu-class="w-100 mh--24 overflow-auto minw-unset"
>
<template #button-content>
<div
class="flex-center minh--11 text-normal position-relative px-2"
>
<span class="pr-5 word-break text-line-clamp-1">{{ data.item.field }}</span>
<i
class="fas fa-chevron-down position-absolute top-50 end--1 translate-middle-y px-2"
/>
</div>
</template>
<b-dropdown-item
v-for="item in listField"
:key="item.id"
variant="normal py-2"
class="fs-12 fs-xl-15"
#click="selectField(item, data)"
>
<span class="word-break text-line-clamp-1">{{ $t(item.text) }}</span>
</b-dropdown-item>
</b-dropdown>
</template>
<template #cell(action)="data">
<b-dropdown
right
no-caret
variant="white"
class="minw--40 w-100"
menu-class="w-100 mh--24 overflow-auto minw-unset"
>
<template #button-content>
<div
class="flex-center minh--11 text-normal position-relative px-2"
>
<span class="pr-5 word-break text-line-clamp-1">{{ data.item.action }}</span>
<i
class="fas fa-chevron-down position-absolute top-50 end--1 translate-middle-y px-2"
/>
</div>
</template>
<b-dropdown-item
v-for="item in listDropdown"
:key="item.id"
variant="normal py-2"
class="fs-12 fs-xl-15"
#click="selectItem(item, data)"
>
<span class="word-break text-line-clamp-1">{{ $t(item.text) }}</span>
</b-dropdown-item>
</b-dropdown>
</template>
<template #cell(selectCharacter)="data">
<b-dropdown
right
no-caret
variant="white"
class="minw--40 w-100"
menu-class="w-100 mh--24 overflow-auto minw-unset"
>
<template #button-content>
<div
class="flex-center minh--11 text-normal position-relative px-2"
>
<span class="pr-5 word-break text-line-clamp-1">{{ data.item.selectCharacter }}</span>
<i
class="fas fa-chevron-down position-absolute top-50 end--1 translate-middle-y px-2"
/>
</div>
</template>
<b-dropdown-item
v-for="item in listCharacter"
:key="item.id"
variant="normal py-2"
class="fs-12 fs-xl-15"
#click="selectCharacter(item, data)"
>
<span class="word-break text-line-clamp-1">{{ $t(item.text) }}</span>
</b-dropdown-item>
</b-dropdown>
</template>
<template #cell(inputCharacter)="data">
<input
v-model="data.item.inputCharacter"
type="text"
class="form-control h--11 border-0"
>
</template>
<template #cell(startPosition)="data">
<input
v-model="data.item.startPosition"
type="number"
class="form-control h--11 border-0"
>
</template>
<template #cell(characterCount)="data">
<input
v-model="data.item.characterCount"
type="number"
class="form-control h--11 border-0"
>
</template>
<template #cell(needReplace)="data">
<input
v-model="data.item.needReplace"
type="text"
class="form-control h--11 border-0"
>
</template>
<template #cell(replace)="data">
<input
v-model="data.item.replace"
type="text"
class="form-control h--11 border-0"
>
</template>
<template #cell(delete)="data">
<div class="flex-center pt-1">
<input
v-if="!data.item.isDrag"
v-model="data.item.delete"
type="checkbox"
>
</div>
</template>
</b-table>
Based on reading the documentation, the table component wasn't designed for this use case because you are adding icons that are outside of the table itself.
One alternative would be to use the grid system https://bootstrap-vue.org/docs/components/layout#layout-and-grid-system to create one narrow column on the left for the icons and one wide column on the right for the table. In the narrow column, you could create rows that are exactly the height of each row of the table so that the items stay aligned with each row.
Another alternative would be to use the #cell slot, and put an element inside that uses CSS, possibly the transform property (https://www.w3schools.com/cssref/css3_pr_transform.asp), to make the element appear to the left of where it really is.

Vue element-ui table columns draggable

I'm using that for table now, but the header of the table is able to drag for now. How can I drag the table's columns?
<el-table
ref="tableRef"
class="table-responsive table-flush"
:data=tableData"
row-key="id"
header-row-class-name="thead"
#sort-change="sortChange"
<el-table-column label="Address" min-width="200px" prop="address">
<template #default="{ row }">
<span>{{ row.address }}</span>
</template>
</el-table-column>
<el-table-column label="Amount" min-width="150px" prop="commitment">
<template #default="{ row }">
<span>{{ row.amount }} {{ shortCurrency }}</span>
</template>
</el-table-column>
<el-table-column
label="Claimable"
min-width="130px"
prop="total"
>
<template #default="{ row }">
<span>
{{ row.amount / currentPrice }}
</span>
</template>
</el-table-column>
</el-table>
mounted() {
this.initializeTable()
},
method: {
initializeTable() {
const theadColumn = this.$refs.tableRef.$el.querySelector(
'.el-table__header-wrapper .el-table__header thead tr'
)
Sortable.create(theadColumn, {
draggable: 'th',
onEnd: this.dragReorderColumn,
})
},
}
I'm using vue element-ui. I want to make to move that header and body of table in same time when I drag the table's header. Is it possible?

How to get all the values of el-rate element embedded in a el-table

<el-table-column label="id" width="80">
<template slot-scope="scope">
<span style="margin-left: 10px">{{(currentPage-1) * pageSize +TableMovie.indexOf(scope.row) +1 }} </span>
</template>
</el-table-column>
<el-table-column label="name" width="120">
<template slot-scope="scope">
<span style="margin-left: 10px">{{ scope.row.movie }}</span>
</template>
</el-table-column>
<el-table-column label="Rate" width="120">
<template slot-scope="scope">
<el-rate
v-model="scope.row.evaValue"
#change="handleRate(scope.row.id,scope.row.evaValue)"
show-text>
</el-rate>
I want to collect the rate for each movie. When user clicks the submit button, the database will be updated correspondingly.
I would like to know how to collect the rate, for the v-model for el-rate is a single value.

Addings Rows in Element In Vue Js

I'm trying to display rows using the Element IO in VueJs. My problem is why is my code not outputting and how can i add some rows and remove some rows? Is there something wrong with my code? I've attached a v-for but it seems it can't work. I'm sorry i'm new to element-ui. Please see my code below.Thank you.
<template>
<div>
<el-form>
<el-table v-for='(item, index) in items' :key='index'>
<el-table-column
sortable="true"
label="Item">
<template>
<el-input v-model="item.item_id"></el-input>
</template>
</el-table-column>
<el-table-column
sortable="true"
label="Quantity">
<template>
<el-input v-model="item.quantity"></el-input>
</template>
</el-table-column>
<el-table-column
sortable="true"
label="Unit">
<template>
<el-input v-model="item.quantity"></el-input>
</template>
</el-table-column>
<el-table-column
sortable="true"
label="Unit Price">
<template>
<el-input v-model="item.unit_price"></el-input>
</template>
</el-table-column>
<el-table-column
fixed="right"
property="action"
label="Action">
<template>
<el-button type="danger" size="small">Remove</el-button>
</template>
</el-table-column>
</el-table>
<br>
<el-form-item style="float:right;">
<el-button type="submit" #click.prevent="createNewPurchaseOrder">Create</el-button>
<el-button>Cancel</el-button>
</el-form-item>
</el-form>
</div>
</template>
<script>
export default {
data () {
return {
items: [
{
item_id: '1',
quantity: '8',
unit_id: 'Gram',
unit_price: '100'
}
]
}
},
methods: {
createNewPurchaseOrder () {
console.log(this.$data)
}
}
}
</script>
You don't need to iterate your self. el-table component from Element-Ui is taking a array as data props to work. See Table Props for el-table component in ElementUI
This should works :
<el-table :data="items">
...
</el-table>
Each el-table-column must be a key of your item in your item list by adding prop='myKey' to each el-table-column
For exemple if I got a array of items like this :
[{
id : 12,
firstname : "John",
lastname : "Doe"
}]
I will have a table like this
<el-table :data="items">
<el-table-column prop="id" label="ID">...</el-table-column>
<el-table-column prop="firstname" label="Firstname">...</el-table-column>
<el-table-column prop="lastname " label="Lastname">...</el-table-column>
</el-table>
When you will remove or add an item to your array, the :data props of the el-table will be reactive to changes.
Take more look at element table documentation
You have to pass a data property to el-table and a prop property to el-table-column
<template>
<div>
<el-form>
<el-table :data="items" :key='index'>
[...]
</el-table>
<br>
<el-form-item style="float:right;">
<el-button type="submit" #click.prevent="createNewPurchaseOrder">Create</el-button>
<el-button>Cancel</el-button>
</el-form-item>
</el-form>
</div>