tarojs-table
TypeScript icon, indicating that this package has built-in type declarations

1.1.0 • Public • Published

tarojs-table

npm npm GitHub forks GitHub Repo stars

介绍

tarojs-table 是一个基于 Taro3.x 和 React18.x 封装的基础表格。

注意:这只是一个非常基础的表格组件,暂不支持合并行/列,展开行等功能,请谨慎选用。(所支持的功能请看属性配置

(温馨提示:此组件仅在微信小程序上运行调试过,并未在其他平台进行调试运行,无法保证在其他平台能正常使用。)

安装

npm install tarojs-table

使用

import { View } from '@tarojs/components';
import { useEffect, useState } from 'react';
import Table, { ColumnProps } from "tarojs-table";
// 内含 .css|.less|.scss 类型的样式文件,可自行选择引入
import "tarojs-table/lib/style.css";
// import 'tarojs-table/lib/style.less'
// import 'tarojs-table/lib/style.scss';

export default () => {
  const [loading, setLoading] = useState(false);
  const [dataSource, setDataSource] = useState<any[]>([]);

  useEffect(() => {
    // 模拟loading
    setLoading(true);

    setTimeout(() => {
      setLoading(false);
    }, 2000);

    setDataSource([
      { name: 'Margaret', gender: 'Female', age: 29, occupation: 'Financial Analyst', address: '789 Birch St' },
      { name: 'Michael', gender: 'Male', age: 42, occupation: 'Marketing Manager', address: '456 Pine Ave' },
      //{ ...其他数据 }
    ]);
  }, []);

  const columns: ColumnProps[] = [
    {
      title: '序号',
      dataIndex: 'index',
      key: 'index',
      width: 50,
      fixed: 'left',
      align: 'center',
      render: (text: any, record: any, index: number) => index + 1
    },
    // {...其他列}
    {
      title: '操作',
      dataIndex: 'action',
      key: 'action',
      width: 80,
      fixed: 'right',
      align: 'center',
      className: 'action-cell',
      render: (text: any, record: any, index: number) => <View style={{ color: "blue" }}>编辑</View>
    }
  ];

  return (
    <Table
      loading={loading}
      columns={columns}
      dataSource={dataSource}
      scroll={{ y: '100vh', x: '100vw' }}
    />
  );
};

效果:

属性配置

TableProps

参数 说明 类型 可选 默认值
id 表格 id string
columns 表格列 ColumnProps[]
dataSource 表格数据 any[]
className 类样式 string
style 内联样式 object
wrapperClassName 最外层的包裹容器类样式 string
wrapperStyle 最外层的包裹容器内联样式 object
rowKey 唯一标识 string
loading 加载中遮罩 boolean 丨 LoadingProps
empty 空数据时显示 TableEmptyProps
scroll 内容超出范围滚动 { x?: number 丨 string, y?: number 丨 string }
onRow 行属性 (record: any, index: number) => RowProps
onScroll 滚动时触发 Function
onScrollToLower 滚动到底部/右边,会触发 scrolltolower 事件 Function
onScrollToUpper 滚动到顶部/左边,会触发 scrolltoupper 事件 Function

ColumnProps

参数 说明 类型 可选 默认值
title 列名 React.ReactNode
key 列唯一标识 string
dataIndex 列字段 string
width 列宽 number
className 类样式 string
align 内容水平位置 'left' 丨 'center' 丨 'right'
fixed 固定列(设置此项时须设置列宽) 'left' 丨 'right'
ellipsis 文本超出省略(默认 1 行省略,可指定多行省略) boolean 丨 number
sortable 是否可排序 boolean
sortOrder 排序方式 'ascend'丨'descend'
onSort 自定义排序 (order?: 'ascend'丨'descend') => void
render 自定义渲染内容 (value: any, record: any, index: number) => React.ReactNode

RowProps

参数 说明 类型 可选 默认值
className 行样式 string
style 行内联样式 object
onTap 点击行 Function

TableEmptyProps

参数 说明 类型 可选 默认值
img 自定义图片(src) string
text 自定义提示文字 string

LoadingProps

参数 说明 类型 可选 默认值
show 是否显示 Loading boolean
img 自定义 loading 图片(src) string
text 自定义 loading 文字 string

Readme

Keywords

Package Sidebar

Install

npm i tarojs-table

Weekly Downloads

14,430

Version

1.1.0

License

MIT

Unpacked Size

45.7 kB

Total Files

8

Last publish

Collaborators

  • chutao.zhang