第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Material-UI表格中如何顯示動態(tài)數(shù)據(jù)?

Material-UI表格中如何顯示動態(tài)數(shù)據(jù)?

慕運維8079593 2022-12-22 14:36:50
我有返回一些表格數(shù)據(jù)的 API。我需要在表格中顯示這些數(shù)據(jù)。我不清楚如何實現(xiàn)這個目標。假設(shè)我想顯示字段id并name存儲在groups. 如何在 Material-UI 表中顯示它們?請在下面查看我當前的代碼。它不會拋出任何錯誤。但兩者都沒有顯示包含數(shù)據(jù)的表格。import '../../App.css';import React, { useEffect } from 'react'import { makeStyles } from '@material-ui/core/styles';import Grid from '@material-ui/core/Grid';import Table from '@material-ui/core/Table';import TableBody from '@material-ui/core/TableBody';import TableCell from '@material-ui/core/TableCell';import TableContainer from '@material-ui/core/TableContainer';import TableHead from '@material-ui/core/TableHead';import TableRow from '@material-ui/core/TableRow';import Paper from '@material-ui/core/Paper';import axios from 'axios'import config from '../../config/config.json';const useStyles = makeStyles((theme) => ({    root: {        width: '100%',    },    heading: {        fontSize: theme.typography.pxToRem(18),        fontWeight: theme.typography.fontWeightBold,    },    content: {        fontSize: theme.typography.pxToRem(14),        fontWeight: theme.typography.fontWeightRegular,        textAlign: "left",        marginTop: theme.spacing.unit*3,        marginLeft: theme.spacing.unit*3,        marginRight: theme.spacing.unit*3    },    table: {        minWidth: 650,    },    tableheader: {        fontWeight: theme.typography.fontWeightBold,        color: "#ffffff",        background: "#3f51b5"    },    tableCell: {        background: "#f50057"    },    button: {        fontSize: "12px",        minWidth: 100    },}));export function Main() {    const [groups, setGroup] = React.useState('');    const classes = useStyles();    const options = {        'headers': {            'Authorization': `Bearer ${localStorage.getItem('accessToken')}`        }    }
查看完整描述

3 回答

?
慕斯王

TA貢獻1864條經(jīng)驗 獲得超2個贊

我認為問題是你使用this.setState而不是setGroup


useEffect(() => {

    axios.get(config.api.url + '/api/test', options)

        .then( (groups) => {

            setGroup(groups)

        })

        .catch( (error) => {

            console.log(error);

        })

}, [])

改變你的地圖功能


{Object.keys(groups).map( (row, index) => (

  <TableRow key={index} selected="false">

    <TableCell>{row._id}</TableCell>

    <TableCell>{row.user_id}</TableCell>

  </TableRow>))}


import '../../App.css';

import React, { useEffect } from 'react'

import { makeStyles } from '@material-ui/core/styles';

import Grid from '@material-ui/core/Grid';

import Table from '@material-ui/core/Table';

import TableBody from '@material-ui/core/TableBody';

import TableCell from '@material-ui/core/TableCell';

import TableContainer from '@material-ui/core/TableContainer';

import TableHead from '@material-ui/core/TableHead';

import TableRow from '@material-ui/core/TableRow';

import Paper from '@material-ui/core/Paper';

import axios from 'axios'

import config from '../../config/config.json';


const useStyles = makeStyles((theme) => ({

    root: {

        width: '100%',

    },

    heading: {

        fontSize: theme.typography.pxToRem(18),

        fontWeight: theme.typography.fontWeightBold,

    },

    content: {

        fontSize: theme.typography.pxToRem(14),

        fontWeight: theme.typography.fontWeightRegular,

        textAlign: "left",

        marginTop: theme.spacing.unit*3,

        marginLeft: theme.spacing.unit*3,

        marginRight: theme.spacing.unit*3

    },

    table: {

        minWidth: 650,

    },

    tableheader: {

        fontWeight: theme.typography.fontWeightBold,

        color: "#ffffff",

        background: "#3f51b5"

    },

    tableCell: {

        background: "#f50057"

    },

    button: {

        fontSize: "12px",

        minWidth: 100

    },

}));



export function Main() {


    const [groups, setGroup] = React.useState([]);


    const classes = useStyles();


    const options = {

        'headers': {

            'Authorization': `Bearer ${localStorage.getItem('accessToken')}`

        }

    }


useEffect(() => {

        axios.get(config.api.url + '/api/test', options)

            .then( (groups) => {

                setGroup(groups.data.subtask)

                console.log(groups.data.subtask);

            })

            .catch( (error) => {

                console.log(error);

            })

    }, [])


return (

    <div className={classes.root}>


        <Grid container spacing={3}>

            <Grid item xs={12} className={classes.content}>

                <TableContainer component={Paper}>

                    <Table id="split_table" size="small">

                        <TableHead>

                        </TableHead>

                        <TableBody>

                                {Object.keys(groups).map( (item, index) => (

                                <TableRow key={index} selected="false">

                                    <TableCell>{item.user_id}</TableCell>

                                    <TableCell>{item.task_name}</TableCell>

                                </TableRow>))}

                        </TableBody>

                    </Table>

                </TableContainer>


            </Grid>    

        </Grid>

    </div>

)



查看完整回答
反對 回復(fù) 2022-12-22
?
慕碼人2483693

TA貢獻1860條經(jīng)驗 獲得超9個贊

我認為這是Object.keys(groups).


它不是 React 狀態(tài),所以它不會重新渲染?


您能否嘗試創(chuàng)建一個 groupKey 狀態(tài),然后在組更新時使用 Effect 更新狀態(tài)。


const [groupKey,setGroupKey] = useState([]);


useEffect(() => {

  setGroupKey(Object.keys(groups));

},[groups]);

在組件中,使用


{groupKey.map((item, index) => (

  <TableRow key={index} selected="false">

    <TableCell>{item.user_id}</TableCell>

    <TableCell>{item.task_name}</TableCell>

  </TableRow>))

}

你明白了。


查看完整回答
反對 回復(fù) 2022-12-22
?
一只甜甜圈

TA貢獻1836條經(jīng)驗 獲得超5個贊

像下面這樣的東西應(yīng)該有所幫助:

http://img1.sycdn.imooc.com//63a3fb4000014ed903680497.jpg

查看完整回答
反對 回復(fù) 2022-12-22
  • 3 回答
  • 0 關(guān)注
  • 179 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號