Hacktoberfest 2026: những issue maintainer đã đánh dấu cho tháng Mười, đang mở và phù hợp người mới. Xem issue Hacktoberfest

selections not working if columns pulled from state?

Đang mở
#418 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Đánh giá

Độ khó
3/5
Thời gian dự kiến
1-2 ngày
Mức phù hợp với người mới
30/100
Loại issue
Lỗi
Độ rõ ràng
Khá rõ ràng
Mức độ hoạt động
Đình trệ
Công nghệ
javascript, react
Lĩnh vực
frontend

Hướng nghiên cứu

Tái hiện ví dụ MultiSelectTable được cung cấp, so sánh các columns được tạo trong useEffect với mảng columns inline được truyền vào BaseTable. Theo dõi cách các columns dựa trên state và cellRenderer quan sát selectedRows, sau đó xác minh rằng việc chọn một checkbox cập nhật cả selectedRows lẫn trạng thái checkbox được render.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

In my application, I need to dynamically create the columns and then have the pull the columns from the state. However, when I do this, the code to update the row state does not work. Below is a simplified version, showing the issue. If you run this code, when you select the checkbox, the state does not get updated correctly.

If, however, I change BaseTable and set the columns= the actual array, then the selection process works.

Code that does not work

/* eslint-disable */
import React, { useState,useEffect } from 'react';
import BaseTable, { Column } from 'react-base-table';
import 'react-base-table/styles.css';

const MultiSelectTable = () => {
  const [selectedRows, setSelectedRows] = useState([]);
  const [columns, setColumns] = useState([])


  useEffect(()=>{
    setColumns(
      [
        {
          key:"checkbox",
          title:"",
          width:50,
          selectedRowKeys:{selectedRows},
          cellRenderer:({ rowData }) => (
                <input
                  type="checkbox"
                  checked={selectedRows.includes(rowData.id)}
                  onChange={() => handleRowSelection({ rowData })}
                  />
            )}
          ,
        {key:"name", dataKey:"name", title:"Name", width:200},
        {key:"age", dataKey:"age", title:"Age", width:100}
        ]
    )
  },[])

  const handleRowSelection = ({ rowData }) => {
    const selectedRowKeys = [...selectedRows];
    const rowIndex = selectedRowKeys.indexOf(rowData.id);

    if (rowIndex !== -1) {
      selectedRowKeys.splice(rowIndex, 1);
    } else {
      selectedRowKeys.push(rowData.id);
    }

    setSelectedRows(selectedRowKeys);
    console.log(selectedRowKeys)
  };



  const data = [
    { id: 1, name: 'John Doe', age: 25 },
    { id: 2, name: 'Jane Smith', age: 30 },
    { id: 3, name: 'Bob Johnson', age: 35 },
    // Add more data as needed
  ];

  return (
    <BaseTable
      width={600}
      height={400}
      data={data}
      columns={columns}
      rowKey="id" 
    />
   
  );
};

export default MultiSelectTable;

Code that works:

/* eslint-disable */
import React, { useState,useEffect } from 'react';
import BaseTable, { Column } from 'react-base-table';
import 'react-base-table/styles.css';

const MultiSelectTable = () => {
  const [selectedRows, setSelectedRows] = useState([]);

  const handleRowSelection = ({ rowData }) => {
    const selectedRowKeys = [...selectedRows];
    const rowIndex = selectedRowKeys.indexOf(rowData.id);

    if (rowIndex !== -1) {
      selectedRowKeys.splice(rowIndex, 1);
    } else {
      selectedRowKeys.push(rowData.id);
    }

    setSelectedRows(selectedRowKeys);
    console.log(selectedRowKeys)
  };

  const columns=  [
    {
      key:"checkbox",
      title:"",
      width:50,
      selectedRowKeys:{selectedRows},
      cellRenderer:({ rowData }) => (
            <input
              type="checkbox"
              checked={selectedRows.includes(rowData.id)}
              onChange={() => handleRowSelection({ rowData })}
              />
        )}
      ,
    {key:"name", dataKey:"name", title:"Name", width:200},
    {key:"age", dataKey:"age", title:"Age", width:100}
    ]


  const data = [
    { id: 1, name: 'John Doe', age: 25 },
    { id: 2, name: 'Jane Smith', age: 30 },
    { id: 3, name: 'Bob Johnson', age: 35 },
    // Add more data as needed
  ];

  return (
    <BaseTable
      width={600}
      height={400}
      data={data}
      columns={columns}
      rowKey="id" 
    />
   
  );
};

export default MultiSelectTable;

How can I load the columsn dynamically into state but still have the selection of rows work?

Ngôn ngữ chính
TypeScript
Star
1.5k
Fork
170
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của Autodesk/react-base-table

Tất cả issue của Autodesk/react-base-table

Issue tương tự

Thêm issue về TypeScript

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.