javascript - Display data json to table in react - Stack Overflow

I'm going to build a web with react js. I'm very new to react so I have a problem to make CRU

I'm going to build a web with react js. I'm very new to react so I have a problem to make CRUD. For the first I want to display some data json to table. I have file .js that named keranjang.js. it contains jsx to display the table. and I have another named barang.js and want to fill it with the method named tampildata() used to hold json data for example {"nama_barang" : "bolu", "harga" : "10000"}. How I write the method? and how do I call the method and data to display that data into the existing table in keranjang.js? Hope everyone helps me.

I'm going to build a web with react js. I'm very new to react so I have a problem to make CRUD. For the first I want to display some data json to table. I have file .js that named keranjang.js. it contains jsx to display the table. and I have another named barang.js and want to fill it with the method named tampildata() used to hold json data for example {"nama_barang" : "bolu", "harga" : "10000"}. How I write the method? and how do I call the method and data to display that data into the existing table in keranjang.js? Hope everyone helps me.

Share Improve this question edited Dec 7, 2017 at 15:52 Eddie 26.9k6 gold badges38 silver badges59 bronze badges asked Dec 7, 2017 at 14:58 Riyan MaulanaRiyan Maulana 551 silver badge5 bronze badges 3
  • Can you post some code showing what you've already tried? – T Porter Commented Dec 7, 2017 at 15:18
  • You are trying to access method that is present in one js file returning json and want to display that json in another js file that having table ui. Correct? – Elumalai Kaliyaperumal Commented Dec 7, 2017 at 15:25
  • yes you're right bro – Riyan Maulana Commented Dec 8, 2017 at 14:57
Add a ment  | 

1 Answer 1

Reset to default 4

I am assuming that you trying to call external file's method in current ponent. In your barang.js file export your function that holds json data like

export function tampildata() {
    return [{ "firstname": "first", "lastname": "f"}, {"firstname": "second", "lastname": "l"}];
}

or

export default {
  tampildata() {
    return [{ "firstname": "first", "lastname": "f"}, {"firstname": "second", "lastname": "l"}];
  }
};

Then in your keranjang.js file import your tampildata method and call that method in ponentDidMount and set state like below

import React from 'react';
import ReactDOM from 'react-dom';
import { tampildata } from 'barang';

class TableComponent extends React.Component {
    constructor(props) {
        super(props)
        this.state = {
            json: []
        }
    }

    ponentDidMount() {
        this.setState((prevState) => {
            return {
                json: tampildata()
            }
        })
    }

    render() {
        return (
            <div>
                <table>
                    <thead>
                        <th>First Name</th>
                        <th>Last Name</th>
                    </thead>
                    <tbody>
                        {this.state.json.map((data, i) => {
                            return (
                                <tr key={i}>
                                    <td>{data.firstname}</td>
                                    <td>{data.lastname}</td>
                                </tr>
                            )
                        })}
                    </tbody>
                </table>
            </div>
        )
    }
}


ReactDOM.render(
  <TableComponent />, 
  document.getElementById("app")
);

Here is the working jsfiddle. Hope this will helps you!

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745099752a4611203.html

相关推荐

  • javascript - Display data json to table in react - Stack Overflow

    I'm going to build a web with react js. I'm very new to react so I have a problem to make CRU

    11小时前
    30

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信