antd组件之table行自定义样式怎样才能生效

375 阅读2分钟

一、自定义表格复选框

设置复选框大小、边框、选中状态背景颜色、半选择样式等

设置大小后要注意调整全选中和半选中的样式

// table中设置样式
.ant-checkbox-inner {
    width: 28px;
    height: 28px;
    border: 1px solid #819bb3;
    border-radius: 6px;
  }
  .ant-checkbox-wrapper-checked .ant-checkbox-inner::after {
    width: 15px;
    left: 30%;
    margin-left: -4px;
  }
  
  .ant-checkbox-indeterminate .ant-checkbox-inner::after {
    width: 20px;
    height: 20px;
  }

// 复选框中设置
.ant-checkbox,
.ant-checkbox-wrapper {
  &:hover {
    .ant-checkbox-inner {
      @include border_color_theme();
      // border-radius: 6px;
    }
  }

  &.ant-checkbox-indeterminate .ant-checkbox-inner::after {
    // @include background_color_theme(1);
    background: #0268fd;
  }

  &.ant-checkbox-checked {
    &::after {
      @include border_color_theme();
      // border-radius: 6px;
    }

    .ant-checkbox-inner {
      // @include background_color_theme(1);
      background: #0268fd;
      // border-radius: 6px;
      @include border_color_theme();
      &::after {
        left: 50%;
        margin-left: -4px;
      }
    }
  }
}

二、给表格行设置边框和圆角

首先要给ant-table设置行类名rowClassName,需要注意的是rowClassName期望接收一个函数作为其值,不能直接设置:rowClassName=“xxx”

会报错:vue.esm.js:5126 [Vue warn]: Invalid prop: type check failed for prop "rowClassName". Expected Function, got String with value "table-row".

<a-table
      rowKey="devId"
      :rowClassName="() => 'table-row'"
      :loading="loading"
      :columns="columns"
      :components="components"
      :scroll="{ y: 500 }"
      :row-selection="rowSelection"
      :data-source="deviceListData"
      :pagination="pagination"
      @change="onTableChange"
    ></a-table>

然后给每行设置边框,背景色和阴影

.ant-table-tbody {
    tr > td {
      line-height: 50px;
      color: #2e2e2e !important;
    }
    .ant-table-row,
    .ant-table-row-selected {
      background: linear-gradient(180deg, #ffffff, #e8f0f7) !important;
      td {
        background: linear-gradient(180deg, #ffffff, #e8f0f7) !important;
        border-top: 1px solid #bcccda !important;
        border-bottom: 1px solid #bcccda !important;
      }
    }
    .ant-table-row {
      &:hover:not(.ant-table-expanded-row):not(.ant-table-row-selected) {
        box-shadow: none !important;
        td {
          background: rgb(239, 245, 255) !important;
        }
      }
      &.ant-table-row-selected {
        box-shadow: none !important;
        td {
          background: linear-gradient(180deg, #ffffff, #e8f0f7) !important;
        }
      }
    }
  }

最后,需要对表格行的边框和圆角做特殊处理,保证能显示圆角并且圆角部分也有边框

.table-row td:first-child {
    border-top-left-radius: 35px;
    border-left: 1px solid #bcccda;
    border-bottom-left-radius: 35px;
  }
  
  .table-row td:last-child {
    border-top-right-radius: 35px;
    border-right: 1px solid #bcccda;
    border-bottom-right-radius: 35px;
  }

三、自定义表头

表头高度做了处理后,需要额外处理一下有排序的列名样式

.ant-table-thead > tr > th {
    line-height: 50px;
    color: #2f2f2f !important;
    background: #fff !important;
    font-size: 20px;
    font-weight: bold;
    .ant-table-column-sorters {
      display: flex !important;
      align-items: center;
      .ant-table-column-sorter {
        margin-top: 6px;
      }
    }
  }

四、设置表格操作列

自定义操作列的按钮样式

.table_action {
    display: flex;
    .action-item {
      display: flex;
      align-items: center;
      @include font_theme();
      margin-right: 20px;
      &:last-child {
        margin-right: 0;
      }
      .iconfont {
        font-size: 16px;
        line-height: 16px;
        vertical-align: middle;
        margin-right: 3px;
      }
      .action-img {
        width: 17px;
        height: 18px;
        margin-right: 3px;
      }
      &.gray {
        color: $gray !important;
      }
      &.red {
        color: $red !important;
      }
      &.orange {
        color: $orange !important;
      }
      &.green {
        color: $green !important;
      }
      &.black {
        color: $black !important;
      }
    }
  }

效果图:

image.png