Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setting columns dynamically #345

Closed
chanakyabhardwajj opened this issue Sep 18, 2023 · 1 comment
Closed

Setting columns dynamically #345

chanakyabhardwajj opened this issue Sep 18, 2023 · 1 comment

Comments

@chanakyabhardwajj
Copy link

chanakyabhardwajj commented Sep 18, 2023

Great work on this library! Thanks for your work!

I wanted to ask if it is possible to use it when the columns are not known beforehand.
For example, if I want to parse a CSV file and render it as a table, how can I do that?

The column names are usually the first row of the CSV file followed by the data.
I am able to parse the CSV file and store the columns and the rows in a Context.
But when I try to render it, it just renders an empty box. No columns or rows.

My setup is like this:

App.tsx looks like:

// `ctxValue` contains `firstRow` & `tableData` 
<AppContext.Provider value={ctxValue}>
  <MyTable />
</AppContext.Provider>

And my MyTable.tsx looks like:

import 'ka-table/style.css'

import React, { useContext, useState } from 'react'
import { AppContext } from '../appContext'
import { DataType, Table } from 'ka-table'
import { Column } from 'ka-table/models'

const MyTable: React.FC = () => {
  const context = useContext(AppContext)
  const columns =
    context?.firstRow.map((colName) => {
      return {
        key: colName,
        title: colName,
        dataType: DataType.String
      }
    }) || []
 
  const [tableProps, changeTableProps] = useState({
    data: context?.tableData,
    rowKeyField: 'id',
    columns: columns
  })

  return (
    <Table
      {...tableProps}
    />
  )
}

export default MyTable

However, this does not render anything. I just see an empty box like this:

Image

All the demos, assume the columns are known beforehand e.g. this json demo. Is there an example of loading dynamic data where the columns are not known earlier?

I would really appreciate your help here! Thanks!

@chanakyabhardwajj
Copy link
Author

Never mind, I figured it out. I was not re-rendering the table correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant