Как показать линии сетки в flatList в реагировать на натив - PullRequest
0 голосов
/ 27 ноября 2018

У меня есть список данных о сотрудниках.Мне нужны линии сетки между ними (для просмотра в виде таблицы).Как это возможно с flatList в реагировать родной?

    <View >
                  <View style={Styles.empTab}>
                    <ScrollView horizontal={true} >
                      <View style={Styles.empTable}>
                        <Text>SL#</Text>
                        <FlatList
                          //style={Styles.empData}
                          data={this.state.empData}
                          keyExtractor={item => item.emp_id + ""}
                          renderItem={({ item }) => (
                            <View style={Styles.empitem}>
                              <Text>{item["emp_id"]}</Text>
                            </View>
                          )}
                        />
                      </View>

                      <View style={Styles.empTable}>
                        <Text>Name</Text>
                        <FlatList
                          //style={Styles.empData}
                          data={this.state.empData}
                          keyExtractor={item => item.emp_id + ""}
                          renderItem={({ item }) => (
                            <View  style={Styles.empitem}>
                              <Text>{item["name"]}</Text>
                            </View>
                          )}
                        />
                      </View>
                    </ScrollView>
                  </View>

Результат похож на

SL#  Name 
1    ab     
2     gh 

Мне нужно просмотреть его в виде таблицы (то есть с границей столбца-строки)

Ответы [ 3 ]

0 голосов
/ 27 ноября 2018

Найден простой способ создать вашу таблицу, для этого я использую act-native-table-component .

import React, { Component } from "react";
import { StyleSheet, View } from 'react-native';
import { Table, TableWrapper, Row, Rows } from 'react-native-table-component';

export default class App extends Component {
  constructor(props) {
    super(props);

    this.state = {
      tableHead: ['SL#', 'Name'],
      tableData: [
        ['1', 'ab'],
        ['2', 'gh'],
        ['3', 'asdf'],
      ]
    }
  }
  render() {
    const state = this.state;
    return (
      <View style={styles.container}>
        <Table>
          <Row data={state.tableHead} flexArr={[1, 1]} style={styles.head} textStyle={styles.text} />
          <TableWrapper style={styles.wrapper}>
            <Rows data={state.tableData} flexArr={[1, 1]} style={styles.row} textStyle={styles.text} />
          </TableWrapper>
        </Table>
      </View>
    )
  }
}

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'
  }
});

Подробнее вы можете прочитать здесь: https://www.npmjs.com/package/react-native-table-component

0 голосов
/ 27 ноября 2018

Здесь ItemSeparatorComponent будет отображать горизонтальную границу, а элемент визуализации - вертикальную границу.Приведенный ниже пример будет работать для двух столбцов, вы можете изменить индекс% 2 на то, что у вас есть numColumns.Например, если numColumns равно 3, это будет индекс% 3

<FlatList
      numColumns={2}
      ItemSeparatorComponent={this.renderSeparator}
    </FlatList>

renderSeparator = () => (
    <View
      style={{
        backgroundColor: 'black',
        height: 1
      }}
    />
  );

renderItem={({ item, index }) => (
        <Text style={[styles.text, index % 2 !== 0 && {borderLeftWidth: 1, borderLeftColor: 'black'}]}>{item}</Text>
    )}
0 голосов
/ 27 ноября 2018

Вы можете использовать ItemSeparatorComponent свойство FlstList

Поэтому создайте одну функцию, которая будет возвращать разделенное представление:

renderSeparatorView = () => {
    return (
      <View style={{
          height: 1, 
          width: "100%",
          backgroundColor: "#CEDCCE",
        }}
      />
    );
  };

Теперь используйте этот метод в FlatList

  <FlatList
        ...
        ItemSeparatorComponent={this.renderSeparatorView}
      />

Это создаст представление горизонтального разделителя.

Для представления вертикального разделителя измените стиль, например:

     style={{
          height: 100%, 
          width: "1",
          backgroundColor: "#CEDCCE",
        }}
...