当React-Table(v6)树形表段打开时如何运行功能?

问题描述 投票:0回答:1

我正在使用React Table (version 6)

我的桌子正在使用Tree Table HoC

tree-table hoc example

当其中一个树表段打开时,我需要运行一些代码。我不知道该怎么做。

是否有简单的方法可以做到这一点?

reactjs react-table
1个回答
0
投票

键最终是onExpandedChange属性。>>

const toArray = iterable => [].slice.call(iterable)

const Table = () => {
    const $tableWrapper = useRef()

    const getGroupFromIndexPath = indexPath => {
        return indexPath.reduce(($parentElem, index) => {
            return toArray($parentElem.children).filter($child => $child.classList.contains('rt-tr-group'))[index]
        }, $tableWrapper.current.querySelector('.rt-tbody'))
    }

    return (
        <div ref={$tableWrapper}>
            <ReactTable
                data={data}
                columns={columns}
                pivotBy={['name']}
                // I have no idea what the 1st parameter value is supposed to represent:/
                onExpandedChange={(value1, indexPath, event, rowData) => {
                    const $parentGroup = getGroupFromIndexPath(indexPath)
                    const $childGroup = $parentGroup.querySelector('.rt-tr-group')

                    if ($childGroup) {
                        // The expander is open
                    } else {
                        // The expander is closed
                    }
                }}
            />
        </div>
    )
}
© www.soinside.com 2019 - 2024. All rights reserved.