使用vuex模仿el-table

ops/2024/9/24 3:50:25/

1、vuex
在main.js引入
在这里插入图片描述

import Vue from 'vue';
import Vuex from 'vuex';Vue.use(Vuex);const store = new Vuex.Store({state: {// 定义要传递的数据datas: []},mutations: {// 定义修改数据的 mutationSET_DATAS(state, newDatas) {state.datas = newDatas;}},actions: {// 定义分发数据的 actionupdateDatas({ commit }, datas) {commit('SET_DATAS', datas);}}
});export default store;

2、index.vue
在这里插入图片描述


<template><div><div><div @click="dades">加100条</div><wb-table style="width: 99%;margin-left: 2px;" :parentValue='datas' :slotPropsValue='parentValue' heights="50"><dade label="选择" width="200"></dade><dade label="项目" width="200"></dade><dade label="项目2" width="200"></dade><dade label="项目3" width="200" v-slot="{item}">{{item.id}}</dade><dade label="项目4"></dade></wb-table><wb-table style="width: 99%;margin-left: 2px;" :parentValue='datasc' :slotPropsValue='parentValue' heights="50"><dade label="选择" width="200"></dade><dade label="项目66" width="200"></dade><dade label="项目2" width="200"></dade><dade label="项目3" width="200" v-slot="{item}">{{item.id}}</dade><dade label="项目4"></dade></wb-table></div></div>
</template><script>export default {components: {},data() {return {isDragging: false,dadeType:false,dadeType2:false,dadeType3:false,startX: 0,startY: 0,currentX: 0,currentY: 0,dragSpeed: 5, /* 调整拖动速度 */width:"",height:"",datas:[{"id":11},{"id":1266}],datasc:[{"id":1199},{"id":369}]};},watch: {},//界面没出来前加载created() {},mounted() {},computed: {},methods: {//点击体dades(){console.log(this.$store.state.datas)let list = {"id":1}for(let i=1;i<1;i++){this.datas.push(list)}},}
};
</script><style scoped>.dade{-webkit-box-shadow: 0 2px 0px 0 rgba(0,0,0,.1);box-shadow: 0 2px 12px 0 rgba(0,0,0,.1);}.draggable-div {position: absolute;}
</style>

3、wb-table
在这里插入图片描述

<template><div class="dadegdt" :style="{ 'height': heights + 'vh' }"><table cellspacing="0"><thead><tr style="background-color: rgb(245, 247, 250);"><td v-for="(it, index) in tableTd" :key="index" v-if="it.name == '选择'" :style="{ 'width': it.width + 'px' }"><el-checkbox v-model="checkboxs" @change="checkdoxs(checkboxs)"></el-checkbox></td><td v-for="(it, index) in tableTd" :key="index" v-if="it.name != '选择'" :style="{ 'width': it.width + 'px' }">{{ it.name }}</td></tr></thead><tbody :class="['dadeInput',bottons]"><tr v-for="(it,index) in $store.state.datas[uid]" :key="index" :id="index" :uid="uid"><slot></slot></tr><!-- 使用底部合计传bottons="bottons,<wb-table bottons="bottons"> --><tr v-if="bottons == 'bottons'" style="background-color: white"><td v-for="(it, index) in tableTd" :key="index" :style="{ 'width': it.width + 'px' }"><span v-if="it.key">{{it.sum}}</span><span v-if="it.name == '操作'">合计</span></td></tr></tbody></table></div>
</template>
<script>
export default {components: {},props: {bottons:'',parentValue: [],dadeindex: 0,fuCheckdoxs: {},datas: [],heights:"",billkindFn: {},},watch: {// 更新选择parentValue: {handler(newValue, oldValue) {if(this.uid == undefined || this.uid == ""){this.uids();}this.$store.state.datas[this.uid] = newValue;},immediate: true, // 如果希望在组件创建时立即触发一次监听函数,可以设置为 true},billkindFn(newValue) {if (newValue) {this.processSlotsData()// 获得高度let defaults = this.$slots.default[0];this.heights = defaults.data.attrs.heights}}},data() {return {checkboxs: false,name: '',dade: [{ "id": 1 }],tableTd: [],heights: "",widths: "",uid:"",};},created() {},mounted() {// 处理头部导航this.processSlotsData()},methods: {uids(){// 生成唯一const timestamp = new Date().getTime();const randomPart = Math.floor(Math.random() * 1000000);this.uid = timestamp+""+randomPart;console.log(this.uid)},clickdade(index) {this.dadeindex = index;},//父组件调用更新选中getUser(e) {this.checkboxs = e;},processSlotsData() {if (this.$slots.default) {this.tableTd = [];let data = this.$slots.default;data.forEach(req => {// 一级标签if (req.data && req.componentOptions.tag === "dade") {let key = "";if(req.data.attrs.sum != undefined){key = req.data.attrs.sum}this.tableTd.push({ "name": req.data.attrs.label, 'width': req.data.attrs.width, 'key': key, sum:0});}});}}}
};
</script>
</script><style scoped>
/* 最后一行浮动 */
.bottons tr:last-child {position: sticky;bottom: 0px;background-color: white;z-index: 200;transition: height 0.3s ease;
}
/* 固定头部 */
thead {position: sticky;top: 0px;background-color: white;z-index: 200;transition: height 0.3s ease;
}table,
tbody {border: 0;line-height: 30px;border-spacing: 0;border-collapse: collapse;cursor: default;
}/* 滚动条 */
tbody {display: block;/* overflow-y: scroll; */
}table thead,
tbody tr {display: table;width: 99.9%;table-layout: fixed;
}table {text-align: center;color: #000;
}td {padding-left: 2px;
}tr,
td {border-top: 1px solid #ebefee;border-left: 1px solid #ebefee;border-right: 1px solid #ebefee;
}/* 给最后一行加样式 */
table tr:last-child td {border-bottom: 1px solid #ebefee;
}/* tr{overflow-y: scroll;} */
/* tbody{overflow-y: scroll;} */
.dadegdt {width: 100%;overflow: auto;/* overflow-x:auto; *//* overflow-x: scroll; */
}.td_width {width: 50px;
}.td_width2 {width: 60px;
}tbody td {max-width: 1500px;min-width: 60px;word-wrap:break-word;text-overflow: ellipsis;white-space: nowrap;
}tbody td:hover {white-space: normal;/* overflow:auto; */padding-left: 0px;
}.dade {/* 初始状态的样式background-color: white;color: black;/* 添加过渡效果,这里设置为 0.3 秒内平滑过渡 */transition: all 0.1s ease;
}.dade:hover {background-color: #dcf4fa;
}.dade[data-v-a83bd3b0] {-webkit-box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);box-shadow: 0 0 0 0 rgba(0, 0, 0, 0);
}.dadeInput>>>.el-input__inner {text-align: center;
}
</style>

4、dade.vue

在这里插入图片描述

<template><td ref="myTd">{{dataFromStore}}<slot :item="dataFromStore"></slot></td>
</template><script>export default {computed: {},data() {return {dataFromStore: []};},mounted() {// 获取 td 元素// var td = document.getElementById('targetTd');const td = this.$refs.myTd;// 获取 td 的父元素 trvar tr = td.parentElement;console.log(tr.getAttribute("uid"))console.log(6666)this.dataFromStore = this.$store.state.datas[tr.getAttribute("uid")][tr.id]// console.log(this.dataFromStore)}}
</script><style>
</style>

成果
在这里插入图片描述


http://www.ppmy.cn/ops/110714.html

相关文章

# 键盘字母上有下标数字,输入时怎么一键去掉,关闭键盘上的下标数字。‌

键盘字母上有下标数字&#xff0c;输入时怎么一键去掉&#xff0c;关闭键盘上的下标数字。‌ 一、问题描述&#xff1a; 如下图&#xff0c;有的笔记本电脑键盘上&#xff0c;没有数字小键盘&#xff0c;数字小键盘会和字母混和在一起&#xff0c;这样打字时&#xff0c;不容…

python中如何打印日志信息推荐logaid库(强大的日志库)

一、安装 pip install logaid二、使用 只打印&#xff0c;不保存 from logaid import loglog.info(hello world) log.error(hello world) log.warning(hello world) log.fatal(hello world,123,{},[],False)开启超级print from logaid import log log.init(print_proTrue)prin…

ASP.NET Core 入门教学二十三 模型绑定和验证

System.ComponentModel.DataAnnotations 命名空间提供了用于在 .NET 应用程序中进行数据验证和绑定的属性。在 ASP.NET Core 中&#xff0c;这些属性可以与模型绑定和模型验证一起使用&#xff0c;以确保用户输入的数据有效且符合预期的格式。 以下是如何使用 System.Componen…

Springboot工程配置https访问

背景 因为前端工程使用nginx配置了https访问&#xff0c;在https直接请求我们Springboot后端的http接口会报错。那么我们就需要配置使得我们后端的springboot服务支持https访问。 证书生成 在配置springboot工程https之前&#xff0c;我们需要生成自签名证书以及Spring Boot…

汇编:嵌入式软件架构学习资源

成为嵌入式软件架构设计师需要掌握多方面的知识&#xff0c;包括嵌入式系统、实时操作系统、硬件接口、软件设计模式等。 以下是一些推荐的博客和网站&#xff0c;可以帮助你深入学习嵌入式软件架构设计&#xff1a; ### 1. **Embedded.com** - **网址**: [Embedded.com](htt…

64. 求 1+2+…+n

comments: true difficulty: 中等 edit_url: https://github.com/doocs/leetcode/edit/main/lcof/%E9%9D%A2%E8%AF%95%E9%A2%9864.%20%E6%B1%821%2B2%2B%E2%80%A6%2Bn/README.md 面试题 64. 求 12…n 题目描述 求 12...n &#xff0c;要求不能使用乘除法、for、while、if、els…

HW行动指南,前辈教你如何真正靠护网赚到钱!

‍正文&#xff1a; HW行动&#xff0c;攻击方的专业性越来越高&#xff0c;ATT&CK攻击手段覆盖率也越来越高&#xff0c;这对于防守方提出了更高的要求&#xff0c;HW行动对甲方是一个双刃剑&#xff0c;既极大地推动了公司的信息安全重视度和投入力量&#xff0c;但同时…

【Obsidian】当笔记接入AI,Copilot插件推荐

当笔记接入AI&#xff0c;Copilot插件推荐 自己的知识库笔记如果增加AI功能会怎样&#xff1f;AI的回答完全基于你自己的知识库余料&#xff0c;是不是很有趣。在插件库中有Copilot插件这款插件&#xff0c;可以实现这个梦想。 一、什么是Copilot&#xff1f; 我们知道githu…