# react-native-table-component
**Repository Path**: skylarklxlong/react-native-table-component
## Basic Information
- **Project Name**: react-native-table-component
- **Description**: No description available
- **Primary Language**: Unknown
- **License**: MIT
- **Default Branch**: master
- **Homepage**: None
- **GVP Project**: No
## Statistics
- **Stars**: 0
- **Forks**: 0
- **Created**: 2022-04-07
- **Last Updated**: 2022-04-07
## Categories & Tags
**Categories**: Uncategorized
**Tags**: None
## README
React Native Table Component
* 由于工作原因该项目不再维护,如有需要可以转让
如果您对项目感兴趣,请通过电子邮件联系594244274@qq.com.
为react-native设计的表格组件.
- [安装](#安装)
- [版本日志](#版本日志)
- [示例](#示例)
- [组件属性](#组件属性)
- [注意事项](#注意事项)
- [License](#license)
[Switch to English document](https://github.com/Gil2015/react-native-table-component#Changelogs)
## 安装
> npm install react-native-table-component
`USE:`
```jsx
import { Table, TableWrapper, Row, Rows, Col, Cols, Cell } from 'react-native-table-component';
```
## 版本日志
+ [v1.0.3]
- 组件名 'TableWraper' 改为 'TableWrapper';
+ [v1.1.1]
- data属性内可以插入自定义组件
+ [v1.2.1]
- borderWidth默认值从1改为0
## Examples
#### 例一
```jsx
import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import { Table, Row, Rows } from 'react-native-table-component';
export default class ExampleOne extends Component {
constructor(props) {
super(props);
this.state = {
tableHead: ['Head', 'Head2', 'Head3', 'Head4'],
tableData: [
['1', '2', '3', '4'],
['a', 'b', 'c', 'd'],
['1', '2', '3', '456\n789'],
['a', 'b', 'c', 'd']
]
}
}
render() {
const state = this.state;
return (
)
}
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
head: { height: 40, backgroundColor: '#f1f8ff' },
text: { margin: 6 }
});
```
---
#### 例二
```jsx
import React, { Component } from 'react';
import { StyleSheet, View } from 'react-native';
import { Table, TableWrapper, Row, Rows, Col } from 'react-native-table-component';
export default class ExampleTwo extends Component {
constructor(props) {
super(props);
this.state = {
tableHead: ['', 'Head1', 'Head2', 'Head3'],
tableTitle: ['Title', 'Title2', 'Title3', 'Title4'],
tableData: [
['1', '2', '3'],
['a', 'b', 'c'],
['1', '2', '3'],
['a', 'b', 'c']
]
}
}
render() {
const state = this.state;
return (
)
}
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
head: { height: 40, backgroundColor: '#f1f8ff' },
wrapper: { flexDirection: 'row' },
title: { flex: 1, backgroundColor: '#f6f8fa' },
row: { height: 28 },
text: { textAlign: 'center' }
});
```
---
#### 例三
```jsx
import React, { Component } from 'react';
import { StyleSheet, View, ScrollView } from 'react-native';
import { Table, TableWrapper, Row } from 'react-native-table-component';
export default class ExampleThree extends Component {
constructor(props) {
super(props);
this.state = {
tableHead: ['Head', 'Head2', 'Head3', 'Head4', 'Head5', 'Head6', 'Head7', 'Head8', 'Head9'],
widthArr: [40, 60, 80, 100, 120, 140, 160, 180, 200]
}
}
render() {
const state = this.state;
const tableData = [];
for (let i = 0; i < 30; i += 1) {
const rowData = [];
for (let j = 0; j < 9; j += 1) {
rowData.push(`${i}${j}`);
}
tableData.push(rowData);
}
return (
{
tableData.map((rowData, index) => (
))
}
)
}
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
header: { height: 50, backgroundColor: '#537791' },
text: { textAlign: 'center', fontWeight: '100' },
dataWrapper: { marginTop: -1 },
row: { height: 40, backgroundColor: '#E7E6E1' }
});
```
---
#### 例四
```jsx
import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Alert } from 'react-native';
import { Table, TableWrapper, Row, Cell } from 'react-native-table-component';
export default class ExampleFour extends Component {
constructor(props) {
super(props);
this.state = {
tableHead: ['Head', 'Head2', 'Head3', 'Head4'],
tableData: [
['1', '2', '3', '4'],
['a', 'b', 'c', 'd'],
['1', '2', '3', '4'],
['a', 'b', 'c', 'd']
]
}
}
_alertIndex(index) {
Alert.alert(`This is row ${index + 1}`);
}
render() {
const state = this.state;
const element = (data, index) => (
this._alertIndex(index)}>
button
);
return (
{
state.tableData.map((rowData, index) => (
{
rowData.map((cellData, cellIndex) => (
))
}
|
))
}
)
}
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
head: { height: 40, backgroundColor: '#808B97' },
text: { margin: 6 },
row: { flexDirection: 'row', backgroundColor: '#FFF1C1' },
btn: { width: 58, height: 18, backgroundColor: '#78B7BB', borderRadius: 2 },
btnText: { textAlign: 'center', color: '#fff' }
});
```
---
#### 例五
```jsx
import React, { Component } from 'react';
import { StyleSheet, View, Text, TouchableOpacity, Alert } from 'react-native';
import { Table, TableWrapper,Col, Cols, Cell } from 'react-native-table-component';
export default class ExampleFive extends Component {
constructor(props) {
super(props);
const elementButton = (value) => (
this._alertIndex(value)}>
button
);
this.state = {
tableTitle: ['Title', 'Title2', 'Title3', 'Title4'],
tableData: [
[elementButton('1'), 'a', 'b', 'c', 'd'],
[elementButton('2'), '1', '2', '3', '4'],
[elementButton('3'), 'a', 'b', 'c', 'd']
]
}
}
_alertIndex(value) {
Alert.alert(`This is column ${value}`);
}
render() {
const state = this.state;
return (
{/* 左边模块 */}
|
{/* 右边模块 */}
)
}
}
const styles = StyleSheet.create({
container: { flex: 1, padding: 16, paddingTop: 30, backgroundColor: '#fff' },
singleHead: { width: 80, height: 40, backgroundColor: '#c8e1ff' },
head: { flex: 1, backgroundColor: '#c8e1ff' },
title: { flex: 2, backgroundColor: '#f6f8fa' },
titleText: { marginRight: 6, textAlign:'right' },
text: { textAlign: 'center' },
btn: { width: 58, height: 18, marginLeft: 15, backgroundColor: '#c8e1ff', borderRadius: 2 },
btnText: { textAlign: 'center' }
});
```
---
## 组件属性
| 属性 | 类型 | 描述 | 默认值 |
|---|---|---|---|
| data | Array | 组件数据 | `null` |
| style | Style | 组件style样式 | `null` |
| borderStyle| Object| 表格边框样式 | `{ borderWidth: 0, borderColor: #000 }` |
| textStyle | Style | 子项文字样式 | `null` |
| flexArr | Array | 每列的flex值 | `[]` |
| widthArr | Array | 每列的宽度值 | `[]` |
| heightArr | Array | 每行的高度值 | `[]` |
| ...props | any | 更多其它属性 | |
---
## 注意事项
+ Col和Cols里的单元格无法做到每行自适应高度
+ 请在textStyle属性里设置margin值来调整内边距,不要用padding值
+ 如果父元素不是Table组件,需要设置borderStyle属性
```jsx
{/* 如果父元素不是Table组件,需要设置borderStyle属性 */}
```
## License
[MIT](LICENSE)