首页 > 其他分享 >vue + element 表格循环列添加点击事件

vue + element 表格循环列添加点击事件

时间:2022-10-08 17:58:59浏览次数:56  
标签:vue return 表格 title element let prop tableData row

<template>

<div>

<el-table       :data="tableData"       auto-reset-scroll       :max-height="520"     >       <el-table-column         header-align="center"         :label="tableLebel"         min-width="140"         fixed       >         <template slot-scope="scope">           <span>{{ scope.row.typeName }}</span>         </template>       </el-table-column>       <template v-for="year in yearList">         <el-table-column           v-if="selectedAllColumns.length"           :key="`${year}_all`"           header-align="center"           :label="`${year}`"         >           <el-table-column             v-for="column in selectedAllColumns"             :key="`${column.prop}_${year}`"             :prop="`${column.prop}_${year}`"             :label="column.columnLabel"             :min-width="getHeaderWidth(column.columnLabel)"             sortable           >             <template slot-scope="scope">               <span                 :class="column.columnLabel == '业绩(万元)' ? 'active' : ''"                 @click="column.columnLabel == '业绩(万元)' ? handleClick(scope.row,scope.column,year) : ''"               >{{ scope.row[`${column.prop}_${year}`] }}</span>             </template>           </el-table-column>         </el-table-column>         <el-table-column           v-if="selectedCutoffColumns.length"           :key="`${year}_cutoff`"           header-align="center"           :label="`${year},其中截止${deadline}`"         >           <el-table-column             v-for="column in selectedCutoffColumns"             :key="`${column.prop}_${year}`"             :prop="`${column.prop}_${year}`"             :label="column.columnLabel"             :min-width="getHeaderWidth(column.columnLabel)"             sortable           >             <template slot-scope="scope">               <span                 :class="column.columnLabel == '业绩(万元)' ? 'active' : ''"                 @click="column.columnLabel == '业绩(万元)' ? handleClick(scope.row,scope.column,year) : ''"               >{{ scope.row[`${column.prop}_${year}`] }}</span>             </template>           </el-table-column>         </el-table-column>       </template>     </el-table>

</div>

</template>

<script> import { fetchDialogTableData } from '@/api/analysis'
export default {   name: 'DataTable',   components: {     CsmTable,     MultipleSelect   },   props: {     deadline: {       type: String,       default: ''     },     tableLebel: {       type: String,       default: ''     },     analyseType: {       type: String,       default: ''     },     systemDate: {       type: String,       default: ''     }   },   data() {     return {       isLoading: false,       tableColumns: [], // 列指标       selectedColumns: [], // 已选列指标       tableData: [], // 表格数据       yearList: [], // 年列表       gridData: []     }   },   computed: {     // 全量数据已选列     selectedAllColumns() {       const { selectedColumns } = this       return selectedColumns.filter(({ cutoff }) => !cutoff)     },
    // 限制数据已选列     selectedCutoffColumns() {       const { selectedColumns } = this       return selectedColumns.filter(({ cutoff }) => cutoff)     }   },   methods: {     formatter(row) { // 业务分类(0 数据库,1 数据定制,2 数据超市)       if (row.businessType == '0') {         return '数据库'       } else if (row.businessType == '1') {         return '数据定制'       } else {         return '数据超市'       }     },     init(yearList, tableColumns, data) {       this.yearList = yearList       this.tableColumns = tableColumns       this.selectedColumns = [].concat(tableColumns)       this.tableData = data || []     },     // 请求表格弹框数据     async getDialogTableData(row, column, year) {       let params = {         analyseType: this.analyseType,         systemDate: this.systemDate,         type: row.type, // 分类Id,(省份id:*,合计:0,新客户1,老客户2)         isCutoff: column.property.indexOf('all') != -1 ? '0' : '1', // 是否查询截止数据(0 否,1 是)         year: year       }       let res = await fetchDialogTableData(params) // 后端接口定义的接口名称       if (res) {         this.gridData = res         this.isLoading = false       }     },     // 表格列点击事件     handleClick(row, column, year) {       this.isLoading = true       this.getDialogTableData(row, column, year)     },     getHeaderWidth(str) {       let strLength = this.computeStrOccupyLength(str)       strLength = Math.ceil(strLength)       return strLength * 15 + 30     }, // 计算字符串占用长度 computeStrOccupyLength(str) {   let num = 0   str.split('').forEach((s) => {     if (/[0-9a-z]|[.?()-:/\\]|\s/.test(s)) {       num += 0.5     } else if (/[A-Z]/.test(s)) {       num += 0.7     } else {       num += 1     }   })   return num },     /**  * flexWidth  * @param prop 每列的prop 可传''  * @param tableData 表格数据  * @param title 标题长内容短的,传标题  可不传  * @param num 列中有标签等加的富余量  * @returns 列的宽度  * 注:prop,title有一个必传  */     flexWidth(prop, tableData, title, num = 0) {       if (tableData.length === 0) {//表格没数据不做处理         return;       }       let flexWidth = 0;//初始化表格列宽       let columnContent = '';//占位最宽的内容       let canvas = document.createElement("canvas");       let context = canvas.getContext("2d");       context.font = "14px Microsoft YaHei";       if ((prop === '') && title) {//标题长内容少的,取标题的值,         columnContent = title       } else {// 获取该列中占位最宽的内容         let index = 0;         for (let i = 0; i < tableData.length; i++) {           const now_temp = tableData[i][prop] + '';           const max_temp = tableData[index][prop] + '';           const now_temp_w = context.measureText(now_temp).width           const max_temp_w = context.measureText(max_temp).width           if (now_temp_w > max_temp_w) {             index = i;           }         }         columnContent = tableData[index][prop]         //比较占位最宽的值跟标题、标题为空的留出四个位置         const column_w = context.measureText(columnContent).width         const title_w = context.measureText(title).width         if (column_w < title_w) {           columnContent = title || '留四个字'         }       }       // 计算最宽内容的列宽       let width = context.measureText(columnContent);       flexWidth = width.width + 40 + num       return flexWidth + 'px';     }   } } </script> <style lang="scss" scoped> .active {   color: #50308d;   &:hover {     text-decoration: underline;     cursor: pointer;   } } </style>

标签:vue,return,表格,title,element,let,prop,tableData,row
From: https://www.cnblogs.com/xiaofang234/p/16769714.html

相关文章

  • vue实现复制功能
    html:<el-buttontype="primary"size="small"plainclass="ml30"@click="_copy(details.id)">复制店铺ID</el-button>js:_copy(context){//创建输入框元......
  • 用vue脚手架创建项目的方法
    首先打开一个文件夹打开cmd输入vuecreate*****项目名称然后会看到如下,选择手动选择特性,因为需要自己配置接下来到这个界面,选择Babel和Css预处理器按回车进......
  • vue.js3:用el-loading显示加载动画([email protected] / [email protected])
    一,el-loading1,文档地址:https://element-plus.gitee.io/zh-CN/component/loading.html2, 查看element-plus的版本:liuhongdi@lhdpc:/data/vue/imgtouch$npmlist......
  • vue路由加载页面
    当vue路由切换时,有时候会出现短暂白屏,需要添加一个加载状态参考:buildadmin地址:https://demo.buildadmin.com/#/利用vue的路由导航守卫:beforeEach、afterEach来判断显示......
  • 【Vue3.x】pinia
    PiniaVue3中使用Pinia替代vuex更改如下:支持ts体积小,压缩后1KB去除mutations,只有state,getters,actions;去除mutations后,actions直接进行同步和异步操作修改数据去......
  • vue-2 模板语法
    router/index.js//1、引入基础依赖importVuefrom'vue'importRouterfrom'vue-router'//2、引入要路由的页面importSmartyfrom"../components/smarty"//3、......
  • vue的computed计算属性的执行机制
    vue中初始化computed,每一个计算属性的本质就是watcher,创建计算属性的watcher的时候,会传入一个懒惰属性,来控制computed缓存,默认是执行的,先处理为vm._computedWatchers对象......
  • vuecli build 代码拆解
    splitChunks:{//表示选择哪些chunks进行分割,可选值有:async,initial和allchunks:"async",//表示新分离出的chunk必须大于等于minSize,默认为30000,约3......
  • 表格排重 | EXCEL表格数据排重,使用相同颜色标记相同值
    前言日常工作处理EXCEL表格数据,通常需要快速查找出表格中重复的数据项,并标注颜色,通过颜色区分数据。此外,使用小O地图EXCEL插件进行地图可视化时,需要将相同数值的数据设置......
  • vuex中commit
    一、不使用模块的基础模式看vuex相关的文件夹,放在src下的store文件夹,里面有一个index.js文件,为vuex的入口,如果不使用模块,可以将所有相关代码写在index.js文件里面,下面是最......