1.7.5 • Published 3 months ago

taro-react-table v1.7.5

Weekly downloads
-
License
MIT
Repository
-
Last release
3 months ago

taro-react-table

基于 Taro3、React 的 H5 和微信小程序多端表格组件

  • 兼容 H5、微信小程序
  • 自定义样式
  • 自定义排序
  • 固定表头
  • 滚动上拉加载

npm.io

安装

npm install taro-react-table

配置

  • config/index.js 配置
  • 让 taro 去通过 postcss 编译处理 taro-react-table模块, 需要对 taro-react-table 里的样式单位进行转换适配
// config/index.js
const config = {
  h5: {
    esnextModules: ['taro-react-table'],
  },
}

导入组件

import Table from 'taro-react-table'
import 'taro-react-table/dist/index.css'

参数说明

Table

参数描述类型必传默认值
本身参数参考ScrollView
dataSource数据源object[][]
columns表格列的配置描述,具体项见下表Columns[][]
rowKey表格行 key 的取值,可以是字符串或一个函数string | function(record): stringkey
wrapperClass外层容器的类名string
wrapperStyle外层容器的样式object
classNameScrollView 容器类名string
styleScrollView 容器样式object
rowClassName行类名string
rowStyle行样式object
colClassName单元格类名string
colStyle单元格样式object
titleStyle标题样式object
titleClassName标题类名string
titleClassName标题类名string
loading是否显示加载boolean
loadingText加载文本string''
loadStatus加载状态loading | noMore | nullnull
unsort设置是否取消排序 (一般需求不需要取消排序,设置 true 可开启取消排序)booleanfalse
showHeader是否显示表头booleantrue
noMoreTextloadStatus 为 noMore 状态显示文案string没有更多了
loadLoadingTextloadStatus 为 loading 状态显示文案string加载中...
distance小于视窗距离多少开始触发 onLoadnumber30
showLoad是否显示 load 加载状态,为 false 时不触发 onLoad 事件booleantrue
fixedLoad是否固定加载更多,不随 X 轴滚动而滚动booleantrue
emptyText空数据显示文本string | ReactNode暂无数据
cellEmptyText单元格空数据显示文本string-
renderEmpty自定义渲染空数据ReactNode
size间距大小'small' | 'middle' | 'large' | numbermiddle
colWidth默认列宽度number120
striped是否显示斑马纹 (为 true 时和 even 效果一样)'odd' | 'even' | booleanfalse

Events

事件名描述类型必传默认值
onLoad滚动底部触发,用于上拉加载,(注意:需要设置 height 高度,才能触发)Function
onSorter点击表头按钮触发排序({ column, field, order }: SorterEvent)
onRow行点击事件function(record, index)

Column

参数描述类型必传默认值
title标题stringJSX.Element
dataIndex列数据在数据项中对应的路径string[]
key表格行 key 的取值,可以是字符串或一个函数string | function(record): stringkey
align设置该列文本对齐方式stringcenter
style标题样式object
align外层容器的类名string
className标题类名string
render生成复杂数据的渲染函数,参数分别为当前行的值,当前行数据,行索引function(text, record, index) {}
widthScrollView 容器类名string
fixed固定列left | right
sorter排序函数,本地排序使用一个函数(参考 Array.sort 的 compareFunction),需要服务端排序可设为 trueCompareFn
sortOrder排序的受控属性,外界可用此控制列的排序,可设置为 ascend descend falseboolean | string
onCell单元格点击事件function(record, index)

使用

import { useState } from 'react'
import 'taro-react-table/dist/index.css'
import Table,{ Columns, LoadStatus, SorterEvent } from 'taro-react-table'

export default function Demo() {
 const [loading, setLoading] = useState(false)
 const [dataSource, setDataSource] = useState([
    {
      name1: '无人之境1',
      name2: '打回原形1',
      name3: '防不胜防1',
      name4: '十面埋伏1',
      name5: 'k歌之王1',
      name6: '岁月如歌1',
    },
    {
      name1: '无人之境2',
      name2: '打回原形2',
      name3: '防不胜防2',
      name4: '十面埋伏2',
      name5: 'k歌之王2',
      name6: '岁月如歌2',
    },
    {
      name1: '无人之境3',
      name2: '打回原形3',
      name3: '防不胜防3',
      name4: '十面埋伏3',
      name5: 'k歌之王3',
      name6: '岁月如歌3',
    },
    {
      name1: '无人之境4',
      name2: '打回原形4',
      name3: '防不胜防4',
      name4: '十面埋伏4',
      name5: 'k歌之王4',
      name6: '岁月如歌4',
    },
    {
      name1: '无人之境5',
      name2: '打回原形5',
      name3: '防不胜防5',
      name4: '十面埋伏5',
      name5: 'k歌之王5',
      name6: '岁月如歌5',
    },
    {
      name1: '无人之境6',
      name2: '打回原形6',
      name3: '防不胜防6',
      name4: '十面埋伏6',
      name5: 'k歌之王6',
      name6: '岁月如歌6',
    },
  ])
  const [loadStatus, setLoadStatus] = useState<LoadStatus>(null)
  const [sortInfo, setSortInfo] = useState<Omit<SorterEvent, 'column'>>({
    field: 'name1',
    order: 'ascend',
  })

   const columns: Columns[] = [
    {
      title: 'Song1',
      dataIndex: 'name1',
      sorter: true,
      fixed: 'left',
      width: 100,
      sortOrder: sortInfo.field == 'name1' && sortInfo.order,
    },
    {
      title: 'Song2',
      width: 100,
      dataIndex: 'name2',
    },
    {
      title: 'Song3',
      dataIndex: 'name3',
    },
    {
      title: 'Song4',
      dataIndex: 'name4',
    },
    {
      title: 'Song5',
      dataIndex: 'name5',
    },
    {
      title: 'Song6',
      dataIndex: 'name6',
    },
  ]

  const getList = async (): Promise<any[]> => {
    return new Promise((resolve, reject) => {
      setTimeout(() => {
        const list = [...dataSource]
        for (let i = 1; i < 10; i++) {
          const size = list.length + 1
          list.push({
            name1: `无人之境${size}`,
            name2: `打回原形${size}`,
            name3: `防不胜防${size}`,
            name4: `十面埋伏${size}`,
            name5: `k歌之王${size}`,
            name6: `岁月如歌${size}`,
          })
        }
        resolve(list)
      }, 1000)
    })
  }

  const onLoad = async e => {
    setLoadStatus('loading')
    const list = await getList()
    setDataSource(list)
    setLoadStatus(list.length > 20 ? 'noMore' : null)
  }

  // 排序回调
  const onSorter = ({ column, field, order }: SorterEvent) => {
    console.log(column, field, order)
    // 模拟排序加载效果
    setLoading(state => !state)
    setSortInfo({ order, field })
    const tempList = [...dataSource]
    setTimeout(() => {
      setLoading(false)
      tempList.reverse()
      setDataSource(tempList)
    }, 1000)
  }

  return (
    <Table
      loading={loading}
      dataSource={dataSource}
      columns={columns}
      style={{ height: '250px' }}
      onLoad={onLoad}
      loadStatus={loadStatus}
      onSorter={onSorter}
    ></Table>
  )
}

友情推荐

项目描述
taro-react-echarts基于 Taro3、React 的 H5 和微信小程序多端图表组件
1.7.5

3 months ago

1.7.4

1 year ago

1.7.3

1 year ago

1.7.2

1 year ago

1.7.1

1 year ago

1.6.2

1 year ago

1.7.0

1 year ago

1.6.1

1 year ago

1.6.0

1 year ago

1.5.1

2 years ago

1.3.7

2 years ago

1.3.6

2 years ago

1.3.5

2 years ago

1.3.4

2 years ago

1.3.3

2 years ago

1.5.0

2 years ago

1.3.2

2 years ago

1.2.3

2 years ago

1.4.0

2 years ago

1.3.1

2 years ago

1.2.2

2 years ago

1.3.0

2 years ago

1.2.1

2 years ago

1.3.9

2 years ago

1.3.8

2 years ago

1.2.0

2 years ago

1.1.1

2 years ago

1.1.0

2 years ago

1.0.1

2 years ago

1.0.0

2 years ago

1.1.8

2 years ago

1.1.7

2 years ago

1.0.8

2 years ago

1.1.6

2 years ago

1.0.7

2 years ago

1.1.5

2 years ago

1.1.4

2 years ago

1.0.5

2 years ago

1.1.3

2 years ago

1.0.4

2 years ago

1.1.2

2 years ago

1.0.3

2 years ago

0.0.6

2 years ago

0.0.5

2 years ago

0.0.3

2 years ago

0.0.2

2 years ago

0.0.1

2 years ago