Vue Ant Design Vue table customRow + rowClassName 设置行属性 + 表格行的类名(带斑马纹和默认选中第一条数据) - 附完整示例

目录

效果

一、介绍

1、官方文档

2、官方示例

二、准备工作

1、安装依赖包

2、示例版本

三、使用步骤

1、在main.ts引入

2、具体使用

四、完整示例

1、main.ts

2、example.vue

 欢迎扫描下方二维码关注VX公众号


效果

一、介绍

1、官方文档

Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.jsAn enterprise-class UI components based on Ant Design and Vueicon-default.png?t=N7T8https://3x.antdv.com/components/table-cn#API

2、官方示例

二、准备工作

1、安装依赖包

# 选择一个你喜欢的包管理器

# NPM
$npm install ant-design-vue --save

#Yarn
$ yarn add ant-design-vue

注:也可以在浏览器中使用 script 和 link 标签直接引入文件,并使用全局变量 antd

Ant Design Vue — An enterprise-class UI components based on Ant Design and Vue.js

2、示例版本

"ant-design-vue": "^3.2.12",

三、使用步骤

1、在main.ts引入

import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'

app.use(Antd);

2、具体使用

  // 设置行属性
  function rowProperties(record, index) {
    return {
      record,
      onClick: (event) => {
        clickRow(record);
      },
    };
  }

  function clickRow(record) {
    selectedRecord.value = record;
  }

  // 表格行的类名 保存的行和选中行如果一致,就返回该样式
  function setRowClassName(record, index) {
    return record.name === selectedRecord.value.name ? 'clicked-row' : (index % 2 === 1) ? 
    'table-striped' : null;
  }

四、完整示例

1、main.ts

import { createApp } from 'vue';
import App from './App.vue';
import Antd from 'ant-design-vue'
import 'ant-design-vue/dist/antd.css'

const app = createApp(App);
app.use(Antd);
app.mount("#app");

2、example.vue

<template>
  <a-table
    :columns="columns"
    :data-source="tableData"
    :customRow="rowProperties"
    :rowClassName="setRowClassName"
    bordered
    size="small"/>
</template>

<script lang="ts" setup>
  import { ref } from 'vue';

  const columns = [
    {
      title: '姓名',
      dataIndex: 'name',
      ellipsis: true,
    },
    {
      title: '身高',
      dataIndex: 'height', 
      ellipsis: true,
    },
    {
      title: '体重',
      dataIndex: 'weight',
      ellipsis: true,
    },
  ];
  const tableData = ref<any>([
    {
      key: '1',
      name: '张三',
      height: 180,
      weight: 165,
    },
    {
      key: '2',
      name: '李四',
      height: 178,
      weight: 146,
    },
    {
      key: '3',
      name: '王五',
      height: 182,
      weight: 168,
    },
    {
      key: '4',
      name: '赵六',
      height: 185,
      weight: 160,
    },
    {
      key: '5',
      name: '孙七',
      height: 176,
      weight: 140,
    },
    {
      key: '6',
      name: '周八',
      height: 183,
      weight: 176,
    },
    {
      key: '7',
      name: '吴九',
      height: 177,
      weight: 148,
    },
    {
      key: '8',
      name: '郑十',
      height: 180,
      weight: 150,
    },
  ]);

  const selectedRecord = ref({});

  // 设置行属性
  function rowProperties(record, index) {
    return {
      record,
      onClick: (event) => {
        clickRow(record);
      },
    };
  }

  function clickRow(record) {
    selectedRecord.value = record;
  }

  // 表格行的类名 保存的行和选中行如果一致,就返回该样式
  function setRowClassName(record, index) {
    return record.name === selectedRecord.value.name ? 'clicked-row' : (index % 2 === 1) ? 'table-striped' : null;
  }

  if (tableData.value.length > 0) clickRow(tableData.value[0]);

</script>

<style scoped>
  :deep() .ant-table-cell {
    text-align: center !important;
  }
  
  :deep() .ant-table-pagination.ant-pagination {
    margin: 10px 0 0 0 !important;
  }
  
  :deep() .clicked-row {
    background-color: #0e8f8f;
  }
  :deep() .table-striped {
    background-color: #fafafa !important;
  }
</style>

分页相关配置示例

Vue Ant Design Vue table pagination - 表格分页-CSDN博客

 欢迎扫描下方二维码关注VX公众号

  • 8
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Ant Design Vue设置中文字体,可以通过以下步骤实现: 1. 导入中文字体文件:首先,将中文字体文件(例如.ttf或.otf格式)下载并放置在项目的静态资源文件夹中,比如 `public/fonts/` 目录下。 2. 创建全局样式:在项目的 `src` 目录下创建一个新的全局样式文件,比如 `global.css`。 3. 设置字体样式:在 `global.css` 文件中,使用 `@font-face` 规则设置字体样式。例如: ```css @font-face { font-family: 'MyCustomFont'; src: url('/fonts/MyCustomFont.ttf') format('truetype'); } ``` 确保将 `url()` 函数中的路径指向中文字体文件的正确位置。 4. 引入全局样式:在项目的入口文件(通常是 `main.js` 或 `main.ts`)中引入全局样式文件。例如: ```javascript import './global.css'; ``` 5. 应用字体样式:在 Ant Design Vue 的配置文件(通常是 `vue.config.js`)中,使用 `css.loaderOptions` 配置项来向 webpack 注入自定义的样式。例如: ```javascript module.exports = { css: { loaderOptions: { less: { modifyVars: { 'font-family': 'MyCustomFont, Arial, sans-serif', }, }, }, }, }; ``` 注意:如果你已经有了一个 `less` 字段,只需将 `modifyVars` 添加到现有的 `less` 配置中即可。 6. 重新编译项目:重新启动项目的开发服务器或重新编译项目,以使更改生效。 通过上述步骤,你可以将 Ant Design Vue 中的字体设置为你下载的中文字体。记得将 `MyCustomFont` 替换为你字体文件中的实际字体名称。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值