I have 3 handsontables in my React view. I need them to be synced when the user is scrolling both horizontally and vertically. I tried to use javascript to get horizontally working without any luck.
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>
);
};
ReactDOM.render(<App />, document.getElementById('example1'));
<link href="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.css" rel="stylesheet">
<script src="https://cdn.jsdelivr.net/npm/handsontable/dist/handsontable.full.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@handsontable/react/dist/react-handsontable.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react/16.6.3/umd/react.production.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-dom/16.6.3/umd/react-dom.production.min.js"></script>
I couldn't get it run in the StackOverflow code editor. so here is link to jsfiddle link