我的 React 视图中有 3 个实践表。当用户水平和垂直滚动时,我需要它们同步。我尝试使用 javascript 在没有任何运气的情况下进行水平工作。
import React from 'react';
import ReactDOM from 'react-dom';
const handsontableData = Handsontable.helper.createSpreadsheetData(6, 50);
const Table1 = () => (
<div>
<Handsontable.react.HotTable
id="T1"
data={handsontableData}
colHeaders
rowHeaders
colWidths={[100]}
height={200}
width="100vw"
/>
</div>
);
const Table2 = () => (
<div>
<Handsontable.react.HotTable
id="T2"
data={handsontableData}
colHeaders
rowHeaders
colWidths={[100]}
height={200}
width="100vw"
/>
</div>
);
const Table3 = () => (
<div>
<Handsontable.react.HotTable
id="T3"
data={handsontableData}
colHeaders
rowHeaders
colWidths={[100]}
height={200}
width="100vw"
/>
</div>
);
const App = () => {
const T1Obj = document.getElementById('T1');
const T2Obj = document.getElementById('T2');
const T3Obj = document.getElementById('T3');
function getScroll(event) {
let elem;
if (event.type === 'scroll') {
elem = (event.srcElement) ? event.srcElement : event.target;
if (elem.id === 'T1') {
T2Obj.scrollLeft = elem.scrollLeft;
T3Obj.scrollLeft = elem.scrollLeft;
} else if (elem.id === 'T2') {
T1Obj.scrollLeft = elem.scrollLeft;
T3Obj.scrollLeft = elem.scrollLeft;
} else if (elem.id === 'T3') {
T1Obj.scrollLeft = elem.scrollLeft;
T2Obj.scrollLeft = elem.scrollLeft;
}
}
}
if (typeof addEventListener !== 'undefined') {
T1Obj && T1Obj.addEventListener('scroll', getScroll);
T2Obj && T2Obj.addEventListener('scroll', getScroll);
} else {
T1Obj && T1Obj.attachEvent('onscroll', getScroll);
T2Obj && T2Obj.attachEvent('onscroll', getScroll);
}
return (
<div>
<Table1 />
<br />
<Table2 />
<br />
<Table3 />
<br />
</div>
);
};
我无法让它在 StackOverflow 代码编辑器中运行。所以这里是 jsfiddle
慕尼黑8549860
相关分类