javascript - 通过单击具有数字和字符串值的列的表标题对 React 表进行排序

标签 javascript reactjs

我在通过单击具有字符串值的列的列标题对我的 React 表进行排序时遇到问题,但是对于具有数值的列,一切正常。我认为我在 sortBySymbolHandler 方法中做错了。我想我检查字符串:this.state.directionSymbol[key] === 'asc'? a[key] - b[key] : b[key] - a[key] 是错误的。

    import React, { Component } from 'react';
    import CoinTable from '../components/CoinTable/CoinTable';
    import data from '../components/Data/Data.json';

    class App extends Component {
        constructor(props) {
            super(props)
            this.state = {
                data: data,
                direction: {
                    price_usd: 'asc'
                },
                directionSymbol: {
                    symbol: 'asc'
                }
            }
        }

        sortBySymbolHandler = (key) => {
             this.setState({
                data: data.sort((a, b) => (
                    this.state.directionSymbol[key] === 'asc'
                    ? a[key] - b[key]
                    : b[key] - a[key]
                )),

                directionSymbol: {
                    [key]: this.state.directionSymbol[key] === 'asc'
                    ? 'desc'
                    : 'asc'
                }
            })
        }

        sortByPriceHandler = (key) => {
            this.setState({
                data: data.sort((a, b) => (
                    this.state.direction[key] === 'asc'
                    ? 
                     parseFloat(a[key]) - parseFloat(b[key])
                    : parseFloat(b[key]) - parseFloat(a[key])
                )),

                direction: {
                    [key]: this.state.direction[key] === 'asc'
                    ? 'desc'
                    : 'asc'
                }
            })
        }

        render() {
            return (
                <div className="App">
                    <div className="container-fluid">
                        <div className="container">
                            <CoinTable 
                                data={this.state.data} 
                                sortBySymbol={this.sortBySymbolHandler}
                                sortByPrice={this.sortByPriceHandler}
                            />
                        </div>
                    </div>
                </div>
            );
        }
    }

    export default App;

Data.json 格式如下:

    [
        {
            "id": "bitcoin", 
            "name": "Bitcoin", 
            "symbol": "BTC", 
            "rank": "1", 
            "price_usd": "10406.5", 
            "price_btc": "1.0", 
            "24h_volume_usd": "9766700000.0", 
            "market_cap_usd": "175053324790", 
            "available_supply": "16821537.0", 
            "total_supply": "16821537.0", 
            "max_supply": "21000000.0", 
            "percent_change_1h": "-0.04", 
            "percent_change_24h": "-3.39", 
            "percent_change_7d": "-12.62", 
            "last_updated": "1516718960"
        }, 
        {
            "id": "ethereum", 
            "name": "Ethereum", 
            "symbol": "ETH", 
            "rank": "2", 
            "price_usd": "947.841", 
            "price_btc": "0.0917641", 
            "24h_volume_usd": "3615420000.0", 
            "market_cap_usd": "92093645501.0", 
            "available_supply": "97161492.0", 
            "total_supply": "97161492.0", 
            "max_supply": null, 
            "percent_change_1h": "0.3", 
            "percent_change_24h": "-4.04", 
            "percent_change_7d": "-13.22", 
            "last_updated": "1516718952"
        }, 
        {
            "id": "ripple", 
            "name": "Ripple", 
            "symbol": "XRP", 
            "rank": "3", 
            "price_usd": "1.27202", 
            "price_btc": "0.00012315", 
            "24h_volume_usd": "2597960000.0", 
            "market_cap_usd": "49276964438.0", 
            "available_supply": "38739142811.0", 
            "total_supply": "99993093880.0", 
            "max_supply": "100000000000", 
            "percent_change_1h": "0.83", 
            "percent_change_24h": "3.49", 
            "percent_change_7d": "-5.9", 
            "last_updated": "1516718941"
        } 
]

CoinTable.js 是这样的:

import React from 'react';;
import TableRow from './TableRow/TableRow';

const coinTable = (props) => (
    <table>
        <thead>
            <tr>
                <th>#</th>
                <th>Name</th>
                <th><button onClick={() => props.sortBySymbol('symbol')}>Symbol</button></th>
                <th><button onClick={() => props.sortByPrice('price_usd')}>Price</button></th>
                <th>%/hour</th>
                <th>%/day</th>
                <th>%/week</th>
            </tr>
        </thead>
        <tbody>
            {
                props.data.map(row => {
                    const price = row.price_usd;
                    const formattedPrice = parseFloat(price).toFixed(2);
                    return (
                        <TableRow key={row.rank} row={row} formattedPrice={formattedPrice} />
                    )
                })
            }
        </tbody>
    </table>
);

export default coinTable;

有人可以帮忙吗?

最佳答案

你不能从一个字符串中减去另一个字符串。

而不是做:

 data: data.sort((a, b) => (
     this.state.directionSymbol[key] === 'asc'
         ? a[key] - b[key]
         : b[key] - a[key]
 )),

做:

 data: data.sort((a, b) => {
     const asc = this.state.directionSymbol[key] === 'asc';
     if (a[key] < b[key]) {
         return asc ? -1 : 1;
     } else if (a[key] > b[key]) {
         return asc ? 1 : -1;
     } else {
         return 0;
     }
 )),

关于javascript - 通过单击具有数字和字符串值的列的表标题对 React 表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53345196/

相关文章:

javascript - $(window).on ("load") 在 postRender 中未触发

javascript - 为什么我在 for...of 循环中收到 'no-unused-vars' 警告,我该如何解决?

javascript - 使用 react-hooks 在每个渲染器上创建处理程序的性能损失

javascript - 如何从远程手机获取坐标

javascript - 使用 useState 更新对象

javascript - 简单的层次结构但仍然警告 "Any use of a keyed object should be wrapped in React.addons.createFragment(object)"

reactjs - 在mapDispatchToProps中使用thunk

javascript - 阅读时刻完整时区名称

javascript - 返回页面加载?

javascript - 输入元素模式属性的行为不符合正则表达式