React Native 表格组件

前端之家收集整理的这篇文章主要介绍了React Native 表格组件前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

本文原创首发于公众号:ReactNative开发圈,转载需注明出处。

React Native 表格组件:react-native-data-table,纯JS组件,功能强大。支持自定义表头、行、单元格样式。支持编辑单元格和选择列。还能显示子行。

效果

安装方法

npm install --save react-native-data-table

组件说明

表格组件主要分成以下几部分:

DataTable 表格
HeaderCell 列头
Row 行
Cell 单元格
CheckableCell 可选择单元格
EditableCell 可编辑单元格
Expansion 子行
其他使用方法类似于官方的ListView组件

使用示例

  1. import {
  2. Cell,DataTable,Header,HeaderCell,Row,EditableCell,CheckableCell,} from 'react-native-data-table';
  3.  
  4. render() {
  5. return (
  6. <View style={styles.container}>
  7. <DataTable
  8. style={styles.container}
  9. listViewStyle={styles.container}
  10. dataSource={this.state.ds}
  11. renderRow={this.renderRow}
  12. renderHeader={this.renderHeader}
  13. />
  14. </View>
  15. );
  16. }
  17. renderHeader() {
  18. return (
  19. <Header>
  20. <HeaderCell style={styles.headerCell} key="1" text="选择" width={1} />
  21. <HeaderCell
  22. style={styles.headerCell}
  23. key="2"
  24. text="序号"
  25. width={1}
  26. onPress={() => this.onColumnSort()}
  27. />
  28. <HeaderCell
  29. style={styles.headerCell}
  30. key="3"
  31. text="科室名称"
  32. width={3}
  33. isAscending={false}
  34. onPress={() => this.onColumnSort()}
  35. />
  36. <HeaderCell
  37. style={styles.headerCell}
  38. key="4"
  39. text="数量"
  40. width={1}
  41. isAscending={false}
  42. onPress={() => this.onColumnSort()}
  43. />
  44. </Header>
  45. );
  46. }
  47.  
  48. renderRow(item) {
  49. let rowStyle = item.no%2 === 0 ? styles.whiteRow : styles.row;
  50. return (
  51. <Row style={rowStyle}>
  52. <CheckableCell
  53. style={styles.cell}
  54. width={1}
  55. onPress={() => this.onCheckablePress()}
  56. renderIsChecked={() => (
  57. <Icon name="checkBox-blank-outline" size={20} color="blue" />
  58. )}
  59. renderIsNotChecked={() => (
  60. <Icon name="checkBox-marked" size={20} color="blue" />
  61. )}
  62. isChecked={item.isChecked}
  63. />
  64. <Cell style={styles.cell} width={1}>
  65. {item.no}
  66. </Cell>
  67. <Cell style={styles.cell} width={3}>
  68. {item.name}
  69. </Cell>
  70. <EditableCell width={1} value={item.qty} onEndEditing={(target,value) => {}}>
  71. </EditableCell>
  72. </Row>
  73. );
  74. }
  75.  
  76. onCheckablePress() {}
  77.  
  78. onColumnSort() {}

完整示例

完整代码https://github.com/forrest23/...
本次示例代码在 Component05文件夹中。请不要吝啬你们的Star

组件地址

https://github.com/sussol/rea...

微信不让跳转外链,可以点击查看原文来查看外链GitHub内容

举手之劳关注我的微信公众号:ReactNative开发圈

猜你在找的React相关文章