【问题标题】:Setting up a table layout in React Native在 React Native 中设置表格布局
【发布时间】:2017-06-04 18:12:33
【问题描述】:

我正在将一个 React 项目转换为 React Native,需要帮助在 React Native 中设置网格布局。我想设置一个 5-col by x-row(行数可能不同)的视图。我玩过 react-native-tableview-simple 包,但我无法指定单元格的跨度。我还尝试了 react-native-flexbox-grid 包,我可以设置列,但我仍然无法设置特定单元格的跨度宽度。我想知道有什么我可以使用的。

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

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

【问题讨论】:

    标签: react-native


    【解决方案1】:

    您可以在没有任何软件包的情况下执行此操作。如果每一行完全相同,则执行以下操作应该可以解决您的问题;

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

    【讨论】:

    • 我有一个问题是,如果我想跨越一个单元格的多列,有没有办法指定?由于我的行/列布局现在是静态的,我可能只需为单元格设置固定宽度即可实现这一点?
    • 使用 flex 做这件事要好得多。如果我是你,我会给 renderRow 函数一个指定单元格 flex 值的数组。之后在 View 的样式部分,您只需编写 flexVals[0]、flexsVals[1] 等。您甚至可以在那里使用 data.map 以使其更清晰。 @jamesvphan
    • 啊,明白了,应该已经意识到 flex 属性决定了宽度。而且我肯定会选择 flex 路由,因为它比使用静态值响应更快。感谢您的帮助!
    • 欢迎您的伙伴。请记住,您还可以添加诸如 minWidth、maxWidth 之类的内容以进一步自定义它@jamesvphan
    • 是的;它通常不以这种方式使用,但是这个答案更多的是“如何做”,而不是真正的代码组织。
    【解决方案2】:

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

    React Native Easy Grid

    我同意基本的弹性表应该内置到 React Native 中,但直到它们成为这个库对我来说非常有用。

    【讨论】:

    • 我喜欢不必添加依赖项,但是当这样的事情自己写起来很痛苦而且有点难看时,只需使用一个简单的库就可以更好/更容易/更清洁处理繁重的工作。在我看来,这是要走的路。尤其是因为它得到了很好的使用和支持。
    【解决方案3】:

    虽然这是一个旧帖子,但我正在寻找问题中类似的问题,

    我已经集思广益并在互联网上寻找解决方案,我得到的最佳解决方案是将 react-native-table-component 包导入我的项目并为我的应用程序准备一个不错的表格布局来显示数据。可能有更多的人想办法做到这一点,我可以推荐这个链接到你所有的答案:This link is the npm package installer link which have explanation to all the code and have examples

    【讨论】:

    • 是否有任何 TypeScript 类型用于此?
    【解决方案4】:

    使用功能组件:

    (指定数组的行数和列数)

    创建一个 Row 组件

    function Row({ column }) {  
      return (
        <View style={styles.rowStyle}>
          {column.map((data) => (
            <Cell data={data} />
          ))}
        </View>
     );
    }
    

    创建一个 Cell 组件

    function Cell({ data }) {
      return (
        <View style={styles.cellStyle}>
          <Text>{data}</Text>
        </View>
      );
    }
    

    创建一个网格组件

    function Grid() {
      const data = [
        [15, 14, 13, 12],
        [11, 10, 9, 8],
        [7, 6, 5, 4],
        [0, 1, 2, 3],
      ];
      return (
        <View style={styles.gridContainer}>
          {data.map((column) => (
            <Row column={column} />
          ))}
        </View>
      );
    }
    

    为组件设置样式:

    const styles = StyleSheet.create({
      gridContainer: {
          width: 220,
      },
      rowStyle: {
        flexDirection: "row",
        alignItems: "center",
        justifyContent: "space-around",
      },
      cellStyle: {
        flex: 1,
        margin: 10,
      },
    });
    

    【讨论】:

    • 当我把它放在水平滚动视图中时,它只是缩小到字符的宽度
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    • 2021-01-11
    • 2017-10-16
    • 1970-01-01
    相关资源
    最近更新 更多