React Native 表格組件

本文原創首發於公衆號:ReactNative開發圈,轉載需註明出處。react

React Native 表格組件:react-native-data-table,純JS組件,功能強大。支持自定義表頭、行、單元格樣式。支持編輯單元格和選擇列。還能顯示子行。

效果圖

安裝方法

npm install --save react-native-data-tablegit

組件說明

表格組件主要分紅如下幾部分:

DataTable 表格
HeaderCell 列頭
Row 行
Cell 單元格
CheckableCell 可選擇單元格
EditableCell 可編輯單元格
Expansion 子行
其餘使用方法相似於官方的ListView組件github

使用示例

import {
  Cell,
  DataTable,
  Header,
  HeaderCell,
  Row,
  EditableCell,
  CheckableCell,
} from 'react-native-data-table';

render() {
    return (
      <View style={styles.container}>
        <DataTable
          style={styles.container}
          listViewStyle={styles.container}
          dataSource={this.state.ds}
          renderRow={this.renderRow}
          renderHeader={this.renderHeader}
        />
      </View>
    );
  }
  
  renderHeader() {
    return (
      <Header>
        <HeaderCell style={styles.headerCell} key="1" text="選擇" width={1} />
        <HeaderCell
          style={styles.headerCell}
          key="2"
          text="序號"
          width={1}
          onPress={() => this.onColumnSort()}
        />
        <HeaderCell
          style={styles.headerCell}
          key="3"
          text="科室名稱"
          width={3}
          isAscending={false}
          onPress={() => this.onColumnSort()}
        />
        <HeaderCell
          style={styles.headerCell}
          key="4"
          text="數量"
          width={1}
          isAscending={false}
          onPress={() => this.onColumnSort()}
        />
      </Header>
    );
  }

  renderRow(item) {
    let rowStyle = item.no%2 === 0  ? styles.whiteRow : styles.row;
    return (
      <Row style={rowStyle}>
        <CheckableCell
          style={styles.cell}
          width={1}
          onPress={() => this.onCheckablePress()}
          renderIsChecked={() => (
            <Icon name="checkbox-blank-outline" size={20} color="blue" />
          )}
          renderIsNotChecked={() => (
            <Icon name="checkbox-marked" size={20} color="blue" />
          )}
          isChecked={item.isChecked}
        />
        <Cell style={styles.cell} width={1}>
          {item.no}
        </Cell>
        <Cell style={styles.cell} width={3}>
          {item.name}
        </Cell>
        <EditableCell width={1} value={item.qty} onEndEditing={(target, value) => {}}>
        </EditableCell>
      </Row>
    );
  }

  onCheckablePress() {}

  onColumnSort() {}

完整示例

完整代碼:https://github.com/forrest23/...
本次示例代碼在 Component05文件夾中。請不要吝嗇大家的Starnpm

組件地址

https://github.com/sussol/rea...react-native

微信不讓跳轉外鏈,能夠點擊查看原文來查看外鏈GitHub內容。微信

舉手之勞關注個人微信公衆號:ReactNative開發圈
this

相關文章
相關標籤/搜索