What is the best way to format my JSON data as a table on the screen?

I need to display my data in a table format at the bottom of the screen, as shown in the screenshot below.

Here is an example of the top part code:

This code snippet is also used for movies.

<View>
<Text key={item.symbol}>{item.symbol}<Text>
<Text key={item.open}>{item.open}<Text>
<Text key={item.close}>{item.close}<Text>

</View>

Currently, my screen looks like this:

https://i.stack.imgur.com/DAmCZ.png

I want to create a layout similar to the one shown below:

https://i.stack.imgur.com/iH1PM.png

The additional data displayed next to 'close' and 'high' values are retrieved from a JSON API.

My current code snippet is as follows:

let movie = ( 
  <View  style={styles.bottomdata}>
   <Text style={styles.text} key={item.name}>
      {item.name}
    </Text>
     <Text style={styles.text} key={item.open}>
      OPEN {item.open}
    </Text>
     <Text style={styles.text} key={item.close}>
      CLOSE{item.close}
    </Text>
   <Text style={styles.text} key={item.volumes}>
      VOLUME{item.volumes}
    </Text>
     <Text style={styles.text} key={item.low}>
     LOW {item.low}
    </Text>

    <Text style={styles.text} key={item.high}>
      HIGH{item.high}
    </Text>  
  </View>
)


  return (
    <TouchableWithoutFeedback onPress={Keyboard.dismiss}>
      <View style={styles.container}>
       <View>{movie}</View>   
      </View>
    </TouchableWithoutFeedback>

   );
}



const styles = StyleSheet.create({
  text: {
    color: "black",
    backgroundColor: "white",
  },
  bottomdata:
  {
      marginTop:400,
      backgroundColor:"black",
  }
});

The movie represents the data displayed on the screen. How can I design it to match the desired layout? The top section should show the symbol name, close value, and high value just like the bottom section. Any suggestions on how to achieve this interface design?

Answer №1

To properly arrange the elements on a page, utilize views and testing to create a structured layout similar to a table. The code snippet below demonstrates how to align items in a row.

JSX

  <View style={{ flex: 1 }}>
  <View
    style={{
      flexDirection: 'row',
      backgroundColor: 'black',
      alignItems: 'center',
    }}>
    <Text style={{ flex: 0.5, color: 'white' }}>{item.symbol}</Text>
    <Text style={{ flex: 0.5, color: 'white' }} key={item.open}>{item.open}</Text>
    <Text
      style={{
        flex: 0.5,
        color: 'white',
        backgroundColor: 'red',
        marginVertical: 5,
        paddingHorizontal: 10,
        borderRadius: 10,
      }}
      key={item.close}>
      {item.close}
    </Text>
  </View>

  <View style={styles.bottomdata}>
    <Text style={[styles.text, { alignSelf: 'center' }]} key={item.name}>
      {item.name}
    </Text>
    <View style={styles.row}>
      <View style={styles.rowItem}>
        <Text style={styles.text}>OPEN</Text>
        <Text style={styles.text}>{item.open} </Text>
      </View>
      <View style={styles.rowItem}>
        <Text style={styles.text}>CLOSE</Text>
        <Text style={styles.text}>{item.close}</Text>
      </View>
    </View>
    <View style={styles.row}>
      <View style={styles.rowItem}>
        <Text style={styles.text}>LOW</Text>
        <Text style={styles.text}>{item.low} </Text>
      </View>
      <View style={styles.rowItem}>
        <Text style={styles.text}>HIGH</Text>
        <Text style={styles.text}>{item.high}</Text>
      </View>
    </View>
    <View style={styles.rowItem}>
      <Text style={styles.text}>VOLUME</Text>
      <Text style={styles.text}>{item.volumes}</Text>
    </View>
  </View>
</View>

Styles:

const styles = StyleSheet.create({
  text: {
    color: 'white',
  },
  bottomdata: {
    marginTop: 'auto',
    backgroundColor: 'black',
  },
  row: {
    flexDirection: 'row',
    width: '100%',
  },
  rowItem: {
    width: '50%',
    justifyContent: 'space-between',
    flexDirection: 'row',
    paddingHorizontal: 5,
  },
});

Answer №2

To effectively display information, create divs with appropriate alignments and retrieve data for the relevant field.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

ASP.NET MVC does not automatically refresh when changes are made to the CSS file

Currently, I am diving into the world of Bootstrap 4 within an ASP.NET MVC project, exclusively making changes to the index.cshtml file in JetBrains Rider. However, I've encountered a strange issue that has me stumped. Each day, when I begin writing ...

"Embracing Flexbox for a fully responsive mobile experience

What's the best way to make this flexbox design responsive for mobile devices? I want the layout to look consistent across both desktop and mobile screens. The issue seems to be related to the responsiveness of the flexbox and the smaller size of mobi ...

What is the method for choosing multiple IDs using CSS?

Is there a way to target multiple IDs in CSS? For instance: #test_id_*{ } <div id="test_id_10"></div> <div id="test_id_11"></div> ...

Error: The object does not have the property createContext necessary to call React.createContext

I'm currently exploring the new React Context API in my app. I've also implemented flow for type checking. However, when I add // @flow to a file that contains the code: const MyContext = React.createContext() An error pops up stating: Cannot ...

Exploring the latest features in React V18 alongside Next.js V12.3.1 - Leveraging Suspense boundaries, StartTransition, and the innovative createRoot

I've been exploring how to make a react app work with next js. Currently, I'm utilizing this boilerplate app template, but with react v18 (and next v 12.3.1). In my attempt, I modified _app.tsx so it's now enclosed in a suspense boundary: ...

Convert an array of strings in C# into a JSON array using serialization

In my _Layout.cshtml file, I have the following code. The purpose is to populate some security items in my JavaScript code. LoggedIn and username work fine, but there seems to be an issue with how the roles are being displayed in the JavaScript. The Roles ...

reveal or conceal content on hover of mouse pointer

I am currently working on a section that functions like jQuery tabs, but instead of requiring a click to activate, I want it to work on mouse hover. The current implementation is somewhat buggy - when hovering over a specific section, it displays as expe ...

Achieve a centered content with a stretched background using Bootstrap 5

Having some trouble with aligning items in a div using align-items-stretch to stretch the background and align-items-center to vertically center the content. It seems that the alignment issue occurs when there's a very long title present. https://i.s ...

Envelop the phrases onto a fresh line

I'm having trouble getting the text inside a div to display correctly. When the text is too long, it just keeps stretching instead of breaking into new lines. How can I make sure that the content displays as a block within the div? Thank you. .box ...

Conflicting styles between Bootstrap and Formstack are causing radio buttons to appear only partially visible

Encountering a conflict between Bootstrap (4.1.3) and Formstack CSS when trying to embed a form in a website. The radio buttons are not fully visible, with the issue located in the "label" class ("fsOptionLabel"). Adjusting the line height provides a parti ...

What is the process for inspecting the element within a div using jsname with JS?

Struggling to inspect elements within a DIV without using xpath. // Assert.assertEquals("Digite uma senha",driver.findElement(By.xpath("//*[@id=\"view_container\"]/div/div/div[2]/div/div[1]/div/form/span/section/div/div/div[1]/div[2]/div[2] ...

What is the purpose of using square brackets in a CSS style declaration?

I've recently taken over the management of a website and I came across some unfamiliar CSS syntax. Has anyone encountered this type of syntax before? Can someone explain what the square brackets inside a style represent? Thank you, Rob .trigger-tx ...

"Using Material-UI to create two object literals inside a label for a checkbox

I am currently working with MUI components and have a set of checkboxes that I want to label using two object literals within my FormGroup. <FormGroup> {data.map((item, index) => { return ( <FormControlLabel key={index} ...

Is it possible to use JavaScript to forcefully transition a CSS keyframe animation to its end state?

One dilemma I am facing involves CSS keyframe animations triggered by scroll behavior. When users scroll too quickly, I would like JavaScript to help send some of the animations to their 'finished/final' state, especially since the animations bui ...

Utilize jQuery to transform array values into date objects

I am receiving an array from a .net controller. The values I am getting for dates are: /Date(1445256000000)/ and /Date(1445256900000)/ Instead of this, I need to convert these into proper date values. Now that I have an array of objects, I want to upda ...

Tips for assigning a class to an Angular accordion header when the panel is in the open state

I am currently utilizing the Angular UI Bootstrap accordion feature, which can be found here. <div ng-controller="AccordionDemoCtrl"> <div accordion close-others="oneAtATime"> <div accordion-group heading ...

Show the complete JSON data on the webpage

I'm trying to remember how to display all the data within an ng-repeat using $index, but it's not coming back to me. Usually, when I've done this in the past, it just displays everything in the HTML of the page like: array: [ { animal: "dog ...

Tips for efficiently applying the same styles to multiple elements in Material-UI with styled components

I have incorporated two material UI icons here, both with identical styles due to mui-styled components. Unfortunately, this prevents me from reusing the style efficiently. import { styled } from '@mui/material/styles'; import KeyboardArrowDown ...

What is the process for selecting the default option in a drop-down menu?

Is there a way to set the default value in a drop-down list using material-ui? I attempted to use displayEmpty="true", but it did not work. I want the first option, A, to be pre-selected so that it is visible to the user in the UI without them having to m ...

Learn how to dynamically disable a button using jQuery within the Materialize CSS framework

While attempting to disable a button in Materialize CSS using jQuery, I encountered an issue. Despite following the documentation found here, it seems that simply adding the 'disabled' class does not automatically disable the button as expected. ...