【问题标题】:React how to make table container that beautiful dnd worksReact 如何制作漂亮的 dnd 工作的表格容器
【发布时间】:2021-01-29 22:35:27
【问题描述】:

我用漂亮的 dnd 组件制作了一个表格,以便我可以重新排序行 但由于我有大量数据,我想制作一个表格容器,这样我就可以拥有一个可滚动的固定大小的表格 但如果我添加“react-table-container”,拖放不再起作用。

谁能帮我制作一个可拖动的容器

export default function DataTable({ data, title }) {
  const columns = useMemo(() => COLUMNS, []);
  const {
    getTableProps,
    getTableBodyProps,
    headerGroups,
    rows,
    prepareRow,
  } = useTable(
    {
      columns,
      data,
    },
    useSortBy
  );
  return (
    <>
      <div className="title">{title} </div>

  <DragDropContext
    onDragEnd={(param) => {
      if (!param.destination) {
        return;
      }
      const srcIdx = param.source.index;
      const desIdx = param.destination.index;
      rows.splice(desIdx, 0, rows.splice(srcIdx, 1)[0]);
    }}
  >
    <ReactTableContainer width="auto" height="800px">
      <Droppable droppableId="droppable">
        {(provided, snapshot) => (
          <table
            ref={provided.innerRef}
            id="players"
            {...getTableProps()}
            style={{ width: 1000, height: 500 }}
          >
            <thead>
              {headerGroups.map((headerGroup) => (
                <tr {...headerGroup.getHeaderGroupProps()}>
                  {headerGroup.headers.map((column) => (
                    <th
                      {...column.getHeaderProps(
                        column.getSortByToggleProps()
                      )}
                    >
                      {column.render("Header")}
                      <span className="sort-icon">
                        {column.isSorted ? (
                          column.isSortedDesc ? (
                            <SortUpIcon />
                          ) : (
                            <SortDownIcon />
                          )
                        ) : (
                          ""
                        )}
                      </span>
                      <div className="filter">
                        {column.canFilter ? column.render("Filter") : null}
                      </div>
                    </th>
                  ))}
                </tr>
              ))}
            </thead>
            <tbody {...getTableBodyProps()}>
              {rows.map((row, i) => {
                prepareRow(row);
                return (
                  <Draggable
                    key={row.id}
                    draggableId={row.id}
                    index={i}
                    className="draggableRow"
                  >
                    {(provided, snapshot) => (
                      <tr
                        ref={provided.innerRef}
                        {...provided.draggableProps}
                        {...provided.dragHandleProps}
                        {...row.getRowProps()}
                        style={{
                          ...provided.draggableProps.style,
                          boxShadow: snapshot.isDragging
                            ? "0 0 .4rem #666"
                            : "none",
                        }}
                      >
                        {row.cells.map((cell) => {
                          return (
                            <td {...cell.getCellProps()}>
                              {cell.render("Cell")}
                            </td>
                          );
                        })}
                      </tr>
                    )}
                  </Draggable>
                );
              })}
              {provided.placeholder}
            </tbody>
          </table>
        )}
      </Droppable>
    </ReactTableContainer>
  </DragDropContext>
</>

); }

【问题讨论】:

    标签: reactjs react-table react-beautiful-dnd


    【解决方案1】:

    ref={provided.innerRef}下添加{...provided.droppableProps}

    <table
       ref={provided.innerRef}
       id="players"
       {...provided.droppableProps}
       {...getTableProps()}
       style={{ width: 1000, height: 500 }}
    >
    

    【讨论】:

    • 我的代码正在使用可拖动的表格我只想添加表格容器
    • 你能用你的代码创建codesandbox或codepen,这样我就可以加入了吗?
    • codesandbox.io/s/peaceful-dew-cwjsc?file=/src/DataTable.js 这是我从自己的数据文件中导入数据的链接
    • 这是我遇到问题的 DataTable 文件
    • 当我运行你的代码沙箱时,它给了我很多关于缺少依赖项的错误,即使我安装了它们,它也在起作用,你能检查一下吗?它适合你吗?
    猜你喜欢
    • 1970-01-01
    • 2018-07-26
    • 1970-01-01
    • 2012-08-29
    • 2021-03-06
    • 1970-01-01
    • 2014-09-11
    • 2021-01-29
    • 2016-01-05
    相关资源
    最近更新 更多