【问题标题】:How to show grid lines in flatList in react native如何在本机反应中在flatList中显示网格线
【发布时间】:2018-11-27 11:05:51
【问题描述】:

我有员工详细信息列表。我需要它们之间的网格线(以表格形式查看)。如何在 react native 中使用 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 

我需要将其视为表格(即带有行列边框)

【问题讨论】:

  • 只需在您的 renderItem 回调方法中为组件提供边框
  • @Siraj 我试过了,但它没有显示为表格
  • 你能分享一下它的外观截图吗?另外,分享您的样式表。

标签: react-native react-native-flatlist


【解决方案1】:

你可以使用FlstListItemSeparatorComponent属性

所以创建一个返回分隔视图的函数:

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

现在在FlatList使用这个方法

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

这将创建一个水平分隔视图。

对于垂直分隔视图更改样式,如:

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

【讨论】:

    【解决方案2】:

    我通过使用下面的 renderRow 代码解决了这个问题。我在网格视图中有 2 列。请通过替换 index % X === 0index 中的 X 来更改列长度,其中 Y 是 columns-1

    renderRow({ item, index }) {
        return (
          <View style={[styles.GridViewContainer, 
                        index % 2 === 0 ? {
                          borderLeftWidth: 1, borderLeftColor: 'black',
                          borderRightWidth: 1, borderRightColor: 'black',} 
                          : 
                          {borderRightWidth: 1, borderRightColor: 'black'}
    
                          ,{borderBottomWidth: 1, borderBottomColor: 'black'}
                          ,index <= 1 && {borderTopWidth: 1, borderBottomColor: 'black'}
                        ]}
          >
    
          ... render item code ...
            </View>
        )
      }
    

    【讨论】:

      【解决方案3】:

      找到了一种创建表格的简单方法,我正在使用 react-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

      【讨论】:

        【解决方案4】:

        这里 ItemSeparatorComponent 将渲染水平边框,渲染项目将给它垂直边框。下面的示例适用于两列,您可以更改 index%2 到任何你 numColumns 是什么。 就像如果 numColumns 是 3 那么它将是 index%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>
            )}
        

        【讨论】:

        • index % 2 !== 0 是什么意思: 1、borderLeftColor: 'black'}]}>{item}
        • 它是一个条件样式语句,这意味着仅当项目索引不能被 2 整除时才会出现borderLeft。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-01-21
        • 1970-01-01
        • 2017-09-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多