【问题标题】:React Native Grid view to show comparsion tableReact Native Grid 视图以显示比较表
【发布时间】:2016-11-29 23:07:10
【问题描述】:

使用 react native 如何构建一个比较表,看起来像 https://www.edrawsoft.com/templates/images/smart-phone-comparison-table.png

如果可能的话,我还想有一个节标题。例如,价格不在其自己的列中,但类似于 iOS 中的表部分标题 列表视图会给我一列,但我想要多列

谢谢

编辑: 下面的代码有效,但问题是如果我水平滚动,那么只有一个列表视图会滚动。我希望他们两个都滚动。 (我基本上是在尝试制作像 excel 这样的行和列,在我的情况下数据可能很大(30 行和 5-8 列),我不想对其进行硬编码。)

 render() {
    return (
      <ScrollView> 
      <ListView horizontal
        style={styles.container}
        dataSource={this.state.dataSource}
        renderRow={this.renderRow}
      />

         <ListView horizontal
        style={styles.container}
        dataSource={this.state.dataSource}
        renderRow={this.renderRow}
      />

      </ScrollView>
    )

在下图中,我希望在滚动第二个列表视图时滚动两个列表视图,但如图所示,只有第二个 ListView 正在滚动,而不是第一个...

【问题讨论】:

  • react-native 中没有开箱即用的 Table 组件。但是用ListView画出来并不难,每一行都用style={{flex: 1, flexDirection: 'row'}}进行渲染
  • @Takahiro 感谢您的评论。我不想硬编码这些值。数据将来自服务器。我尝试了上面的更新代码,但问题是如果我水平滚动,那么只有一个列表视图会滚动。我希望它们都滚动。
  • 为什么需要多个ListView?我以为你只需要一个(一张桌子)。如果您使用 ListViewDataSource,您当然不需要硬编码这些值。您需要做的就是转换您的 DataSource,并将每一行呈现为单元格列表,并使用 flex 布局来对齐它们。
  • 是的,假设我有一个滚动视图,其中有 2 个水平的列表视图。现在如果我滚动水平 listview1 我想 listView2 也被滚动。我该怎么做?
  • 恐怕不是,目前嵌套的滚动视图是 react-native 中的一个突出问题。 github.com/facebook/react-native/issues/8024

标签: listview reactjs react-native scrollview


【解决方案1】:

使用 flex-box 样式可以很容易地实现这一点。如果你是 React-native 的新手。 Listview 乍一看有点令人生畏(数据源,克隆行 {rowHasChanged: (r1, r2) => r1 !== r2})??)。为了保持简单并更好地理解 Flex-box,这应该可以完成您的工作。但更推荐使用 ListView。

import React, { Component } from 'react';
import { AppRegistry,View, ListView, StyleSheet,Text } from 'react-native';

class Example extends Component{
  render() {
    return (
      <View style={{flex:1}}>

          <View style={styles.Row}>
              <View style={styles.Box1}>
                  <Text>Box 1</Text>
              </View>
              <View style={styles.Box2}>
                  <Text>Box 2</Text>
              </View>
              <View style={styles.Box1}>
                  <Text>Box 3</Text>
              </View>
              {/*
                <View....
                The more Views you add here the more horizontal Boxes you have
                */}
          </View>


          <View style={styles.Row}>
              <View style={styles.Box2}>
                  <Text>Box 1</Text>
              </View>
              <View style={styles.Box1}>
                  <Text>Box 2</Text>
              </View>
              <View style={styles.Box2}>
                  <Text>Box 3</Text>
              </View>
          </View>
          {/*
            <View....
            The more Views you add here the more rows  you have
            */}

      </View>
    )
  }
}

const styles = StyleSheet.create({
  Row : {flex:1,flexDirection:'row'},
  Box1 : {flex:1,backgroundColor:'tan',alignItems:'center',justifyContent:'center'},
  Box2 : {flex:1,backgroundColor:'coral',alignItems:'center',justifyContent:'center'},
});

module.exports = Example

【讨论】:

  • 谢谢,但我如何使用 ListView 来实现这一点?因为我的表会很大,数据将从服务器获取。?我尝试了下面的代码,但是如果我滚动一个列表视图,另一个列表视图将不会水平滚动,这是我想要的。 (见上文我用代码更新了答案)
  • 您是否尝试并行实现两个列表视图(一个在左侧,一个在右侧),它们都应该独立滚动。
  • 是的,这是我能想到的唯一可以制作网格的方法。 @Vkrm
  • 在下图中,我希望在第二个滚动时滚动两个列表视图,但如图所示,只有第二个 ListView 正在滚动,而不是第一个... [1] :i.stack.imgur.com/XNvZz.png
  • 请看一下新的答案。如果没有帮助,请在此 PM 我:facebook.com/belvikram
【解决方案2】:
import React, { Component } from 'react';
import {
  AppRegistry,
  StyleSheet,
  Text,
  View,
  ScrollView
} from 'react-native';

class MultiScroll extends Component {

  render() {
    var tableData = []
    for(i = 0; i < 90; i++){
      tableData.push(['Row '+ i + ' Item 1','Row '+ i + ' Item 2','Row '+ i + ' Item 3','Row '+ i + ' Item 4','Row '+ i + ' Item 5','Row '+ i + ' Item 6','Row '+ i + ' Item 7','Row '+ i + ' Item 8','Row '+ i + ' Item 9','Row '+ i + ' Item 10'])
    }
    return (
      <View>
          <View style={{flexDirection:'row',marginTop:0}}>
          <ScrollView>
            <ScrollView horizontal = {true} showsHorizontalScrollIndicator= {false}>
                <View style={{}}>
                  {
                    tableData.map((eachRow,j) => {
                          return (
                            <View style={{flexDirection:'row'}} key = {j}>
                                {
                                  eachRow.map((eachItem,i) => {
                                    return <View key = {i} style={{width:100,height:30,backgroundColor:(((j+i)%2)?'tan':'coral'),alignItems:'center',justifyContent:'center'}}><Text>{eachItem}</Text></View>
                                  })
                                }
                            </View>
                          );
                      })
                  }
                </View>
            </ScrollView>
          </ScrollView>
          </View>
      </View>
    );
  }
}

module.exports = MultiScroll

希望这会有所帮助:)

【讨论】:

  • 这太棒了!只想要我想要的。谢谢! :) 最后一件事,如果我说 90 行,那么如果我将水平滚动视图放在垂直视图内,我的垂直滚动将不起作用
  • 感谢您的更新,但我仍然无法垂直滚动。它会自动反弹回来。这也发生在你身上吗? @Vkrm
  • 我没有垂直滚动的问题。可能尝试将持有该组件的父组件设置为 flex:1。
【解决方案3】:

<View>
  <View style={{flexDirection:'row'}}>
    <ScrollView horizontal ={true} showsHorizontalScrollIndicator= {false}>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
    </ScrollView>
  </View>
  <View style={{flexDirection:'row'}}>
    <ScrollView horizontal ={true} showsHorizontalScrollIndicator= {false}>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
        <View style={{width:100,height:30}}><Text>Button</Text></View>
    </ScrollView>
  </View>
</View>  

我无法在评论中插入图片。因此,添加作为答案。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2016-03-18
  • 2016-02-23
  • 2019-02-07
  • 2021-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多