1

I have a normal table in react js that is generated like so (this is just an extract for generating the the td elements other code is a class that is called CompanyList that is a react component that renders the table;

import React from 'react';
import { connect } from 'react-redux'
import { bindActionCreators } from 'redux'
import { Line } from 'react-chartjs-2';
import { actionCreators as companiesActionCreators } from '../../reducers/companies'
import { getSelectedAndFilteredCompanies, getChartData } from '../../selectors/companies';
import DropBox from '../DropBox'
import { getRevenueSelectedRange, getTotalAssetsSelectedRange } from '../../selectors/companies'
import 'react-bootstrap-table-next/dist/react-bootstrap-table2.min.css';
import BootstrapTable from 'react-bootstrap-table-next';

const Company = ({ company, selectCompany, deselectCompany }) => (
  <tr>
    <td><input type='checkbox' checked={company.selected} onChange={() => company.selected ? deselectCompany(company.id) : selectCompany(company.id)} /></td>
    <td>{ company['Company Name'] }</td>
    <td>{ company['Revenue bucket'] }</td>
    <td>{ company['Current Yield'] }</td>
    <td>{ company['Sector'] }</td>
    <td>{ company['Revenue bucket'] }</td>
    <td>{ company['Revenue 2007'] }</td>
    <td>{ company['Company Name'] }</td>
    <td>{ company['Company Name'] }</td>
  </tr>
);

However I want to convert this table to use the react js plugin react-bootstrap-table-2 but after reading the docs I am unsure whether my products array should be like this;

products = [{ 'Name' : { company['Company Name'] } , 'checkbox' : <input type='checkbox' checked={company.selected} onChange={() => company.selected ? deselectCompany(company.id) : selectCompany(company.id)} /> }]

1 Answer 1

2

Here is an example how your table will look like build with react-bootstrap-table-2:

import BootstrapTable from 'react-bootstrap-table-next';

class Component extends React.Component {
    renderColumns () {
        return [
            { dataField: 'id', text: 'Id', hidden: true },
            { dataField: 'isSelected', text: 'Selected' },
            { dataField: 'companyName', text: 'Company name' },
            { dataField: 'revenueBucket', text: 'Revenue bucket' },
            { dataField: 'currentYield', text: 'Current Yield' },
            { dataField: 'sector', text: 'Sector' },
        ]
    }

    renderRows () {
        const { companies, deselectCompany, selectCompany} = this.props 
        companies.map(company => {
            return {
                id: company.id,
                isSelected: <input type='checkbox' checked={company.isSelected} onChange={() => company.isSelected ? deselectCompany(company.id) : selectCompany(company.id)} />,
                companyName: company['Company Name'],
                revenueBucket: company['Revenue bucket'],
                currentYield: company['Current Yield'],
                sector: company['Sector']
            }
        })
    }

    render () {
        return <BootstrapTable
            keyField='id'
            data={ this.renderRows(companies) }
            columns={ this.renderColumns() }
        />
    }

}

Please have in mind that the column dataField and the row key have to be equal. And one more advice is to separate columns and rows rendering like me in order to avoid confusion.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.