我正在使用'react-virtualized'来创建一个表。在该表中,一些数据可能显示为'<b>Brian Vaughn1</b>'
. 这个表格单元格应该有font-weight: bold
并且只应该呈现文本,没有标签。像这样:
我已经设法通过使用来解决这个问题ReactDOM.findDOMNode
(据我所知应该避免),但我认为应该有一个更好、更清洁的方法来做到这一点。这是我的代码:
import React, { Component } from 'react';
import { Column, Table } from 'react-virtualized';
import './chart-table.scss';
class ChartRTable extends Component{
constructor(props){
super(props);
this.handleScroll = this.handleScroll.bind(this);
}
getColumns(data){
const columnsNumber = data.namesForColumns.length;
const columnWidth = this.props.tableWidth / columnsNumber;
let namesForColumns = ['name', 'description'];
return namesForColumns.map( name => {
return (
<Column
label={name}
dataKey={name}
width={columnWidth}
/>
)
})
}
handleScroll(){
// not a very nice way to make use of <b> tags in data
let dom = ReactDOM.findDOMNode(this);
let all = dom.getElementsByClassName('ReactVirtualized__Table__rowColumn');
let texts = [].slice.call(all).filter( (text) => text.innerHTML.indexOf('<') > -1);
texts.map( (el) => {
let text = el.innerHTML;
let newText = text.replace(/<b>/g, '').replace(/<\/b>/g, '');
el.innerHTML = newText;
el.style.fontWeight = 'bold';
})
}
componentDidMount(){
this.handleScroll();
}
render() {
// dummy data
const list = [
{ name: 'Brian Vaughn', description: '<b>Software engineer1</b>' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: '<b>Brian Vaughn1</b>', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: '<b>Brian Vaughn</b>', description: '<b>Software engineer</b>' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
{ name: 'Brian Vaughn', description: 'Software engineer' },
];
return(
<Table
onScroll={this.handleScroll}
ref='table'
width={this.props.tableWidth}
height={this.props.tableHeight}
headerHeight={35}
rowHeight={37}
rowCount={list.length}
rowGetter={({ index }) => list[index] }
>
{this.getColumns(this.data)}
</Table>
)
}
};
//TODO PropTypes
export default ChartRTable;
有任何想法吗?