How to change style in specific row in Element UI Table - vue.js

I'm using Element UI for my web app.
In my app, there is a table component, that is made by el-table component provided by Element UI.
And I would like to change style for the only specific row.
Firstly, please take a look at the screenshot and my code, later, I will explain what I would like to do specifically with them.
These are the screenshot of table in my app and my code.
<template>
<el-table
:data="tableData"
height="250"
style="width: 100%">
<el-table-column
prop="date"
label="Date"
width="180">
</el-table-column>
<el-table-column
prop="name"
label="Name"
width="180">
</el-table-column>
<el-table-column
prop="address"
label="Address">
</el-table-column>
</el-table>
</template>
<script>
export default {
data() {
return {
tableData: [{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-08',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-06',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-07',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}],
}
}
}
</script>
I would like to change background color blue for the row that's Data is 2016-05-03 and 2016-05-04 in the case above.

You should have a look at this https://element.eleme.io/#/en-US/component/table#table-with-status. based on the property row-class-name you can apply some logic to add an class to the row.
<el-table
:data="tableData"
style="width: 100%"
:row-class-name="tableRowClassName">
JS
methods: {
tableRowClassName({row, rowIndex}) {
if (row.date >= new Date()) {
return 'warning-row';
} else if (rowIndex === 3) {
return 'success-row';
}
return '';
}
},
Of course you've to customize this to your needs

Add this code on the component stylesheet
.el-table tr:nth-child(odd) {
background-color: #845353;
}

Additionally +
If you are using the new version of Element-UI and row-style instead of header-row-class-name, you must not use like return: 'background: red'. You have to return Object.
ex: return: {'background': 'red'}

Related

Elment Plus + Vue3 js table with dynamic table columns , input box inside the table

the actual display of the table
Got problems with data-binding under such circumstance. Input boxes from the whole row seems to be recognizede as one.
<h2>Vue3 + Element plus Dyanamic table</h2>
<el-table :data="tableData" style="width: 100%">
<el-table-column
:prop="index"
:label="item"
v-for="(item, index) in tableHeader"
:key="index"
>
<template v-slot="scope">
<el-input :name="item" v-model="scope.row.index"></el-input>
</template>
</el-table-column>
</el-table>
data() {
return {
tableHeader: {
1: "Name",
2: "Birthday",
3: "Address",
4: "Age",
5: "Tel",
},
tableData: [{
1:'',
2:'',
3:'',
4:'',
5:'',
}, {
1:'',
2:'',
3:'',
4:'',
5:'',
}]
I'm trying to load the table header(namely the number of the columns and each column's name) from back-end data. Input boxes is the main feature in this table. And i want to store the data fram input boxes into tableData and then send it back to back-end.How could i fix this?
Here is an example of a very basic Element+ Table from the Docs.
The way you are trying to bind props to index does not make sense to me.
<template>
<el-table :data="tableData" stripe style="width: 100%">
<el-table-column prop="date" label="Date" width="180" />
<el-table-column prop="name" label="Name" width="180" />
<el-table-column prop="address" label="Address" />
</el-table>
</template>
<script lang="ts" setup>
const tableData = [
{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
{
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles',
},
]
</script>

How to create event on each column in row using ant design, Vuejs

I am using ANT design table, and my problem is that I need to add events to cells on a row, for example when I click on the column with the item named John Brown, a function A will be generated. , click on the age column and function B will be created. Currently I am using the below method and it requires me to click on the correct text tag to trigger the event instead of just clicking on the cell.
<template>
<a-table :columns="columns" :data-source="data" bordered>
<template #bodyCell="{ column, text }">
<template v-if="column.dataIndex === 'name'">
<a #click="test(text)">{{ text }}</a>
</template>
<template v-if="column.dataIndex === 'age'" #click="test(text)">
<a >{{ text }}</a>
</template>
</template>
</a-table>
</template>
<script setup lang="ts">
const columns = [
{
title: 'Name',
dataIndex: 'name',
key: 'name',
},
{
title: 'Age',
dataIndex: 'age',
key: 'age',
width: 80,
},
{
title: 'Address',
dataIndex: 'test',
key: 'address 1',
ellipsis: true,}
];
const test = (text) => {
console.log(text)
};
const data = [
{
key: '1',
name: 'John Brown',
age: 32,
address: 'New York No. 1 Lake Park, New York No. 1 Lake Park',
tags: ['nice', 'developer'],
test: 'Vũ Đức Huy',
},
{
key: '2',
name: 'Jim Green',
age: 42,
address: 'London No. 2 Lake Park, London No. 2 Lake Park',
tags: ['loser'],
test: 'Trần Văn Huy',
},
{
key: '3',
name: 'Joe Black',
age: 32,
address: 'Sidney No. 1 Lake Park, Sidney No. 1 Lake Park',
tags: ['cool', 'teacher'],
test: 'Nguyễn Văn Vũ'
},
];
</script>
Is there any solution for me in this case?

How can we design as per screenshot using vuetify-data-table

I used Vuetify-data-table for the data tables, But i want another format for data table. So i added below screen what i need . Please help me .
<template>
<v-container>
<c-data-table
:data="punchInOutData"
:headers="punchInOutHeaders"
:pagination="true"
></c-data-table>
<!-- <c-typography label="Your 2 Comp Off Expires in this month 4th and 18th" />
<c-divider label="Upcoming Holidays" />
<v-row>
<v-col cols="6">
<client-only><vc-calendar is-expanded /> </client-only
></v-col>
<v-col cols="6">
<c-typography
color="#34c0be"
label="Leave Suggestion"
size="medium"
weight="normal"
/>
<div class="box">
<c-typography label="Plan A: Apply Leave on 13 Mar 2019" />
<hr style="color:#e7e7e7" />
<div class="subBox">
<v-row>
<v-col cols="4"></v-col>
<v-col cols="8" class="d-flex justify-space-between">
<span>
<c-typography
label="Benefit"
size="small"
weight="light"
class="mb-n1 mt-n1"
/>
<c-typography
label="4 Day"
size="large"
weight="light"
class="mb-n1 mt-n2"
/>
</span>
<span>
<ul class="timeline">
<li><p class="font mb-2">22/3/2019</p></li>
<li><p class="font">24/9/2019</p></li>
</ul>
</span>
<span class="button">
<v-btn color="#5d409f" style="color:white">Apply Leave</v-btn>
</span>
</v-col>
</v-row>
</div>
</div>
</v-col>
</v-row> -->
</v-container>
</template>
<script lang="ts">
import {
State,
Vue,
Component,
Prop,
Action,
Watch,
} from 'nuxt-property-decorator'
#Component({})
export default class LeavePlanner extends Vue {
punchInOutHeaders: Array<any> = [
{
text: 'S.No',
align: 'left',
value: 'sno',
},
{
text: 'Employee Info',
align: 'left',
value: 'emp_info',
},
{
text: '',
align: 'left',
value: 'duration_fields',
},
]
punchInOutData: Array<any> = [
{
sno: 1,
emp_info: {
name: 'Áshish Sharma',
employee_id: 'ATC04904',
department: 'Sales',
designation: 'Account Manager',
},
duration_fields: {
intime: {
field_name: 'In Time',
items: ['10:45 AM', '12:00 PM', '10:45 AM', '10:45 AM'],
},
},
},
{
sno: 2,
emp_info: {
name: 'Goutham Moka',
employee_id: 'ATC04904',
department: 'Sales',
designation: 'Account Manager',
},
},
{
sno: 3,
emp_info: {
name: 'Lavraju Allu',
employee_id: 'ATC04905',
department: 'Development',
designation: 'Soft Ware Engineer',
},
},
]
punchdate_items: Array<any> = [
{ name: '02 Feb 2020', id: 1 },
{ name: '04 Feb 2020', id: 2 },
{ name: '05 Feb 2020', id: 3 },
{ name: '06 Feb 2020', id: 4 },
{ name: '07 Feb 2020', id: 5 },
{ name: '08 Feb 2020', id: 6 },
{ name: '09 Feb 2020', id: 7 },
{ name: '10 Feb 2020', id: 8 },
{ name: '11 Feb 2020', id: 9 },
{ name: '12 Feb 2020', id: 10 },
{ name: '13 Feb 2020', id: 11 },
]
async created() {
console.log('locations_items: ', this.punchdate_items)
this.punchdate_items.map((item: any, index: number) => {
let formatObj = {
text: item.name,
align: 'center',
value: item.name,
sortable: false,
}
this.punchInOutHeaders.push(formatObj) //[...this.showLocations, formatObj]
})
}
}
</script>

Setting up row components for element-ui table

just started using Vue and Element UI libraray but got stuck with their table component: https://element.eleme.io/#/en-US/component/table
I'd like to have a component for each table row to handle all logic related to that row (actions, click, etc.) in this component but checking their documentation looks like the components are column-based. What am I missing?
Let me show you how to use the basic el-table, such as processing the data of a row
var Main = {
data() {
return {
tableData: [{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}, {
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
}]
}
},
methods: {
handleEdit(index, row) {
console.log(index, row);
},
handleDelete(index, row) {
// Here you can access the row data (objects in the object array) to be deleted
console.log(index, row);
this.$confirm('This will permanently delete the record. Continue?', 'Warning', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
this.tableData.splice(index, 1);
this.$message({
type: 'success',
message: 'Delete completed'
});
}).catch(() => {
this.$message({
type: 'info',
message: 'Delete canceled'
});
});
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
#import url("//unpkg.com/element-ui#2.12.0/lib/theme-chalk/index.css");
<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/element-ui#2.12.0/lib/index.js"></script>
<div id="app">
<template>
<el-table
:data="tableData"
style="width: 100%">
<el-table-column
label="Date"
width="180">
<template slot-scope="scope">
<i class="el-icon-time"></i>
<span style="margin-left: 10px">{{ scope.row.date }}</span>
</template>
</el-table-column>
<el-table-column label="Name" width="180">
<template slot-scope="scope">
<el-tag size="medium">{{ scope.row.name }}</el-tag>
</template>
</el-table-column>
<el-table-column label="Operations">
<template slot-scope="scope">
<el-button
size="mini"
#click="handleEdit(scope.$index, scope.row)">Edit</el-button>
<el-button
size="mini"
type="danger"
#click="handleDelete(scope.$index, scope.row)">Delete</el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>
Based on Sugars' answer you can toggle an input to allow edits throughout the columns on each row. I've made a simple demo based on his answer.
var Main = {
data() {
return {
tableData: [{
date: '2016-05-03',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
editable: false
}, {
date: '2016-05-02',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
editable: false
}, {
date: '2016-05-04',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
editable: false
}, {
date: '2016-05-01',
name: 'Tom',
address: 'No. 189, Grove St, Los Angeles'
editable: false
}]
}
},
methods: {
handleEdit(index, row) {
console.log(index, row);
row.editable = !row.editable;
},
handleDelete(index, row) {
// Here you can access the row data (objects in the object array) to be deleted
console.log(index, row);
this.$confirm('This will permanently delete the record. Continue?', 'Warning', {
confirmButtonText: 'OK',
cancelButtonText: 'Cancel',
type: 'warning'
}).then(() => {
this.tableData.splice(index, 1);
this.$message({
type: 'success',
message: 'Delete completed'
});
}).catch(() => {
this.$message({
type: 'info',
message: 'Delete canceled'
});
});
}
}
}
var Ctor = Vue.extend(Main)
new Ctor().$mount('#app')
#import url("//unpkg.com/element-ui#2.12.0/lib/theme-chalk/index.css");
<script src="//unpkg.com/vue/dist/vue.min.js"></script>
<script src="//unpkg.com/element-ui#2.12.0/lib/index.js"></script>
<div id="app">
<template>
<el-table
:data="tableData"
style="width: 100%">
<el-table-column
label="Date"
width="180">
<template slot-scope="scope">
<i class="el-icon-time"></i>
<span style="margin-left: 10px" v-if="!scope.row.editable">{{ scope.row.date }}</span>
<el-date-picker v-model="scope.row.date" v-if="scope.row.editable"></el-date-picker>
</template>
</el-table-column>
<el-table-column label="Name" width="180">
<template slot-scope="scope">
<el-tag size="medium" v-if="!scope.row.editable">{{ scope.row.name }}</el-tag>
<el-input v-model="scope.row.name" v-if="scope.row.editable"></el-input>
</template>
</el-table-column>
<el-table-column label="Operations">
<template slot-scope="scope">
<el-button
size="mini"
#click="handleEdit(scope.$index, scope.row)">Edit</el-button>
<el-button
size="mini"
type="danger"
#click="handleDelete(scope.$index, scope.row)">Delete</el-button>
</template>
</el-table-column>
</el-table>
</template>
</div>

Vuetify / autocomplete : styling group

I need to style the groups displaying in the autocomplete vuetify component. Here is the example of the vuetify Autocomplete component : https://vuetifyjs.com/en/components/autocompletes#autocomplete
<v-autocomplete
v-model="friends"
:disabled="isUpdating"
:items="people"
box
chips
color="blue-grey lighten-2"
label="Select"
item-text="name"
item-value="name"
multiple
>
<template v-slot:selection="data">
<v-chip
:selected="data.selected"
close
class="chip--select-multi"
#input="remove(data.item)"
>
<v-avatar>
<img :src="data.item.avatar">
</v-avatar>
{{ data.item.name }}
</v-chip>
</template>
<template v-slot:item="data">
<template v-if="typeof data.item !== 'object'">
<v-list-tile-content v-text="data.item"></v-list-tile-content>
</template>
<template v-else>
<v-list-tile-avatar>
<img :src="data.item.avatar">
</v-list-tile-avatar>
<v-list-tile-content>
<v-list-tile-title v-html="data.item.name"></v-list-tile-title>
<v-list-tile-sub-title v-html="data.item.group"></v-list-tile-sub-title>
</v-list-tile-content>
</template>
</template>
</v-autocomplete>
with the data :
people: [
{ header: 'Group 1' },
{ name: 'Sandra Adams', group: 'Group 1', avatar: srcs[1] },
{ name: 'Ali Connors', group: 'Group 1', avatar: srcs[2] },
{ name: 'Trevor Hansen', group: 'Group 1', avatar: srcs[3] },
{ name: 'Tucker Smith', group: 'Group 1', avatar: srcs[2] },
{ divider: true },
{ header: 'Group 2' },
{ name: 'Britta Holt', group: 'Group 2', avatar: srcs[4] },
{ name: 'Jane Smith ', group: 'Group 2', avatar: srcs[5] },
{ name: 'John Smith', group: 'Group 2', avatar: srcs[1] },
{ name: 'Sandra Williams', group: 'Group 2', avatar: srcs[3] }
],
Here is the default style :
Do you have an idea ?
I don't believe there is an option to style the subheader, but you can style it using css.
.v-autocomplete-content .v-select-list .v-subheader {
color: blue;
}