vue3+element-plus表格点击单行或多行都添加上背景颜色

使用场景:

1.当点击表格的多选框选中的情况下, 只有选中的框有标识 表格行字段没有太明显的区分,效果不是很好,话不多说直接上代码

<template>
  <el-table ref="table" :data="tableData" style="width: 900px" :cell-style="tableRowStyle" @row-click="clickRow" @selection-change="handleSelectionChange" :border="true">
    <el-table-column type="selection" width="50" align="center" />
    <el-table-column prop="date" label="Date" align="center" />
    <el-table-column prop="name" label="Name" align="center" />
    <el-table-column prop="address" label="Address" show-overflow-tooltip />
  </el-table>
</template>

<script setup>
import { getCurrentInstance, ref } from 'vue'
const { proxy } = getCurrentInstance()
const selectValue = ref(null)
const tableData = [
  {
    id: 1,
    date: '2016-05-03',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 2,
    date: '2016-05-02',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 3,
    date: '2016-05-04',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
  {
    id: 4,
    date: '2016-05-01',
    name: 'Tom',
    address: 'No. 189, Grove St, Los Angeles',
  },
]
//将选中的设置背景颜色
const tableRowStyle = ({ row }) => {
  let arr = []
  if (selectValue.value !== null) {
    selectValue.value.filter((item, index) => {
      arr.push(item.id)
    })
  }
  for (var i = 0; i <= arr.length; i++) {
    if (arr[i] == row.id) {
      return { backgroundColor: '#ebf5ff !important' }
    }
  }
}
// 点击其他位置也选中当前行
const clickRow = (row) => {
  proxy.$refs.table.toggleRowSelection(row, undefined)
}
// 多选框选中数据
function handleSelectionChange(selection) {
  selectValue.value = selection
}
</script>

4.运行结果:不会发动图直接发效果图片哈

 

3.使用注意点:图片标注位置尽量使用数据的唯一值,一定一定不要使用重复的值

 

总结:

很简单的代码例子希望如果有这种场景的需求的 可以试一试的

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是使用Vue 3.0+Element-Plus开发后台管理系统的步骤: 1.首先,确保你已经安装了Node.js和npm包管理器。 2.使用以下命令安装Vue CLI 4: ```shell npm install -g @vue/cli ``` 3.使用Vue CLI创建一个新项目: ```shell vue create my-project ``` 4.在创建项目时,选择使用Vue 3.0版本,并启用class-style component语法: ```shell ? Please pick a preset: Manually select features ? Check the features needed for your project: Choose Vue version, Babel, Router, Vuex, CSS Pre-processors, Linter, Unit ? Choose a version of Vue.js that you want to start the project with 3.x (Preview) ? Use class-style component syntax? Yes ? Use Babel alongside TypeScript (required for modern mode, auto-detected polyfills, transpiling JSX)? No ? Use history mode for router? (Requires proper server setup for index fallback in production) Yes ? Pick a CSS pre-processor (PostCSS, Autoprefixer and CSS Modules are supported by default): Sass/SCSS (with dart-sass) ? Pick a linter / formatter config: ESLint with error prevention only ? Pick additional lint features: Lint on save ? Where do you prefer placing config for Babel, ESLint, etc.? In dedicated config files ``` 5.安装Element-Plus和Echarts 5.0: ```shell npm install element-plus echarts@5.0 ``` 6.在main.js中引入Element-Plus和Echarts: ```javascript import { createApp } from 'vue' import App from './App.vue' import router from './router' import store from './store' import ElementPlus from 'element-plus' import 'element-plus/lib/theme-chalk/index.css' import echarts from 'echarts' const app = createApp(App) app.use(store) app.use(router) app.use(ElementPlus) app.config.globalProperties.$echarts = echarts app.mount('#app') ``` 7.现在你可以开始使用Element-Plus和Echarts来开发你的后台管理系统了。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值