1.4.11 • Published 3 years ago

antd-simple-table v1.4.11

Weekly downloads
3
License
MIT
Repository
github
Last release
3 years ago

antd-simple-table

Build Status Coverage Status License Version Code Style: Prettier Commitizen friendly Semantic Release Dependabot

✨ 特色功能

  • 列设置 开启 antd-simple-table 表头的列设置 ⚙️ 按钮,可以选择性的隐藏表格的某些列,还可以使用鼠标拖动切换列顺序。重要的是这些配置信息会被保存,关闭网页重新打开将会恢复之前配置。
  • 下载 antd-simple-table 提供将表格数据下载为 Exel 文件到用户电脑。

📦 安装

antd-simple-table 基于 Ant Design 构建,使用时务必同时安装 antd

npm i antd-simple-table @ant-design/icons antd

or

yarn add antd-simple-table @ant-design/icons antd

🔨 使用说明

示例

您可以参考以下 storybook 精选示例,以了解 ant-simple-table 的工作方式: https://antd-simple-table-b472afd.netlify.app

指定表格右上角功能键

表头默认展示全部功能键,包括下载、尺寸、刷新、设置列。可设置 options 选择性隐藏功能键。

<SimpleTable
  id="user"
  name="用户"
  options={{ size: false, download: true, setting: true, reload: true }}
/>

保存为 Excel 文件到本地

点击表头下载图标,可将表格数据以 Excel 文件存储到本地。设置 name 可指定保存到本地的文件名称。

<SimpleTable id="user" name="用户" />

// 保存到本地的文件名为:2020-05-31-用户

刷新列表

onRefresh 可实现刷新操作

<SimpleTable
  id="user"
  name="用户"
  onRefresh={async () => {
    const { data } = await axios.get(`https://xxx`);
    ...
  }}
/>

可复制的

columns 属性中设置 copyable 为 true 后,可将表格数据复制到剪切板。

<SimpleTable
  id="user"
  name="用户"
  columns={[
    {
      title: "日期",
      key: "day",
      dataIndex: "day",
      copyable: true,
    },
  ]}
/>

隐藏表格内容

当表格数据内容超出列宽度,可设置 ellipsis 为 true 进行缩略展示。

<SimpleTable
  id="user"
  name="用户"
  columns={[
    {
      title: "日期",
      key: "day",
      dataIndex: "day",
      ellipsis: true,
    },
  ]}
/>

默认值类型

SimpleTable 封装了一些常用的值类型来减少重复的 render 操作,配置 valueType 即可展示格式化响应的数据。

ValueType 目前支持的值类型如下:

类型描述渲染结果示例
ValueType.DATE日期2019-11-16
ValueType.DATE_TIME日期和时间2019-11-16 12:50:00
ValueType.TIME时间12:50:00
ValueType.INDEX行号-
ValueType.PERCENT百分号23%
ValueType.NUMBER数字23.23
ValueType.MONEY货币¥10,000.26、\$100、€100
ValueType.TAG标签-
ValueType.SWITCH开关-

1. 百分比

valueType 传入类型字符串

import { ValueType } from "antd-simple-table";

<SimpleTable
  id="user"
  name="用户"
  columns={[
    {
      title: "百分比",
      key: "percent",
      dataIndex: "percent",
      valueType: ValueType.PERCENT,
    },
  ]}
/>;

// 最大保留小数点后 20 位

valueType 传入类型配置对象

import { ValueType } from "antd-simple-table";
<SimpleTable
  id="user"
  name="用户"
  columns={[
    {
      title: "百分比",
      key: "percent",
      dataIndex: "percent",
      valueType: {
        type: ValueType.PERCENT,
        showSymbol: false,
        useGrouping: false,
        precision: 2,
      },
    },
  ]}
/>;
valueType 参数参数类型描述示例
typestring类型ValueType.PERCENT
showSymbolboolean是否显示正负号+23% (-23%)
useGroupingboolean是否显示分隔符10,000.00%
precisionnumber保留小数点后位数,默认两位23.00%

2. 货币

配置为货币类型,经过 antd-simple-table 内部 format 后自动转换为您想要的货币格式。

valueType 传入对象

import { ValueType } from "antd-simple-table";
const column = {
  title: "货币",
  key: "money",
  dataIndex: "money",
  valueType: {
    type: ValueType.MONEY,
    currency: "USD"
    useGrouping: false,
  },
};
valueType 参数参数类型描述示例
typestring类型ValueType.MONEY
currencystring |(value:any, record:T, index:number)=> string货币代码"USD"
useGroupingboolean是否显示分隔符\$10,000,000

⚠️ 注意:若 dataSource 中有指定货币符号,currency 可以传入函数,返回 record 中的货币符号。此场景适用于货币符号由后端数据决定。

currency 输出参数说明

参数参数类型描述示例
valueany--
recordT该行所有属性-
indexnumber该行在表格中的序号-
import { ValueType } from "antd-simple-table";
const column = {
  title: "货币",
  key: "money",
  dataIndex: "money",
  valueType: {
    type: ValueType.MONEY,
    currency: (value, record, index) => record.currencyCode
    useGrouping: false,
  },
};

3. 数字

valueType 传入字符串

import { ValueType } from "antd-simple-table";
const column = {
  title: "数字",
  key: "number",
  dataIndex: "number",
  valueType: ValueType.NUMBER,
};

// 最大保留小数点后 20 位,不展示分隔符

valueType 传入对象

import { ValueType } from "antd-simple-table";
const column = {
  title: "数字",
  key: "number",
  dataIndex: "number",
  valueType: {
    type: ValueType.NUMBER,
    useGrouping: true,
    precision: 3,
  },
};

// 保留小数点后 3 位,使用分隔符。
valueType 参数参数类型描述示例
typestring类型ValueType.NUMBER
precisionnumber保留小数点后位数,默认 3 位23.000
useGroupingboolean是否显示分隔符10,000,000

4. 标签

⚠️ 注意! dataSource 中的 tag 标签必须为数组才能正常渲染。

valueType 传入字符串

import { ValueType } from "antd-simple-table";
const column = {
  title: "标签",
  key: "tags",
  dataIndex: "tags",
  valueType: ValueType.TAG,
};

如果你想给表格中的标签绑定点击事件,可以以对象形式传入 onClick 属性。

valueType 传入对象

import { ValueType } from "antd-simple-table";
const column = {
  title: "数字",
  key: "number",
  dataIndex: "number",
  valueType: {
    type: ValueType.TAG,
    onClick: (tag, tags, record, index) => {
      console.log(tag, tags, record, index);
    },
  },
};

onClick 输出参数说明

参数参数类型描述示例
tagtag: string, index: number被点击的 tag, tag0 为 tag 文本,tag1 为在 tags 数组中的索引"Tony", 1
tagsany[]该行所有 tag"2020-6-1", "Tom", "管理员"
recordT该行所有属性-
indexnumber该行在表格中的序号-

5. 开关

valueType 传入对象

import { ValueType } from "antd-simple-table";
const column = {
  title: "开关",
  key: "enable",
  dataIndex: "enable",
  valueType: {
    type: ValueType.SWITCH,
    loading: loading,
    disabled: false,
    size: "small"
    onChange: (value, oldValue, record, index) => {
      console.log(value, oldValue, record, index);
    },
  },
};
valueType 参数参数类型描述示例
typestring类型ValueType.SWITCH
loadingboolean是否正在加载-
sizesmalldefault按钮大小-
disabledboolean是否禁用开关-
onChange(value, oldValue, record, index) => void拨动开关的事件回调-

onChange 输出参数说明

参数参数类型描述示例
valueboolean当前开关状态-
oldValueany原始数据-
recordT该行所有属性-
indexnumber该行在表格中的序号-
1.4.11

3 years ago

1.4.10

4 years ago

1.4.9

4 years ago

1.4.8

4 years ago

1.4.7

4 years ago

1.4.6

4 years ago

1.4.5

4 years ago

1.4.4

4 years ago

1.4.3

4 years ago

1.4.2

4 years ago

1.4.1

4 years ago

1.4.0

4 years ago

1.3.0

4 years ago

1.2.1

4 years ago

1.2.0

4 years ago

1.1.0

4 years ago

1.0.5

4 years ago

1.0.4

4 years ago

1.0.3

4 years ago

1.0.2

4 years ago

1.0.1

4 years ago

1.0.0

4 years ago

0.0.1

4 years ago