Vue项目中使用element-plus的el-table组件-组件使用-样式修改

336 篇文章 80 订阅
本文介绍了在Vue项目中如何使用element-plus的el-table组件,并详细讲解了如何进行样式重置和单元格背景填充的修改,通过代码示例展示了具体的实现过程。
摘要由CSDN通过智能技术生成

项目配置

<div class="table-wrap">
      <el-table
        class="table-card-container"
        :data="tableData"
        style="width: 100%"
      >
        <template v-for="column in tableColumn">
          <el-table-column
            v-if="column.isShow"
            :key="column.prop"
            :prop="column.prop"
            :label="column.label"
            :fixed="column.fixed"
            :width="column.width"
            :align="column.align"
          >
            <!-- :label="column.label" -->
            <!-- <template #header></template> -->
            <template #default="scope">
              <!-- <div v-if="column.label == '币种'" class="bz-icon">
                <img :src="getIcon(scope.row.bz)" />
                {{ scope.row.bz }}
              </div>
              <div v-if="column.label == '价格(24h%)'">
                <span
                  :class="{
                    'long-color': +scope.row.jg24 >= 0,
                    'short-color': +scope.row.jg24 < 0
                  }"
                >
                  {{ formatValue(scope.row.jg24, 'zfb') }}
                </span>
              </div> -->
            </template>
          </el-table-column>
        </template>
      </el-table>
    </div>
const tableColumn = ref([
  {
    label: '排名',
    prop: 'pm',
    fixed: false,
    width: 80,
    isShow: true,
    align: 'center'
  },
  {
    label: '平台',
    prop: 'pt',
    fixed: false,
    width: 120,
    isShow: true,
    align: 'left'
  },
  {
    label: '持仓量',
    prop: 'ccl',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'left'
  },
  {
    label: '持仓额',
    prop: 'cce',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'left'
  },
  {
    label: '占比',
    prop: 'zb',
    fixed: false,
    width: 120,
    isShow: true,
    align: 'left'
  },
  {
    label: '持仓变化(1小时)',
    prop: 'ccbh1',
    fixed: false,
    width: 160,
    isShow: true,
    align: 'left'
  },
  {
    label: '持仓变化(4小时)',
    prop: 'ccbh4',
    fixed: false,
    width: 160,
    isShow: true,
    align: 'left'
  },
  {
    label: '持仓变化(24小时)',
    prop: 'ccbh24',
    fixed: false,
    width: 160,
    isShow: true,
    align: 'left'
  },
  {
    label: '持仓/24小时成交量',
    prop: 'cc24cjl',
    fixed: false,
    width: 160,
    isShow: true,
    align: 'right'
  }
]);
const tableData = [
  {
    pt: 'BTC',
    pm: '1',
    ccl: '123.32BTC',
    cce: '$131.32亿',
    zb: '1',
    ccbh1: '0.0032',
    ccbh4: '0.002',
    ccbh24: '0.0649',
    cc24cjl: '0.243243'
  }
];

样式重置修改文件

.table-card-container {
    @gray-bg: #080514;
    // --el-table-border: none;
    --el-table-border-color:#252525;
    --el-table-row-hover-bg-color:@gray-bg;
--el-fill-color-blank: @gray-bg;
    :deep(.el-table__header) {
      .el-table__cell {
        padding: 0;
        height: 50px;
        background: @gray-bg;
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 400;
        color: #868E9B;
  
        // &.ascending {
        //   .caret-wrapper {
        //     .sort-caret.ascending {
        //       border-bottom-color: #0ECB81;
        //     }
        //   }
        // }
  
        // &.descending {
        //   .caret-wrapper {
        //     .sort-caret.descending {
        //       border-top-color: #0ECB81;
        //     }
        //   }
        // }
      }
  
    }
  
    :deep(.el-table__body) {

    //   .el-table__row:nth-child(2n) {
    //     background: @gray-bg;
    //   }
  
      // .el-table__body{
      //   background: @gray-bg;
      // }
      
      .el-table__row {
        padding: 0;
        height: 60px;
        font-family: DIN,DIN-Medium, arial, sans-serif;
        font-size: 14px;
        color: #FFFFFF;
        --el-table-tr-bg-color: @gray-bg;
        .el-table__cell{
          --el-bg-color: @gray-bg;
        }
      }
      // .hover-row{
      //   --el-table-row-hover-bg-color:@gray-bg;
      // }
      
    }
    :deep(.el-table__body-wrapper) {
      background-color: @gray-bg;
    }
    :deep(.el-pagination) {
      --el-pagination-bg-color: #fff;
      --el-pagination-button-disabled-bg-color: transparent;
      --el-pagination-button-disabled-color: #868E9B;
      --el-pagination-hover-color: #0ECB81;
      --el-pagination-button-color: #868E9B;
      --el-pagination-font-size: 14px;
      font-family: DIN-Medium, arial, sans-serif;
      margin-top: 50px;
      justify-content: center;
  
      .btn-prev .el-icon,
      .btn-next .el-icon {
        font-size: 15px;
      }
      .el-pager {
        .number {
          margin: 0 8px
        }
        .is-active {
          background: #0ECB81;
          color: #fff;
        }
      }
    }
  }

单元格背景填充 

代码

<template>
  <div class="TableList">
    <div class="title">
      <h1>数字货币合约爆仓图表</h1>
      <div class="title-right">
        <el-input class="search-input" v-model="searchText" placeholder="搜索">
          <template #prefix>
            <img src="@/assets/images/prices/search.svg" />
          </template>
        </el-input>
      </div>
    </div>
    <div class="table-wrap">
      <el-table
        class="table-card-container"
        :data="tableData"
        style="width: 100%"
      >
        <template v-for="column in tableColumn">
          <el-table-column
            v-if="column.isShow"
            :key="column.prop"
            :prop="column.prop"
            :label="column.label"
            :fixed="column.fixed"
            :width="column.width"
            :align="column.align"
          >
            <!-- :label="column.label" -->
            <!-- <template #header></template> -->
            <template #default="scope">
              <div v-if="column.label == '排名'" class="label-icon">
                <span>{{ scope.$index + 1 }}</span>
              </div>
              <div v-if="column.label == '币种'" class="label-icon">
                <img :src="getIcon(scope.row.bz)" />
                <span>{{ scope.row.bz }}</span>
              </div>
              <div v-if="column.label == '价格(24h%)'">
                <span
                  :class="{
                    'long-color': +scope.row.jg24 >= 0,
                    'short-color': +scope.row.jg24 < 0
                  }"
                  >{{ formatValue(scope.row.jg24, 'zfb') }}</span
                >
              </div>
              <div
                v-if="column.label == '1小时多单爆仓'"
                class="label-lty"
                :class="{
                  'long-bg-color': +scope.row.ddbc1s == 0,
                  'short-bg-color': +scope.row.ddbc1s == 1
                }"
              >
                <span>
                  {{ scope.row.ddbc1 }}
                </span>
              </div>
              <div
                v-if="column.label == '1小时空单爆仓'"
                class="label-lty"
                :class="{
                  'long-bg-color': +scope.row.kdbc1s == 0,
                  'short-bg-color': +scope.row.kdbc1s == 1
                }"
              >
                <span>
                  {{ scope.row.kdbc1 }}
                </span>
              </div>
              <div
                v-if="column.label == '4小时多单爆仓'"
                class="label-lty"
                :class="{
                  'long-bg-color': +scope.row.ddbc4s == 0,
                  'short-bg-color': +scope.row.ddbc4s == 1
                }"
              >
                <span>
                  {{ scope.row.ddbc4 }}
                </span>
              </div>
              <div
                v-if="column.label == '4小时空单爆仓'"
                class="label-lty"
                :class="{
                  'long-bg-color': +scope.row.kdbc4s == 0,
                  'short-bg-color': +scope.row.kdbc4s == 1
                }"
              >
                <span>
                  {{ scope.row.kdbc4 }}
                </span>
              </div>
              <div
                v-if="column.label == '24小时多单爆仓'"
                class="label-lty"
                :class="{
                  'long-bg-color': +scope.row.ddbc24s == 0,
                  'short-bg-color': +scope.row.ddbc24s == 1
                }"
              >
                <span>
                  {{ scope.row.ddbc24 }}
                </span>
              </div>
              <div
                v-if="column.label == '24小时空单爆仓'"
                class="label-lty"
                :class="{
                  'long-bg-color': +scope.row.kdbc24s == 0,
                  'short-bg-color': +scope.row.kdbc24s == 1
                }"
              >
                <span>
                  {{ scope.row.kdbc24 }}
                </span>
              </div>
            </template>
          </el-table-column>
        </template>
      </el-table>
      <div class="pagination">
        <ElPagination
          v-model:current-page="page"
          :page-size="pageSize"
          :total="allTotalCount"
          layout="prev, pager, next"
          hide-on-single-page
          @current-change="onCurrentChange"
        />
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue';
import {
  getIcon,
  getExchangeIcon,
  formatValue
} from '@/views/Prices/Hooks/useUtils.js';
const searchText = ref('');
const allTotalCount = ref(100);
const pageSize = ref(10);
const page = ref(1);
const onCurrentChange = (pageIdx) => {
  page.value = pageIdx;
};
const tableColumn = ref([
  {
    label: '排名',
    prop: 'pm',
    fixed: false,
    width: 70,
    isShow: true,
    align: 'left'
  },
  {
    label: '币种',
    prop: 'bz',
    fixed: false,
    width: 128,
    isShow: true,
    align: 'center'
  },
  {
    label: '价格',
    prop: 'jg',
    fixed: false,
    width: 120,
    isShow: true,
    align: 'left'
  },
  {
    label: '价格(24h%)',
    prop: 'jg24',
    fixed: false,
    width: 120,
    isShow: true,
    align: 'center'
  },
  {
    label: '1小时多单爆仓',
    prop: 'ddbc1',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'center'
  },
  {
    label: '1小时空单爆仓',
    prop: 'kdbc1',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'center'
  },
  {
    label: '4小时多单爆仓',
    prop: 'ddbc4',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'center'
  },
  {
    label: '4小时空单爆仓',
    prop: 'kdbc4',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'center'
  },
  {
    label: '24小时多单爆仓',
    prop: 'ddbc24',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'center'
  },
  {
    label: '24小时空单爆仓',
    prop: 'kdbc24',
    fixed: false,
    width: 130,
    isShow: true,
    align: 'center'
  }
]);
const tableData = [
  {
    bz: 'ddd',
    jg: '$2342.1',
    jg24: '0.0023',
    ddbc1: '$2342.1万',
    ddbc1s: '1',
    kdbc1: '$2342.1万',
    kdbc1s: '0',
    ddbc4: '$2342.1万',
    ddbc4s: '0',
    kdbc4: '$2342.1万',
    kdbc4s: '1',
    ddbc24: '$2342.1万',
    ddbc24s: '1',
    kdbc24: '$2342.1万',
    kdbc24s: '1'
  },
  {
    bz: 'ddd',
    jg: '$2342.1',
    jg24: '0.0023',
    ddbc1: '$2342.1万',
    ddbc1s: '0',
    kdbc1: '$2342.1万',
    kdbc1s: '1',
    ddbc4: '$2342.1万',
    ddbc4s: '0',
    kdbc4: '$2342.1万',
    kdbc4s: '0',
    ddbc24: '$2342.1万',
    ddbc24s: '1',
    kdbc24: '$2342.1万',
    kdbc24s: '0'
  }
];
</script>

<style lang="less" scoped>
@import '../tableStyle2';
.TableList {
  .title {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 50px;
    h1 {
      color: #fff;
      font-family: PingFang SC;
      font-size: 24px;
      font-weight: 600;
    }
    .title-right {
      display: flex;
      .search-input {
        width: 225px;
        height: 40px;
        border-radius: 4px;
        border: 1px solid #404040;
        // margin-right: 15px;
        background: #121212;
        :deep(.el-input__inner) {
          color: #fff;
        }
      }
    }
  }
  .table-wrap {
    .table-card-container {
      :deep(.el-table__header) {
        .el-table__cell {
          .cell {
            padding: 0 4px;
          }
        }
      }
      :deep(.el-table__body) {
        .el-table__cell {
          .cell {
            padding: 0 4px;
          }
        }
      }
    }
    .label-icon {
      color: #fff;
      font-family: DIN;
      font-size: 14px;
      img {
        width: 16px;
        height: 16px;
        margin-right: 5px;
        vertical-align: text-top;
      }
    }
    .label-lty {
      height: 60px;
      line-height: 60px;
    }
    --table-bg-Short1: rgba(226, 98, 109, 0.2);
    --table-bg-Long1: rgba(14, 203, 129, 0.2);
    .long-bg-color {
      background: var(--table-bg-Long1);
    }
    .short-bg-color {
      background: var(--table-bg-Short1);
    }
    .pagination {
      padding-top: 30px;
      padding-bottom: 50px;
    }
  }
}
</style>

样式重置修改

.table-card-container {
  @gray-bg: transparent;//#18191e;
  // --el-table-border: none;
  --el-table-border-color: #252525;
  --el-table-row-hover-bg-color: @gray-bg;
  --el-fill-color-blank: @gray-bg;
    :deep(.el-table__header) {
      .el-table__cell {
        padding: 0;
        height: 50px;
        background: @gray-bg;
        font-family: PingFang SC;
        font-size: 14px;
        font-weight: 400;
        color: #868e9b;
  
        // &.ascending {
        //   .caret-wrapper {
        //     .sort-caret.ascending {
        //       border-bottom-color: #0ECB81;
        //     }
        //   }
        // }
  
        // &.descending {
        //   .caret-wrapper {
        //     .sort-caret.descending {
        //       border-top-color: #0ECB81;
        //     }
        //   }
        // }
      }
  
    }
  
    :deep(.el-table__body) {

    //   .el-table__row:nth-child(2n) {
    //     background: @gray-bg;
    //   }
  
      // .el-table__body{
      //   background: @gray-bg;
      // }
      
      .el-table__row {
        padding: 0;
        height: 60px;
        font-family: DIN,DIN-Medium, arial, sans-serif;
        font-size: 14px;
        color: #FFFFFF;
        --el-table-tr-bg-color: @gray-bg;
        .el-table__cell{
          padding: 0;
          --el-bg-color: @gray-bg;
        }
      }
      // .hover-row{
      //   --el-table-row-hover-bg-color:@gray-bg;
      // }
      
    }
    :deep(.el-table__body-wrapper) {
      background-color: @gray-bg;
    }
    :deep(.el-pagination) {
      --el-pagination-bg-color: #fff;
      --el-pagination-button-disabled-bg-color: transparent;
      --el-pagination-button-disabled-color: #868E9B;
      --el-pagination-hover-color: #0ECB81;
      --el-pagination-button-color: #868E9B;
      --el-pagination-font-size: 14px;
      font-family: DIN-Medium, arial, sans-serif;
      margin-top: 50px;
      justify-content: center;
  
      .btn-prev .el-icon,
      .btn-next .el-icon {
        font-size: 15px;
      }
      .el-pager {
        .number {
          margin: 0 8px
        }
        .is-active {
          background: #0ECB81;
          color: #fff;
        }
      }
    }
  }

要实现Vue3 Element-Plus的el-table汇总行单元格合并,你可以使用`span-method`属性来定义一个函数,该函数可以返回每个单元格需要合并的行数和列数。下面是一个示例代码: ```html <el-table :data="tableData" border style="width: 100%"> <el-table-column prop="date" label="日期" width="180"> </el-table-column> <el-table-column prop="name" label="姓名" width="180"> </el-table-column> <el-table-column prop="address" label="地址"> </el-table-column> <el-table-column label="合计" :span-method="objectSpanMethod"> <template #default="{ rows, row, column, $index }"> <span v-if="$index === 0">{{ row.total }}</span> </template> </el-table-column> </el-table> ``` 在上面的代码,我们定义了一个`objectSpanMethod`方法,并将其赋值给`span-method`属性。这个方法接收四个参数:`{ rows, row, column, $index }`。其`rows`是当前列的所有行数据,`row`是当前行数据,`column`是当前列数据,`$index`是当前行的索引。 下面是`objectSpanMethod`方法的实现: ```js methods: { objectSpanMethod({ row, column, rowIndex, columnIndex }) { if (columnIndex === 3) { if (rowIndex === 0) { return { rowspan: this.tableData.length, colspan: 1 }; } else { return { rowspan: 0, colspan: 0 }; } } } } ``` 在上面的代码,我们检查当前列是否为第四列(即“合计”列),如果是,我们检查当前行是否为第一行。如果是,我们返回一个对象`{ rowspan: this.tableData.length, colspan: 1 }`,其`rowspan`表示当前单元格需要合并的行数,`colspan`表示当前单元格需要合并的列数。如果当前行不是第一行,则返回一个空对象`{ rowspan: 0, colspan: 0 }`,表示当前单元格不需要合并。 这样,我们就可以实现Vue3 Element-Plus的el-table汇总行单元格合并了。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

JackieDYH

谢谢您嘞!我会继续加油地

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

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

打赏作者

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

抵扣说明:

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

余额充值