Enable Handsontable with checkbox selection and update input ID with selected ITIDs
HTML
<link rel="stylesheet" type="text/css" href="https://cdn.jsdelivr.net/npm/handsontable@6.2.2/dist/handsontable.full.min.css">
<script src="https://cdn.jsdelivr.net/npm/handsontable@5.0.2/dist/handsontable.full.min.js"></script>
<div id="example"></div>
<br />
<input type="text" id="tbITID" />
JavaScript
var ddIM='';
const container = document.getElementById('example');
new Handsontable(container, {
data: [
{'Selected':false,'ITID':'1001','CSN':'ABCD1'},
{'Selected':false,'ITID':'1002','CSN':'ABCD2'}
],
editor: false,
comments: false,
contextMenu: false, // disable context menu to change things
disableVisualSelection: true, // prevent user from visually selecting
manualColumnResize: false, // prevent dragging to resize columns
manualRowResize: false,
columns: [{data:'Selected',type:'checkbox'},
{data:'ITID'},
{data:'CSN'}],
colHeaders: ['', 'Column 2', 'Column 3'],
afterOnCellMouseDown: function(event, coords, TD) {
if (coords.row >= 0) {
if(this.colToProp(coords.col) === 'Selected') {
if(this.getDataAtCell(coords.row,this.propToCol('Selected')) == false) {
ddIM = ddIM + this.getDataAtCell(coords.row,this.propToCol('ITID')) + '@@';
document.getElementById('tbITID').value = ddIM;
}
else {
ddIM = ddIM.replace(this.getDataAtCell(coords.row,this.propToCol('ITID')) + '@@','');
document.getElementById('tbITID').value = ddIM;
}
}
}
}
});