首页 文章

在React Native中设置表格布局

提问于
浏览
10

我正在将React项目转换为React Native,需要帮助在React Native中设置网格布局 . 我想通过x行设置一个5-col(行数可能会有所不同)视图 . 我已经使用了react-native-tableview-simple软件包,但我无法指定单元格的范围 . 我也尝试过react-native-flexbox-grid包,我可以设置列,但是我仍然无法设置特定单元格的 Span 宽度 . 我想知道我能用什么 .

作为参考,我希望我的表看起来像这样的行:

|Col 1|Col 2|Col 3|Col 4|Col 5|
     |------------------------------
Row 1|     Text        | Yes |  No | 
     |------------------------------
Row 2|     Text        | Yes |  No | 
     |------------------------------
Row 3|     Text        |  Dropdown |

3 回答

  • 21

    你可以不用任何包来做到这一点 . 如果每行完全相同,则执行以下操作应解决您的问题;

    export default class Table extends Component {
        renderRow() {
            return (
                <View style={{ flex: 1, alignSelf: 'stretch', flexDirection: 'row' }}>
                    <View style={{ flex: 1, alignSelf: 'stretch' }} /> { /* Edit these as they are your cells. You may even take parameters to display different data / react elements etc. */}
                    <View style={{ flex: 1, alignSelf: 'stretch' }} />
                    <View style={{ flex: 1, alignSelf: 'stretch' }} />
                    <View style={{ flex: 1, alignSelf: 'stretch' }} />
                    <View style={{ flex: 1, alignSelf: 'stretch' }} />
                </View>
            );
        }
    
        render() {
            const data = [1, 2, 3, 4, 5];
            return (
                <View style={{ flex: 1, alignItems: 'center', justifyContent: 'center' }}>
                {
                    data.map((datum) => { // This will render a row for each data element.
                        return this.renderRow();
                    })
                }
                </View>
            );
        }
    }
    
  • 3

    在寻找答案后,我发现了一个非常棒的库,其行为与Bootstrap表非常相似 . 我发现使用React Native的布局非常具有挑战性,但是这个库提供了可预测的布局结果 .

    React Native Easy Grid

    我同意应该在React Native中内置基本的flex表,但是直到他们这个库对我来说非常有用 .

  • 0

    如果你想要一个通用的Table组件,为了获得这样的输出:

    enter image description here

    import React from 'react';
    
    import { Text, View } from 'react-native'
    
    export default class Table extends React.Component {
    
        renderRow(row) {
            return (
                <View style={{ flex: 1, flexDirection: 'row', padding: 10 }}>
                    {
                        row.map((column) => {
                            return (
                                this.renderColumn(column)
                            )
                        })
                    }
                </View>
            );
        }
    
        renderColumn(column) {
            return (
                <View style={{
                    flex: 1, flexDirection: 'column', alignItems: 'center', justifyContent: 'center'
                }}>
                    {
                        column.map((item) => {
                            return (
                                <View style={{ flex: 1, flexDirection: "row" }}>
                                    <Text style={{ flex: 1, alignSelf: 'center' }} size={16}>{item}</Text>
                                </View>
                            )
                        })
                    }
                </View>
            )
        }
    
        render() {
            const data = this.props.dataSource;
            return (
                <View style={{
                    alignSelf: 'stretch',
                    flexDirection: 'column',
                    alignItems: 'center',
                }}>
                    {
                        data.map((row) => {
                            return this.renderRow(row);
                        })
                    }
                </View>
            );
        }
    }
    

    在主UI文件中,您可以通过以下方式呈现表:

    render() {
        const dataSource = [
            [["Row1 Column 1 Item0"], ["Row1 Column1 Item0 "]],
            [["Row2 Column 1 Item0"], ["Row2 Column2 Item0", "Row2 Column2 Item1"]],
        ]
        return (
            <UWTable dataSource={dataSource}/>
        )
    }
    

相关问题