How to combine props arithmetically in Vue.js? - vue.js

I have a table that looks like this:
<el-table
:data="info"
class="myform"
stripe
style="width: 100%">
<el-table-column
prop="location_name"
label="Name"
width="180">
</el-table-column>
<el-table-column
v-if="promoActive"
prop="original_price"
label="Original Price"
width="180">
</el-table-column>
<el-table-column
prop="spaces_available"
label="Spaces"
width="180">
</el-table-column>
<el-table-column
v-if="promoActive"
prop="discount_value"
label="Discount Value"
width="180">
</el-table-column>
</el-table-column>
<el-table-column
v-if="promoActive"
label="Final Price"
width="180">
</el-table-column>
</el-table>
I'm trying to make the prop value of the last table column equal to the the final price which is the original price minus the discount price. However, when I put the prop like this, it doesn't work:
<el-table-column
v-if="promoActive"
prop="(original_price - discount_value)"
label="Final Price"
width="180">
</el-table-column>
What's the best way to go about this?

you can use computed property like
computed:{
calculate(){
return this.prop1 - this.prop2;
}
}
And then called the prop

Have you considered computed properties in Vue JS ?
refer : [https://v2.vuejs.org/v2/guide/computed.html#Computed-Properties]
these properties are derived from the data and change whenever the source data is modified. This could be the recommended approach.
example :
computed:{
finalPrize : function () {
return this.original_price - this.discount_value;
}
}

First and foremost, the reason why your prop="(original_price - discount_value)" did not work is because prop attribute is evaluated as text unless you use the v-bind syntax. So this should work:
v-bind:prop="(original_price - discount_value)"
However, this is what computed properties in VueJS can be used for, to abstract all these calculation logic away from your template: you can perform the calculations in there, and simply use the v-bind directive to bind the computed value to the element. Let's name your computed property finalPrice. In your VueJS app/component code, you can do this:
computed: {
finalPrice() {
return this.original_price - this.discount_value;
}
}
Then, update your markup to use v-bind:prop="finalPrice":
<el-table-column
v-if="promoActive"
v-bind:prop="finalPrice"
label="Final Price"
width="180">
</el-table-column>
p/s: If you prefer to use shorthands, fret not: :prop="finalPrice" (note the : prefix) is syntactically identical to v-bind:prop="finalPrice".
Not recommended, but still workable: if you want to pass the arguments to perform calculations instead, you can use a method instead:
method: {
calculateFinalPrice(original, discount) {
return original- discount;
}
}
And your markup will be:
<el-table-column
v-if="promoActive"
v-bind:prop="calculateFinalPrice(original_price, discount_value)"
label="Final Price"
width="180">
</el-table-column>

You can use computed property for this type of arithmetic calculations. where you can define a function and have full control over all properties of component.
follow this link. you will get idea how you can do it.

Related

Element UI, passing id to table column with checkbox

Is there a way to pass id to el-table-column with selection type? I've tried passing a slot, but then checkbox is not rendering. Here is the code:
<el-table-column>
<template slot-scope="scope" v-if="scope.row">
<div :id="`column-${scope.row.name}`">{{ `reports-${scope.row.name}` }}</div>
</template>
</el-table-column>
<el-table-column prop="selected" align="center" type="selection" class-name="checkbox-column">
</el-table-column>
First column getting id via scoped slot.
Instead of ID if you are okay with class then use cell-class-name
In your template
<el-table your-attrs ... :cell-class-name="cellClassName">
In your script
methods: {
cellClassName({row, column, rowIndex, columnIndex}) {
if (columnIndex === 1) return `checkbox-${rowIndex}`;
}
}

Vue element-ui <el-table-column> formatter – how to display html?

How to return html formatted cell value?
I want to get custom formatted value (with html or other components) with <el-table-column> formatter.
<el-table :data="data">
<el-table-column v-for="(column, index) in columns"
:key="index" :label="column.label"
:formatter="column.formatter">
</el-table-column>
</el-table>
data() {
return {
columns: [
{
label: 'Created at',
formatter: (row, col, value, index) => {
return `<span class="date">${value}</span>`
}
},
// edit:
{
label: 'Address',
formatter: (row, col, value, index) => {
return `<mini-map :data="${row}"></mini-map>`
}
}
// other dynamic columns...
]
}
}
But cell content is displayed as escaped html string. Is there any possibility to force html?
EPIC EDIT: I added an answer with a solution.
Ok, after a few hours of brainstorming I found pretty nice solution. I'm putting it here for others - I hope this helps somebody.
Value displayed in custom column can be:
simple string (prop)
formatted string (safe, without any html or components, via original formatter)
customized value (html, component, also safe!)
In order to achieve this, I had to create custom formatter components, which I put in folder i.e. /TableFormatters/
For this purpose, there is simple functional component DatetimeFormatter that display date-time with icon.
TableFormatters/DatetimeFormatter.vue
<template functional>
<span>
<i class="icon-date"></i> {{ props.row[props.column] }}
</span>
</template>
<script>
export default {
name: 'DatetimeFormatter',
}
</script>
Here come's columns configuration:
import DatetimeFormatter from './TableFormatters/DatetimeFormatter'
// ...
data() {
return {
data: [/*...*/],
columns: [
name: {
label: 'Name',
},
state: {
label: 'State',
formatter: row => {
return 'State: '+row.state__label
}
},
created_at: {
label: 'Created at',
formatter: DatetimeFormatter
}
]
}
}
Now it's time to define <el-table>:
<el-table :data="data">
<el-table-column
v-for="(column, index) in columns"
:key="index"
:label="columns[column] ? columns[column].label : column"
:prop="column"
:formatter="typeof columns[column].formatter === 'function' ? columns[column].formatter : null">
<template #default="{row}" v-if="typeof columns[column].formatter !== 'function'">
<div v-if="columns[column].formatter"
:is="columns[column].formatter"
:row="row"
:column="column">
</div>
<template v-else>
{{ row[column] }}
</template>
</template>
</el-table-column>
</el-table>
This works like a charm. What's going on here with formatter?
First we check if the formatter is set as a function. If so, the native <el-table-column> formatter takes the control, because <template #default={row}> will not be created. Otherwise formatter component will be created (via :is attribute). However, it there is no formatter, the plain value for a row will be shown.
If you want to render custom HTML for a <el-table-column>, you will need to make use of the custom column template functionality, rather than the :formatter prop. It's going to look something like this:
<el-table :data="data">
<el-table-column
v-for="(column, index) in columns"
:key="index"
:label="column.label"
>
<template slot-scope="scope">
<span class="date">{{ scope.row.value }}</span>
</template>
</el-table-column>
</el-table>
In the general case, you can make use of the v-html directive if you need to render unescaped HTML. There are some security implications involved, so make sure you understand those before reaching for v-html.
Essentially, it boils down to this: never use v-html to render content that was provided by the user.
Use template slot scope to add html formatted columns
<script src="//unpkg.com/vue/dist/vue.js"></script>
<script src="//unpkg.com/element-ui/lib/index.js"></script>
<div id="app">
<template>
<el-table :data="tblData">
<el-table-column prop="title"></el-table-column>
<el-table-column prop="text">
<template scope="scope">
<span style="color:red;">{{scope.row.text}}</span>
</template>
</el-table-column>
</el-table>
</template>
</div>
var Main = {
data() {
return {
tblData : [
{title: 'title1', text:'text1'},
{title: 'title2', text:'text2'},
{title: 'title3', text:'text3'},
],
}
},
methods : {
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
This also works for me:
<el-table
:data="tenancy.renewals"
stripe
height="300"
style="width: 100%">
<el-table-column
prop="term"
label="Term"
:formatter="formatTerm"
width="180">
</el-table-column>
<el-table-column
prop="started"
label="Started"
:formatter="formatColDate"
width="180">
</el-table-column>
<el-table-column
prop="expiry"
:formatter="formatColDate"
label="Expiry">
</el-table-column>
<el-table-column
prop="amount"
:formatter="formatAmount"
label="Amount">
</el-table-column>
</el-table>
Then in your methods have methods corresponding to the formatter ones.
In my case, I already have mixins for numbers and dates:
...
formatTerm (row, col, value, index) {
return this.addSuffix(value, false)
},
formatColDate (row, col, value, index) {
return this.formatDate(value)
},
formatAmount (row, col, value, index) {
return this.formatMoney(value)
},
...
I feel headache, but it worked for me
<el-table :data="tableData" style="width: 100%">
<el-table-column label="shortName" width="180">
<template v-slot="scope">
<span v-html="scope ? scope.row.shortName : ''"></span>
</template>
</el-table-column>
...

Element UI Table Column that uses slot-scope Issue "Cannot read property 'column_name' of undefined"

Anyone here uses element ui framework? https://element.eleme.io/#/en-US/
I'm using its table component and using slot-scope on its columns. It's working fine until I ran npm update which of course updates the packages. Now, console has a lot of errors. And later I discovered that this slot-scope of the table column causes the issue.
Any help would be very much appreciated. Here is a fiddle of the issue.
https://jsfiddle.net/japhfortin/jkzma0v8/3/
<el-table :data=list>
<el-table-column label="First Name">
<template slot-scope="scope">{{ scope.row.first_name }}</template>
</el-table-column>
<el-table-column prop="last_name" label="Last Name">
</el-table-column>
</el-table>
data() {
return {
input: '',
list: [
{
first_name: 'Foo',
last_name: 'Bar'
}
]
}
},
The error is thrown because the value scope is an empty object on the first render. It means that the object row is undefined an it throws. You have to check that the row value is defined before accessing it. You can also use their alternative form to bind the value to the column. It depends of your use case.
<el-table :data="list">
<el-table-column prop="first_name" label="First Name"> </el-table-column>
<el-table-column prop="last_name" label="Last Name"> </el-table-column>
</el-table>
You can also use a v-if on the scope.row to ensure that the value is present at render time.
<el-table :data="list">
<el-table-column label="First Name">
<template slot-scope="scope" v-if="scope.row">
{{ scope.row.first_name }}
</template>
</el-table-column>
<el-table-column prop="last_name" label="Last Name"> </el-table-column>
</el-table>
The problem occurs because of new slot syntax in Vue.
More information at https://github.com/vuejs/rfcs/blob/master/active-rfcs/0001-new-slot-syntax.md
Default slot with text
<!-- old -->
<foo>
<template slot-scope="scope">
{{ scope}}
</template>
</foo>
<!-- new -->
<foo v-slot="scope">
{{ scope}}
</foo>
Another example using the Table component of the UI element.
Note: Upgrade the Vue to the latest version, currently it is 2.6.3.
<el-table :data="list">
<el-table-column label="First Name" v-slot="scope">
{{ scope.row.first_name }}
</el-table-column>
<el-table-column prop="last_name" label="Last Name">
</el-table-column>
</el-table>
The issue is fixed on Vue#2.6.3

Vue Js Form Validation with Components and Element UI

I am trying to find a way using vue js, components, and element ui to validate a form that is within a table and has data pushed into it from a template.
At the moment this is a snippet of the code that I have written, where if el-input is empty the message “hi” should appear as a blur event.
However, it shows up even when there is a value in the input field, and seems like the validation cannot read the value that is inserted from the template.
Any ideas on how to connect the two?
<el-form ref=“formA”>
<el-table :data=“data_list” border class=“my-table" size="mini" row-key="id">
<el-table-column property=“my_name":label='$t("attributes.person_name")' width="200">
<template slot-scope="scope">
<el-form-item :prop=“’data_list.' + scope.$index + ‘.my_name'"
:rules="{
required: true,
message: 'hi',
trigger: 'blur'
}">
<el-input v-model="scope.row.my_name" />
</el-form-item>
</template>
</el-table-column>
</el-table>

VueJS - Element UI : How to enable edit one row el-table-column

I want in a row table can be edited with enable and disable parameters, if edit button action in click then one row table is enable but if save button action in click then disable. and for the default table value is disabled.
It's my code:
<el-table :data="tableData" :key="index" style="width: 100%" stripe>
<el-table-column label="Name">
<template slot-scope="scope">
<el-input v-model="scope.row.name" :disabled="isEdit"></el-input>
</template>
</el-table-column>
<el-table-column label="Address">
<template slot-scope="scope">
<el-input v-model="scope.row.address" :disabled="isEdit"></el-input>
</template>
</el-table-column>
<el-table-column>
<template slot-scope="scope">
<el-button type="default" #click="handleSaveRow">Save</el-button>
<el-button type="primary" #click="handleEditRow">Edit</el-button>
</template>
</el-table-column>
</el-table>
but when I click edit button all rows of columns becomes enabled.
expected: edit click can change one row of table to enable
fiddle: https://jsfiddle.net/dede402/otxahoev/
It's normal as you use the same boolean for all rows. You must find a way to have one boolean per row indicating the edit mode.
Here is a working solution : https://jsfiddle.net/budgw/d3fxw5wq/1/
If you want to separate your data from the UI logic (generally a good idea) you should use a computed property in order to create a list with the edited field.
#budgw answers is correct - i would like to add to his answer. Rather than disabling the input you can make it a readonly attribute. I think its better that way and also makes your table look cleaner.
<el-input v-model="scope.row.name" :readonly="!scope.row.edited"></el-input>
Visit https://jsfiddle.net/noted/0atjsrnw/4/ for the full code.