【问题标题】:how to add scrollbar in horizontal in datatable react native paper?如何在数据表反应原生纸中水平添加滚动条?
【发布时间】:2021-04-07 15:51:19
【问题描述】:

喂!我是反应原生开发和使用 expo-cli 的新手。我在我的应用程序中添加了一个 react-native-paper 数据表,它工作正常,但我有很多字段我想在水平方向滚动屏幕,但我不明白该由谁来做。请向我建议我可以为这个问题做些什么。

import React from 'react';
import { StyleSheet, Text, View, ScrollView, TextInput, Button, } from 'react-native';
import { Card } from "react-native-shadow-cards";
import {DataTable} from  "react-native-paper"


const AllUsers = () =>{
    return(
        <View>
            <View style={styles.headSection}>
                <Text style={styles.titleHeading}>All Users</Text>
            </View>
            <ScrollView horizontal>
            <DataTable style={styles.table} >
                <DataTable.Header>
                    <DataTable.Title style={styles.header} > <Text style={styles.tableHeading}>Id</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} > <Text style={styles.tableHeading} >Username</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} > <Text style={styles.tableHeading} >First Name</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} > <Text style={styles.tableHeading} >Last Name</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} >  <Text style={styles.tableHeading} >Email</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} > <Text style={styles.tableHeading} >First Name</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} > <Text style={styles.tableHeading} >Last Name</Text></DataTable.Title>
                    <DataTable.Title style={styles.header} >  <Text style={styles.tableHeading} >Email</Text></DataTable.Title>
                    
                </DataTable.Header>
                <DataTable.Row>
                    <DataTable.Cell>1.</DataTable.Cell>
                    <DataTable.Cell>adiljaz02</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                </DataTable.Row>
                <DataTable.Row>
                    <DataTable.Cell>1.</DataTable.Cell>
                    <DataTable.Cell>adiljaz02</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                </DataTable.Row>
                <DataTable.Row>
                    <DataTable.Cell>1.</DataTable.Cell>
                    <DataTable.Cell>adiljaz02</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                </DataTable.Row>
                <DataTable.Pagination
                    page={1}
                    numberOfPages={3}
                    onPageChange={(page) => { console.log(page); }}
                    label="1-2 of 6"
                />
            </DataTable>
            </ScrollView>
        </View>
    )    
}
const styles = StyleSheet.create({
    table:{
        
    },
    headSection:{
        borderBottomWidth:2,
        borderColor:'black',
        paddingBottom:15,
        
    },
    titleHeading:{
        marginTop:50,
        fontWeight:'bold',
        marginHorizontal:167,
    },
    tableHeading:{
        fontWeight:'bold',
        color:'black',
    },
    header:{
        paddingLeft:0,
    },
});
export default AllUsers;

上面的代码包含三个字段,但我想添加更多字段。但想在水平方向滚动。

【问题讨论】:

    标签: javascript android reactjs react-native react-native-paper


    【解决方案1】:

    要启用水平滚动,您可以将要滚动的内容放在 ScrollView 组件中,并以水平作为道具,如下所示:

    <ScrollView horizontal>
    <DataTable style={styles.table} >
                    <DataTable.Header>
                        <DataTable.Title style={styles.header} > <Text style={styles.tableHeading}>Id</Text></DataTable.Title>
                        <DataTable.Title style={styles.header} > <Text style={styles.tableHeading} >Username</Text></DataTable.Title>
                        <DataTable.Title style={styles.header} >  <Text style={styles.tableHeading} >Email</Text></DataTable.Title>
                    </DataTable.Header>
                    <DataTable.Row>
                        <DataTable.Cell>1.</DataTable.Cell>
                        <DataTable.Cell>adiljaz02</DataTable.Cell>
                        <DataTable.Cell>adilijaz.02@gmail.com</DataTable.Cell>
                    </DataTable.Row>
                    <DataTable.Row>
                        <DataTable.Cell>2.</DataTable.Cell>
                        <DataTable.Cell>adil09</DataTable.Cell>
                        <DataTable.Cell>adili09@gmail.com</DataTable.Cell>
                    </DataTable.Row>
                    <DataTable.Pagination
                        page={1}
                        numberOfPages={3}
                        onPageChange={(page) => { console.log(page); }}
                        label="1-2 of 6"
                    />
                </DataTable>
    </ScrollView>
    

    【讨论】:

    • 我编辑我的问题看看数据是如何显示的
    • @AdilIjaz 这一切都在你的造型中。您可以尝试删除所有样式并查看它是否看起来更好吗?样式.header & styles.tableHeading
    • 我删除了所有样式,但看起来仍然是乱七八糟的内容
    • 如果我在没有任何样式的情况下删除了看起来很棒的滚动视图表,但是当我添加滚动视图数据时看起来很乱。但我也想添加滚动选项。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-29
    • 2016-08-05
    • 2016-08-09
    • 2018-07-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多