How to set table width based on its content?

My implementation has the following problem.

When initializing a table that does not fit into the container in width and height, the vertical scroll is not displayed, since it is set at the end of the table. If I edit a cell, the horizontal scroll position is reset to the beginning of the table and a vertical scroll will also appear, inherent in the container itself, into which the table does not fit in width. Later, when editing, the scroll position is not reset.

The goal I am trying to achieve is that I have a dynamic table in width, which can have any number of columns, I want the table width to be set “in fact”, so that the vertical scroll is next to the table. But if the table does not fit into the parent container, then the scroll should be on the acceptable border of this container, and not at the end of the table, which can only be reached by scrolling the horizontal scroll. And so that the horizontal scroll position is not reset when editing.