javascript - 使用 react native 中的节标题水平显示数据

标签 javascript react-native

I am running sample app in react-native. I am using section headers for displaying list of data. In this case data will be displayed in vertical. My question is how to display the data in horizontal using section list. Please give me any suggestion. Thank You.

Here is my code:

 import { StyleSheet, Text, View, ListView, TouchableOpacity, Image, TouchableWithoutFeedback, ScrollView, SectionList, TextInput, WebView, Platform, FlatList } from 'react-native'
    var setListArray = [
      {
        name: 'Hotel 1',
        data: [
          {
            id: '1'
          },
          {
            id: '2'
          },
          {
            id: '3'
          },
          {
            id: '4'
          },
          {
            id: '5'
          },
          {
            id: '6'
          },
          {
            id: '7'
          },
          {
            id: '8'
          },
          {
            id: '9'
          },
          {
            id: '10'
          },
          {
            id: '11'
          },
          {
            id: '12'
          },
          {
            id: '13'
          },
          {
            id: '14'
          },
          {
            id: '15'
          },
          {
            id: '16'
          },
          {
            id: '17'
          },
          {
            id: '18'
          },
          {
            id: '19'
          },
          {
            id: '20'
          }
        ]
      },
      {
        name: 'Hotel 2',
        data: [
          {
            id: '1'
          },
          {
            id: '2'
          },
          {
            id: '3'
          },
          {
            id: '4'
          },
          {
            id: '5'
          },
          {
            id: '6'
          },
          {
            id: '7'
          },
          {
            id: '8'
          },
          {
            id: '9'
          },
          {
            id: '10'
          }
        ]
      },
      {
        name: 'Hotel 3',
        data: [
          {
            id: '1'
          },
          {
            id: '2'
          },
          {
            id: '3'
          },
          {
            id: '4'
          },
          {
            id: '5'
          },
          {
            id: '6'
          },
          {
            id: '7'
          },
          {
            id: '8'
          },
          {
            id: '9'
          },
          {
            id: '10'
          }
        ]
      }
    ]
    export default class Example extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
      dataSource: new ListView.DataSource({ sectionHeaderHasChanged: (r1, r2) => r1 !== r2, rowHasChanged: (r1, r2) => r1 !== r2 }),
    setListArray: []
    }
    }
    componentWillMount() {

        this.setState({
          setListArray: setListArray
        })
      }
    renderSectionHeader(section) {

        return (
          <View style={{ flex: 1, height: 30, alignItems: 'center', justifyContent: 'center', backgroundColor: 'black' }}>
            <Text style={{ color: 'white' }}>{section.section.name}</Text>
          </View>
        )
      }
    renderItem(item) {
    return (
          <View style={{ flexWrap: 'wrap', marginTop: 10, marginRight: 10, marginBottom: 10 }}>
            <TouchableOpacity>
              <Image style={{ height: 20, width: 20, marginLeft: 10 }} source={require('../images/available.png')} />
            </TouchableOpacity>
          </View>
        )
    }
      render() {
    return(
    <View style={{ flex: 1, marginTop: 10, flexWrap: 'wrap' }}>
      <SectionList sections={this.state.setListArray} renderItem={this.renderItem.bind(this)} renderSectionHeader={this.renderSectionHeader} />
</View>
    )
    }
    }

Here is my screenshot:

enter image description here

I want to display the data like this:

enter image description here

最佳答案

我认为你应该处理函数 renderItem 中的所有节数据,而不仅仅是一个节数据。一些代码,例如

<View style={{ flexWrap: 'wrap', marginTop: 10, marginRight: 10, marginBottom: 10 }}>
{
 section.data.map((item)=>{
    return (<TouchableOpacity>
      <Image style={{ height: 20, width: 20, marginLeft: 10 }} source={require('../images/available.png')} />
    </TouchableOpacity>);
});
}  
</View>

关于javascript - 使用 react native 中的节标题水平显示数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47387039/

相关文章:

javascript - 使用 npm start 传入 CLI 选项

javascript - 如何在 React JSX 中除最后一个元素之外的每个元素之后在 array.map 中添加一个逗号

android - React-Native 安卓 | JScode 在 APK 中的位置

react-native - react native navigationOptions调用函数错误

reactjs - 类型错误 : undefined is not an object (evaluating 'row[OBJECT_COLUMN_NAME] = key' ) | React Native

javascript - 在 Backbone 模型中将数组渲染为列表

javascript - 从数组添加值 - Jquery UI slider

javascript - 创建一个使用 Vue 和 Typescript 的 Meteor 项目

html - 在 React Native 中将 HTML 转换为图像

javascript - 不返回任何内容的 web 服务的推荐返回类型(使用 jquery 调用)