【问题标题】:Table is not showing data accordingly in Reactjs表格没有在 Reactjs 中相应地显示数据
【发布时间】:2020-05-20 20:55:43
【问题描述】:

我正在尝试将 API 的响应显示到 react-semantic-ui 表中。在设置了获取数据的状态后,我需要将一阶键作为列名,然后将二阶键作为行名,二阶键的值将显示在表体中。我已经做到了这一点,这导致每行中的单元格数量增加一倍,同时使用相同的数据设置所有单元格值。

class NonProtectedFeatureBias extends Component {
    constructor(props){
        super(props);
        this.state = {       
            staticPostData:{
                dataset_id: 1
            },
            tableData:{},
            tableColumnNames:[],
        };
    }
    renderKeys (data) {
        let tcolumn = [];
        for(let i=0; i<Object.keys(data).length;i++){
            const Obj1 = Object.keys(data)[i];
            //console.log(Obj1)
            tcolumn.push(Obj1);

        }
        let col = tcolumn.map(item=>{
            return <Table.HeaderCell>{item}</Table.HeaderCell>
        })
        return col
    }
    renderValues (data) {
        let row=null;
        for(let i=0; i<Object.keys(data).length;i++){
            const Obj1 = Object.values(data)[i];

            let trows = [];
            const Obj2 = Object.keys(Obj1).map(item =>{
                trows.push(item);
            });

            let tvalues =[];
            const Obj3 = Object.values(Obj1).map(item =>{
                tvalues.push(item);
                console.log(item)
            });
            row = trows.map(item=>{
                return(
                    <Table.Row  className="cell-current">
                        <Table.Cell className="cell-width-single">{item}</Table.Cell>
                        {tvalues.map(val =>{
                            return <Table.Cell selectable>{val}</Table.Cell>
                            //console.log(val)
                        })}
                    </Table.Row>
                )
            })
        }

        return row
    }

    componentDidMount() {
        this.fetchData();
    }
    fetchData(){
        axios.post('http://localhost:5000/GetProxyTable', this.state.staticPostData)
         .then((response) =>{

            var count = Object.keys(response.data).length;
            this.setState({tableData:response.data})
            console.log(response.data)
        });
    }
    render(){
        return ( 
            <Container style={{height:"250px", backgroundColor:""}}>
                <Table definition >
                    <Table.Header>
                    <Table.Row className="cell-with-no-padding">
                        <Table.HeaderCell />
                        {this.renderKeys(this.state.tableData)}
                    </Table.Row>
                    </Table.Header>

                    <Table.Body>
                        {this.renderValues(this.state.tableData)}
                    </Table.Body>
                </Table>
            </Container>
        );
    }
}
export default NonProtectedFeatureBias;

这是我从 API 得到的响应

这就是我想要实现的,

这就是我现在得到的,

这是实现我的目标的正确方法吗?!我希望有一个优化的方法来做到这一点。而且我还需要能够单击一个单元格并获取相关的行和列名称。任何建议或意见将不胜感激。

【问题讨论】:

  • renderKeys 可以简化为Object.keys(data).map(item=&gt;(&lt;Table.HeaderCell&gt;{item}&lt;/Table.HeaderCell&gt;)),例如。 map 将结果推送到数组中,因此您可以删除样板。
  • 谢谢!这确实简化了 renderKeys 方法。请你看看另一个,renderValues 吗?
  • 先自己动手,确保你明白我做了什么。

标签: html css reactjs semantic-ui-react


【解决方案1】:

您的renderValues 是通过列而不是行映射。另外,您将每个假行设置为相同的值,因此所有“行”都是相等的。最后,返回最后一个,也就是最后一列的值。

您可以尝试下面的代码,在呈现行之前先将列映射到行对象:

renderValues (data) {
  const rows = {}
  Object.values(data).forEach(col => {
    for (let [key, value] of Object.entries(col)) {
      rows[key] = rows[key] ? [...rows[key], value] : [value]
    }
  })

  return Object.entries(rows).map(([item, values]) => (
    <Table.Row className="cell-current">
      <Table.Cell className="cell-width-single">{item}</Table.Cell>
        { values.map(val => <Table.Cell selectable>{val}</Table.Cell> ) }
    </Table.Row>
  ))
}

【讨论】:

  • 就像一个魅力!现在我明白了我的代码问题所在。 :D 非常感谢(和平)
  • 目前我可以使用 onClick={()=>alert(item)}} 轻松获取单元格的行名。很明显,如果我在这个 alert() 中只使用参数“data”,这将获得所有的列名。现在,请至少向我建议如何从当前代码中获取列名以及您的代码。
  • 尝试将数组添加到行,而不是像:rows[key] = rows[key] ? [...rows[key], [col, value] ] : [ [col, value] ]。现在,一旦您遍历值以创建 Table.Cellval 将是一个数组,您可以执行 values.map(([col, value]) =&gt; ...。你也可以存储为一个对象,而不是像{ [col]: value } 这样的数组,但是在映射val 将是一个对象,你必须做Object.values(val) 来提取它的内容。
  • 表格行和表格单元格应该有key 属性吧?
猜你喜欢
  • 2021-11-27
  • 2018-09-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-08
  • 2021-09-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多