r/reactjs •

[deleted by user]

[deleted] — view deleted post

0 Upvotes

255 comments sorted by

View all comments

1

u/[deleted] Sep 20 '20

React noob here trying to figure out how to get a table with sortable columns working while importing the table's data from an API. Right now I'm using MD bootstrap (MDBDataTableV5) and it's displaying the data properly but I'm not able to sort the data when I click on the column name. Here's my code:

import { MDBDataTableV5 } from 'mdbreact';
       const Players = ({ players }) => {

  const data = {
columns: [
  {
    label: 'Name',
    field: 'player_name',
    sort: 'asc',
    width: 150
  },
  {
    label: 'Goals',
    field: 'goals_season',
    // sort: 'asc',
    width: 270
  },
  {
    label: 'Assists',
    field: 'assists_season',
    // sort: 'asc',
    width: 200
  },
  {
    label: 'Games Played',
    field: 'games',
    // sort: 'asc',
    width: 100
  }
],
rows: []

};

  const datatableProps = {...data, rows:players};
  return <MDBDataTableV5
  
  players={players}
  data={datatableProps}
  />;

Any help would be appreciated.

1

u/Awnry_Abe Sep 21 '20

I'm surprised that MDBDataTableV5 takes a "players" prop. I don't know anything about that lib, but I wonder if it the MDBTableEditor component that provides table sorting?