Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
448 views
in Technique[技术] by (71.8m points)

json - How to display data using a for loop in Timeline (Material-UI) - React

I have a data in the form of json as given below:

[
    {
        "module_id": 2,
        "module_type": "Instructional",
        "module_name": "Introduction and Course Overview",
        "duration": 30,
        "course": {
            "course_id": 1,
            "course_name": "AWS"
        }
    },
    {
        "module_id": 1,
        "module_type": "Instructional",
        "module_name": "Quiz",
        "duration": 20,
        "course": {
            "course_id": 1,
            "course_name": "AWS"
        }
    }
]

Now, I have the following code in react and I want to render it on timeline: Timeline Code (Material - UI)

return(
    <Timeline align="alternate">
      <TimelineItem>
      <TimelineSeparator>
      <TimelineDot color="primary">
        <LaptopMacIcon />
      </TimelineDot>
      <TimelineConnector />
    </TimelineSeparator>
    <TimelineContent>
      <Paper elevation={3} className={classes.paper}>
        <Typography variant="h6" component="h1">
          **Here, I want to display the module_name i.e Introduction and Course Overview and Quiz**
        </Typography>
      </Paper>
    </TimelineContent>
  </TimelineItem>
  <TimelineItem>
)

Please help on how to do the same, I want the timeline to have the laptop icon with something like this

                                Introduction and Course Overview
                               |
                               |
                            Quiz

Thank you in advance :)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to map your json to <TimelineItem />, which you can do by writting a function as below

const getModules = () => {
    return res.map((r, i) => {
      const className = i % 2 === 0 ? "custom-even-class" : "custom-odd-class";
      return (
        <TimelineItem classes={{ alignAlternate: className }} key={r.module_id}>
          <TimelineSeparator>
            <TimelineDot />
            <TimelineConnector />
          </TimelineSeparator>
          <TimelineContent>{r.module_name}</TimelineContent>
        </TimelineItem>
      );
    });
  };

This function will return list of <TimelineItem /> and to render it you can call this function within your render function, as shown below

return (
    <Timeline align="alternate">
      <TimelineItem>
        <TimelineSeparator>
          <TimelineDot>
            <LaptopMacIcon />
          </TimelineDot>
          <TimelineConnector />
        </TimelineSeparator>
        <TimelineContent></TimelineContent>
      </TimelineItem>

      {getModules()}
    </Timeline>
  );

After this you will see timeline like this

enter image description here

Now as you mentioned you want your timeline to look like this, with starting Item as laptop icon, we have to use alternate timeline, and by default implementation all even item will be displayed in left and odd items will be displayed in right of the timeline, as you can see from above image.

                            Introduction and Course Overview
                           |
                           |
                        Quiz

So to modify it as per your question, I implemented a hacky way to modify the default CSS and made it look like as below

enter image description here

you can check the final code here https://codesandbox.io/s/recursing-fire-nbpmu?file=/src/styles.css


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.7k users

...