Search code examples
javascriptandroidreactjsreact-nativereact-native-paper

how to add scrollbar in horizontal in datatable react native paper?


Hy! I'm new in react native development and using expo-cli. I add a react-native-paper datatable in my app its working fine but I have many fields I want to scroll a screen in the horizontal direction but I can't understand who to do it. Kindly suggest to me what I can do for this problem.

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>[email protected]</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>[email protected]</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>[email protected]</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>[email protected]</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>[email protected]</DataTable.Cell>
                    <DataTable.Cell>Adil</DataTable.Cell>
                    <DataTable.Cell>IJaz</DataTable.Cell>
                    <DataTable.Cell>[email protected]</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;

Here's above code is having three fields but I want to add more fields. but want to scroll in the horizontal direction. Looks that


Solution

  • To enable horizontal scrolling, you can put the content you want to scroll in a ScrollView component with horizontal as a prop like this:

    <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>[email protected]</DataTable.Cell>
                    </DataTable.Row>
                    <DataTable.Row>
                        <DataTable.Cell>2.</DataTable.Cell>
                        <DataTable.Cell>adil09</DataTable.Cell>
                        <DataTable.Cell>[email protected]</DataTable.Cell>
                    </DataTable.Row>
                    <DataTable.Pagination
                        page={1}
                        numberOfPages={3}
                        onPageChange={(page) => { console.log(page); }}
                        label="1-2 of 6"
                    />
                </DataTable>
    </ScrollView>