vue+el-table点击表头实现改变其当前样式

 更新时间:2024年08月06日 09:23:23   作者:昨夜太平长安888  
这篇文章主要介绍了vue+el-table点击表头实现改变其当前样式问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教

vue el-table点击表头改变其当前样式

废话不多说,先看效果

网上找了一大圈没有符合的,只能自己看着搞:

直接贴代码

 <el-table
        ref="table"
        :data="tableData"
        border
        stripe
        @sort-change="changeColumn"
      >
        <el-table-column label="排名" prop="userRank" align="center" fixed />
        <el-table-column label="员工" prop="userName" align="center" fixed />
        <el-table-column label="合计" prop="score" align="center">
          <template slot="header">
            <span @click="sortClick(0, 2 ,'0')">合计</span>
          </template>
        </el-table-column>
        <template>
          <el-table-column
            v-for="(item, index) in headData"
            :key="item.id"
            :label="item.name"
            align="center"
            :prop="String(item.id)"
          >
            <template slot="header">
              <span @click="sortClick(item.id, index ,'1')">{{ item.name }}</span>
            </template>
            <template slot-scope="scope">
              <span>
                <div
                  v-for="i in scope.row.items"
                  @click="detailAdopt(scope.row)"
                  style="cursor: pointer"
                  :key="i.id"
                >
                  <div v-if="i.parentCategoryId === item.id">
                    <div>
                      {{ i.score }}分<span v-show="numShow"
                        >({{ i.count }}个)</span
                      >
                    </div>
                  </div>
                </div>
              </span>
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" prop="name" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              @click="seeProportion(scope.row)"
              type="text"
              size="small"
            >
              个人占比
            </el-button>
          </template>
        </el-table-column>
      </el-table>
<script>
	export default {
	  data() {
	    return {
	      tableData: [
	        // 表格数据...
	      ],
	      prevIndex: -1 // 用于保存上一次点击的表头索引
	    };
	  },
	  methods: {
	    sortClick(id, index , type) {
	      if(type === '1') {
	        index = index + 3;
	      } else {
	        index = index + 0;
	      }
	      // 通过ref获取表头元素
	      const tableHeader =
	        this.$refs.table.$el.getElementsByClassName("el-table__header")[0];
	      // 恢复上一次点击表头的字体颜色为默认
	      if (this.prevIndex !== -1) {
	        const prevHeader =
	          tableHeader.getElementsByTagName("th")[this.prevIndex];
	        prevHeader.style.color = ""; // 恢复默认颜色(空字符串)
	      }
	      // 修改当前点击表头的字体颜色
	      const targetHeader = tableHeader.getElementsByTagName("th")[index];
	      targetHeader.style.color = "#409eff"; // 修改为你想要的颜色
	      // 更新prevIndex为当前点击的表头索引
	      this.prevIndex = index;
	
	      this.form.parentCategoryId = id;
	      this.list();
	    },
	  }
	};
</script>

这种写法经使用过程中发现问题

故修改为以下:

<el-table ref="table" v-loading="loading" :data="tableData" border stripe>
        <el-table-column label="排名" prop="userRank" align="center" fixed />
        <el-table-column label="员工" prop="userName" align="center" fixed />
        <el-table-column label="合计" prop="score" align="center" fixed>
          <template slot="header">
            <span
              @click="sortClick({ id: 0, name: '合计' })"
              :class="titClick ? 'fontColor' : ''"
              >合计</span
            >
          </template>
        </el-table-column>
        <template>
          <el-table-column
            v-for="item in headData"
            :key="item.id"
            :label="item.name"
            align="center"
            :prop="String(item.id)"
          >
            <template slot="header">
              <span @click="sortClick(item)">{{ item.name }}</span>
            </template>
            <template slot-scope="scope">
              <span>
                <div v-if="scope.row.items.length">
                  <div
                    v-for="i in scope.row.items"
                    style="cursor: pointer"
                    :key="i.id"
                  >
                    <div v-if="i.parentCategoryId === item.id">
                      <div @click="detailAdopt(i)">
                        {{ i.score }}分
                        <p v-show="numShow">({{ i.count }}个)</p>
                      </div>
                    </div>
                  </div>
                </div>
                <!-- <div v-else>0</div> -->
              </span>
            </template>
          </el-table-column>
        </template>
        <el-table-column label="操作" prop="name" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button
              @click="seeProportion(scope.row)"
              type="text"
              size="small"
              v-if="scope.row.score !== '0.00'"
            >
              个人占比
            </el-button>
          </template>
        </el-table-column>
      </el-table>
<script>
	export default {
	  data() {
	    return {
	      tableData: [
	        // 表格数据...
	      ],
	      prevIndex: -1 // 用于保存上一次点击的表头索引
	    };
	  },
	  methods: {
	        sortClick(item) {
		      const tableHeader =
		        this.$refs.table.$el.getElementsByClassName("el-table__header")[0];
		      if (this.prevIndex !== -1) {
		        const prevHeader =
		          tableHeader.getElementsByTagName("th")[this.prevIndex];
		        prevHeader.style.color = "";
		      }
		      const targetHeader = tableHeader.getElementsByTagName("th");
		      if (item.id === 0) {  //但是这个样式不生效,还不知道为啥
		        this.titClick = true;
		      } else {
		        this.titClick = false;
		        for (let i = 0; i < targetHeader.length; i++) {
		          if (targetHeader[i].innerText === item.name) { //这里做的是名称的判断
		            targetHeader[i].style.color = "#409EFF";
		            this.prevIndex = i;
		          }
		        }
		      }
		      this.form.parentCategoryId = item.id;
		      this.list();
		    },
	  }
	};
</script>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

相关文章

  • 在Vue3中使用localStorage保存数据的流程步骤

    在Vue3中使用localStorage保存数据的流程步骤

    在前端开发中,尤其是利用Vue3构建现代Web应用时,掌握如何使用本地存储(localStorage)来保存数据是非常重要的能力,在这篇博客中,我将详细介绍如何在Vue3中使用localStorage保存数据,并提供示例代码来帮助理解,需要的朋友可以参考下
    2024-06-06
  • vue中this.$router.push()路由传值和获取的两种常见方法汇总

    vue中this.$router.push()路由传值和获取的两种常见方法汇总

    这篇文章主要介绍了vue中this.$router.push()路由传值和获取的两种常见方法,本文结合示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-12-12
  • vue3 使用provide inject父子组件传值失败且子组件不响应

    vue3 使用provide inject父子组件传值失败且子组件不响应

    这篇文章主要介绍了vue3使用provide inject父子组件传值传不过去且传递后子组件不具备响应性问题解决方法,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪
    2023-08-08
  • 总结Vue Element UI使用中遇到的问题

    总结Vue Element UI使用中遇到的问题

    这篇文章主要介绍了Vue Element UI使用中遇到的问题,对ElementUI感兴趣的同学,可以参考下
    2021-05-05
  • vue filters和directives访问this的问题详解

    vue filters和directives访问this的问题详解

    这篇文章主要介绍了vue filters和directives访问this的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • 详解vue-cli3多页应用改造

    详解vue-cli3多页应用改造

    这篇文章主要介绍了详解vue-cli3多页应用改造,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2019-06-06
  • 如何修改Vue打包后文件的接口地址配置的方法

    如何修改Vue打包后文件的接口地址配置的方法

    这篇文章主要介绍了如何修改Vue打包后文件的接口地址配置的方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2020-04-04
  • Vue Router4如何正确重置路由的实例详解

    Vue Router4如何正确重置路由的实例详解

    在使用 Vue 3 和 Vue Router 4 开发中大型 SPA 应用时,我们经常会遇到需要动态添加或删除路由的场景,下面小编来和大家讲讲Vue Router4如何正确重置路由吧
    2025-05-05
  • vue 实现全选全不选的示例代码

    vue 实现全选全不选的示例代码

    本篇文章主要介绍了vue 实现全选全不选的示例代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-03-03
  • 浅析从vue源码看观察者模式

    浅析从vue源码看观察者模式

    本篇文章主要介绍了vue源码看观察者模式,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-01-01

最新评论