react-data-table-component icon indicating copy to clipboard operation
react-data-table-component copied to clipboard

Selected props doesn't work

Open smcalilly opened this issue 5 years ago • 7 comments

The documentation says that the DataTable component can be passed a Selected props but this doesn't work with my code. I've scanned through the source code and I haven't been able to find this props. I might be missing it in the source code, but does this props argument still exist?

Also, is there anyway to get an individual row when it's selected? I've only been able to return the entire array of rows, which requires me to iterate through the entire array to get any newly selected row. I can't find anything in the source code that looks like it can do this, nor have I been able to get the selectableRowSelected props to work. The callback executes on every single row whenever the table loads, instead of whenever the row is selected.

Here is the code I'm trying:

import React from 'react'
import DataTable from 'react-data-table-component'

const PortfolioTable = ({ projects = [], onUpdatePortfolio }) => {
  const columns = React.useMemo(
    () => [
      {
        name: 'Project Description',
        selector: 'projectDescription', 
        sortable: true,
        maxWidth: '550px'
      },
      {
        name: 'Total Score',
        selector: 'score',
        sortable: true,
        maxWidth: '50px',
      },
      {
        name: 'Total Budget',
        selector: 'budget',
        sortable: true,
        maxWidth: '75px'
      }
    ], []
  )

  const data = React.useMemo(
    () => projects.map((project) => {
      return {
        projectDescription: project.project_description || 'No description available.',
        score: project.score || 'N/A',
        budget: project.budget || 0,
        key: project.key,
        name: project.name,
        zone: project.zone
      }
    }), [projects])

  const handleChange = (event) => {
    console.log('Selected', event)
  }

  return (
    <>
      <div className="table-responsive">
        <DataTable
          columns={columns}
          data={data}
          pagination
          keyField='key'
          selectableRows
          Selected={(row) => handleChange(row)}
          onSelectedRowsChange={(rows) => onUpdatePortfolio(rows.selectedRows)}
        />  
      </div>
    </>
  )
}

export default PortfolioTable

smcalilly avatar Dec 10 '20 15:12 smcalilly

There is no Selected prop. But yea, you would use the rows returned from onSelectedRowsChange and have to iterate through them. Here is an example, albeit as a class component:

https://jbetancur.github.io/react-data-table-component/?path=/story/material-ui--override-default-components

If you are looking for the last selected row then has not been implemented yet, although it would be easy to do if that's what you are looking for.

jbetancur avatar Dec 10 '20 23:12 jbetancur

The README documentation is a bit misleading then, if there is no Selected prop. I've been trying to get this example from your documentation to work: https://github.com/jbetancur/react-data-table-component#selectable-rows. The misleading code:

<DataTable
      title="Arnold Movies"
      columns={columns}
      data={data}
      selectableRows // add for checkbox selection
      Clicked
      Selected={handleChange}
      />

smcalilly avatar Dec 11 '20 15:12 smcalilly

Oh wow, that appears to be a typo that made it into a pr commit. Apologies for that, it should not be there.

I'll tag this issue to get the documentation fixed.

jbetancur avatar Dec 12 '20 00:12 jbetancur

"onSelectedRowsChange" method is correct one which you can use.

erelcolak avatar Jan 10 '21 21:01 erelcolak

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Feb 13 '21 07:02 stale[bot]

Not sure this is stale because the documentation still has the error.

smcalilly avatar Feb 15 '21 22:02 smcalilly

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

stale[bot] avatar Mar 19 '21 22:03 stale[bot]