使用vuex模仿el-table

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

import Vue from 'vue';
import Vuex from 'vuex';

Vue.use(Vuex);

const store = new Vuex.Store({
    state: {
        // 定义要传递的数据
        datas: []
    },
    mutations: {
        // 定义修改数据的 mutation
        SET_DATAS(state, newDatas) {
          state.datas = newDatas;
        }
    },
    actions: {
        // 定义分发数据的 action
        updateDatas({ 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 的父元素 tr
        var 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>

成果
在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
为了实现多选功能,我们需要在数据源中添加一个属性来记录选中状态,然后在表格中添加一个复选框列,以及一个全选复选框。当用户点击复选框时,我们需要更新数据源中对应的选中状态。 下面是一个简单的示例代码: ```html <template> <div> <el-table :data="tableData" style="width: 100%"> <el-table-column type="selection" width="55"></el-table-column> <el-table-column prop="name" label="姓名"></el-table-column> <el-table-column prop="age" label="年龄"></el-table-column> <el-table-column prop="address" label="地址"></el-table-column> </el-table> </div> </template> <script> export default { data() { return { tableData: [ { name: '张三', age: 18, address: '北京', selected: false }, { name: '李四', age: 20, address: '上海', selected: false }, { name: '王五', age: 22, address: '广州', selected: false }, { name: '赵六', age: 24, address: '深圳', selected: false } ] } }, computed: { // 计算选中的行数 selectedCount() { return this.tableData.filter(row => row.selected).length }, // 是否全选 isAllSelected() { return this.selectedCount === this.tableData.length } }, methods: { // 全选/取消全选 toggleSelectAll(checked) { this.tableData.forEach(row => { row.selected = checked }) }, // 切换选中状态 toggleSelect(row) { row.selected = !row.selected } } } </script> ``` 在这个示例中,我们添加了一个`selected`属性来记录每一行的选中状态。`computed`中的`selectedCount`计算选中的行数,`isAllSelected`判断是否全选。 在模板中,我们在表格中添加了一个类型为`selection`的列,用来显示复选框。同时,我们添加了一个全选复选框,绑定`toggleSelectAll`方法。当用户点击复选框时,我们调用`toggleSelect`方法来更新数据源中对应的选中状态。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

大得369

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值