I'm using a library for React and I have a list (array) of headers and on the other side an array containing the table data.
After reading the documentation I was able to enable the menu where I added the options "Add Column to Left" and "Add Column to Right" but this was only done visually and did not update the React state title.
I tried two methods. The second method is "Custom Dropdown Configuration", I added the columns manually and used the handsontable method "updateSettings" to update the title, but it still doesn't work(sometimes it works, sometimes it's elsewhere Add column).
function addToHeader (array, index, side, valor) { if (side === "Left") array.splice(index, 0, valor) else if (side === "Right") array.splice(index + 1, 0, valor) return array } 在代码中: dropdownMenu: { items: { col_right: { name: 'Move column right', callback: (key: any, selection: any, clickEvent: any) => { const column = selection[0].end.col headers = addToHeader(headers, column, "Right", 'New column') console.log('headers', headers) // 返回正确更改的数组 hot.updateSettings({ colHeaders: headers }) } }, col_left: { name: 'Move column left', callback: (key: any, selection: any, clickEvent: any) => { const column = selection[0].end.col headers = addToHeader(headers, column, "Left", 'New column') console.log('headers', headers) // 返回正确更改的数组 hot.updateSettings({ colHeaders: headers }) } }, }
I completed this task in the following way:
The key is, what I didn't realize before is that in addition to the status of the table header not being updated, when moving the column, the table data must also be updated, which is where I implemented a js function to move the information (
moveColumnTable
)IMPORTANT NOTE: I implemented this function to move a single column