【问题标题】:How to change CSS of columns - ReactTable如何更改列的 CSS - ReactTable
【发布时间】:2018-07-27 01:58:40
【问题描述】:

我在我的应用程序中使用react-table

我一直在做一件事,即在调整列大小时更改 CSScolumns

目前,当您 resize 时,只有 cursor 会发生变化。我想要的是将border 添加到selected column

我也在SOgoogle 上搜索过这个。但是找不到有用的东西。而且在文档中也没有提到这个话题。

更新

现在我可以在调整大小时在拖动列时添加边框。我可以通过添加和删除类来做到这一点。

我做了什么:

在 className 的 state 中创建了一个 var:

  this.state = {
         addBorder: null
   }

在我的专栏中传递了这个类名:

     const columns = [{
    Header: 'Name',
    accessor: 'name', // String-based value accessors!,
    headerClassName: this.state.addBorder,
    className: this.state.addBorder
}, {
    Header: 'Age',
    accessor: 'age',
    Cell: props => <span className='number'>{2}</span> // Custom cell components!
}, {
    id: 'friendName', // Required because our accessor is not a string
    Header: 'Friend Name',
    accessor: d => d.friend.name // Custom value accessors!
}, {
    Header: props => <span>Friend Age</span>, // Custom header components!
    accessor: 'friend.age'
}];

return (
    <div onMouseUp={this.handleMouseUp}>
    <ReactTable
        data={data}
        columns={columns} 
        resizable={true}
        onResizedChange={(col, e) => {
            const column = col[col.length-1];
            this.setState({addBorder: column.id})
        }} />
        </div>
)
}

拖动结束时删除类:

   handleMouseUp (e) {
    this.setState({addBorder: null});
}

但我仍然无法在悬停时添加边框。

现在,我将在标题道具中发送我的自定义 HTML。在我的 HTML 中,我制作了一个额外的 div。我已经把这个 div 移到了右边。在这个 div 悬停时,我会发出鼠标事件并相应地更改 CSS。

但是负责调整列大小的标题中的现有 div 与我的 Div 重叠。

  Header: props => <div className='header-div'> Name <div onMouseOver = {() => {
        console.log('mose');
        this.setState({className: 'addBorder'});
    }} className='hover-div' onMouseOut = {() => {console.log('sdasd');this.setState({className: null});}}> </div></div> ,

【问题讨论】:

  • 我认为没有处理程序可以做到这一点。但是,您可以使用 onResizedChange 回调来弥补。

标签: css reactjs resize react-table


【解决方案1】:

据我了解,您希望在将鼠标悬停在列标题上时添加一些边框。如果我的理解是正确的,你可以在 header 类上使用:hover 伪选择器

.hdrCls:hover {
  border: 2px solid rgba(0,0,0,0.6) !important;
}

更新:

您可以在 react-table 公开的onResizedChange 处理程序中操作状态

onResizedChange={(newResized, event) => {
  let resizedCol = newResized.slice(-1)[0].id;
  if(this.state.activeCol !== resizedCol) {
    this.setState({
      activeCol: resizedCol,
      resizing: true
    })
  }
}}

此外,请确保您必须在 mouseup 事件中将 resizing 状态设置为 false。为此,我提出了以下解决方案。

componentDidUpdate(props, state) {
  if (this.state.resizing && !state.resizing) {
    document.addEventListener('mouseup', this.onMouseUp);
  } else if (!this.state.resizing && state.resizing) {
    document.removeEventListener('mouseup', this.onMouseUp);
  }
}

onMouseUp = (evt) => {
  this.setState({
    activeCol: '',
    resizing: false
  });
  evt.stopPropagation();
  evt.preventDefault();
}      

供参考:

const ReactTable = window.ReactTable.default

class App extends React.Component {
  constructor(props) {
    super(props)
    this.state = {
      activeCol: '',
      resizing: false
    }
  }
  
  componentDidUpdate(props, state) {
    if (this.state.resizing && !state.resizing) {
      document.addEventListener('mouseup', this.onMouseUp);
    } else if (!this.state.resizing && state.resizing) {
      document.removeEventListener('mouseup', this.onMouseUp);
    }
  }
  
  onMouseUp = (evt) => {
    this.setState({
      activeCol: '',
      resizing: false
    });
    evt.stopPropagation();
    evt.preventDefault();
  }
  
  render() {
    const data = [{
      name:"Mark",
      age:24
    },
    {
      name:"Derek",
      age:26
    }]

    const columns = [{
          Header: 'Name',
          accessor: 'name', // String-based value accessors!,
          headerClassName: 'hdrCls',
          className: (this.state.activeCol === 'name') && this.state.resizing ? 'borderCellCls' : 'defaultCellCls'
      }, {
          Header: 'Age',
          accessor: 'age',
          headerClassName: 'hdrCls',
          className: (this.state.activeCol === 'age') && this.state.resizing ? 'borderCellCls' : 'defaultCellCls'
      }];

    return <ReactTable
      data = { data }
      columns = { columns }
      showPagination= {false}
      onResizedChange={(newResized, event) => {
        let resizedCol = newResized.slice(-1)[0].id;
        if(this.state.activeCol !== resizedCol) {
          this.setState({
            activeCol: resizedCol,
            resizing: true
          })
        }
      }}
    />
  }
}

ReactDOM.render( < App / > , document.getElementById("app"))
.hdrCls:hover {
  border: 2px solid rgba(0,0,0,0.6) !important;
}


.borderCellCls {
  border-right: 2px solid rgba(0,0,0,0.6) !important;
  border-left: 2px solid rgba(0,0,0,0.6) !important;
}

.defaultCellCls {
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/15.1.0/react-dom.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.7.6/react-table.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/react-table/6.7.6/react-table.css"></link>
<div id="app"></div>

您可以使用 CSS。希望这是您想要的,希望对您有所帮助。

更新:

我认为你必须使用 CSS 来实现你想要的。

.borderCellCls {
   border-right: 2px solid rgba(0,0,0,0.6) !important;
   border-left: 2px solid rgba(0,0,0,0.6) !important;
}

【讨论】:

  • 感谢@dev,在您的解决方案中,当我将鼠标悬停在列的边缘时,只有一列突出显示。我想要的是在 while 列中添加边框,而不是在一个单元格上添加。
  • @Dalvik,是的,根据上述逻辑,当您尝试调整特定列的大小时,该列会出现边框。这是我的理解。如果我错了,请纠正我,你想要的是相同的行为,但不是单元格被突出显示,整列应该在调整大小时显示边框。对吧??
  • @Dalvik,我认为如果上面的答案接近你想要的,唯一的问题是,边框应该显示在列而不是单元格上,我建议你可以用CSS 来达到预期的效果。但乐于助人。有时间我会再试一次。
【解决方案2】:

如果您在这里了解如何将 className 设置为列单元格(使用 react-table),这里是解决方案

1)

<tr
            
              {...row.getRowProps()}
            >
              {row.cells.map((cell) => (
                <td
                  {...cell.getCellProps([
                    {
                      className: cell.column.className, // pay attention to this
                      style: cell.column.style,
                      // set here your other custom props
                    },
                  ])}
                >
                  {cell.render('Cell')}
                </td>
              ))}
            </tr>

2)

  const columns = React.useMemo(
() => [
  {
    Header: 'Date',
    accessor: 'date',
    minWidth: 70,
    className: 'text-dark fw-bolder fs-6 min-w-70px', // pass className props here
    headerClassName: 'text-muted', // or another props like this one
  }]
  
<Table columns={columns} ... />

最后,那些 props 将被传递到您的单元格

【讨论】:

    【解决方案3】:

    对于 TypeScript 支持,请遵循 DefinitelyTyped 中的说明,即。使用说明中的内容创建文件/src/types/react-table-config.d.ts,然后在其中添加以下内容以支持列上的自定义属性(根据需要在最后一行添加更多属性):

      // Added to support classes to template from:
      // https://github.com/DefinitelyTyped/DefinitelyTyped/tree/master/types/react-table
      export interface ColumnInstance<
        D extends Record<string, unknown> = Record<string, unknown>
      > extends Omit<ColumnInterface<D>, 'id'>,
          ColumnInterfaceBasedOnValue<D>,
          UseTableColumnProps<D>,
          Record<headerClassName | className, string> {}
    

    【讨论】:

      猜你喜欢
      • 2020-11-02
      • 2019-03-06
      • 1970-01-01
      • 2015-05-13
      • 2018-06-13
      • 1970-01-01
      • 2012-08-12
      • 2020-11-12
      • 2015-04-12
      相关资源
      最近更新 更多