Как правильно использовать CellMeasurer для установки ширины столбца таблицы на основе длины содержимого? - PullRequest
2 голосов
/ 29 апреля 2019

Я пытаюсь динамически установить ширину столбца с помощью CellMeasurer в динамически создаваемых столбцах.

// hoping that CellMeasurer is what I needed to adjust
// width of my table columns, but not working.
const cellRenderer = ({ columnIndex, key, parent, rowIndex, style }) => {
    const content = list[rowIndex][columnHeader[columnIndex].columnName];
    return (
      <CellMeasurer
        cache={cache}
        columnIndex={columnIndex}
        key={key}
        parent={parent}
        rowIndex={rowIndex}
      >
        <div
          style={{
            ...style,
            height: 35,
            whiteSpace: "nowrap"
          }}
        >
          {content}
        </div>
      </CellMeasurer>
    );
  };

  const cache = new CellMeasurerCache({
    defaultWidth: 100,
    minWidth: 75,
    fixedHeight: true
  });

return (
<AutoSizer>
  {({ height, width }) => (
    <div width={width}>
      <Table
        width={width}
        height={500} // why can't i use {height} here?
        headerHeight={40}
        rowHeight={30}
        rowCount={list.length}
        rowGetter={({ index }) => list[index]}
      >
        // Dynamically create Column
        {columnHeader.map((columnData, i) => (
          <Column
            width={800}
            onClick={this.onHeaderClick}
            label={columnData.displayName}
            dataKey={columnData.columnName}
            cellRenderer={cellRenderer}
            {...this.props}
            key={i}
          />
        ))}
      </Table>
    </div>
  )}
</AutoSizer>
);

Вот пример кода: https://codesandbox.io/s/mzj5y255kj

Я хочу установить столбец «Новости» равным длине в тексте.

Результатом будет то, что независимо от длины содержимого столбец будет расти.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...